Skip to content

Commit

Permalink
[Application] The Application now holds a FrameTimeInfo directly
Browse files Browse the repository at this point in the history
- The callback given to Application::run() receives the whole FrameTimeInfo object instead of just the delta time
  • Loading branch information
Razakhel committed Mar 4, 2024
1 parent 1643846 commit 3832b78
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 61 deletions.
4 changes: 2 additions & 2 deletions examples/audioDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,8 @@ int main() {

std::vector<uint8_t> captureData;

app.run([&] (float) {
soundTrans.setPosition((moveSource ? Raz::Vec3f(std::sin(app.getGlobalTime()) * 3.f, 0.f, 1.f) : Raz::Vec3f(0.f)));
app.run([&] (const Raz::FrameTimeInfo& timeInfo) {
soundTrans.setPosition((moveSource ? Raz::Vec3f(std::sin(timeInfo.globalTime) * 3.f, 0.f, 1.f) : Raz::Vec3f(0.f)));

if (isCapturing) {
microphone.recoverData(captureData);
Expand Down
4 changes: 2 additions & 2 deletions examples/bloomDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ int main() {
// Starting application //
//////////////////////////

app.run([&] (float deltaTime) {
meshTrans.rotate(-45.0_deg * deltaTime, Raz::Axis::Y);
app.run([&] (const Raz::FrameTimeInfo& timeInfo) {
meshTrans.rotate(-45.0_deg * timeInfo.deltaTime, Raz::Axis::Y);

#if !defined(USE_OPENGL_ES)
geomPlot.push(geometryPass.recoverElapsedTime());
Expand Down
2 changes: 1 addition & 1 deletion examples/fullDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ end)";
//////////////////////////

#if !defined(USE_OPENGL_ES)
app.run([&] (float) {
app.run([&] (const Raz::FrameTimeInfo&) {
geomPlot.push(geometryPass.recoverElapsedTime());
chromAberrPlot.push(chromaticAberration.recoverElapsedTime());
blurPlot.push(boxBlur.recoverElapsedTime());
Expand Down
4 changes: 2 additions & 2 deletions examples/showcaseDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ int main() {

loadBallScene(mesh, render);

app.run([&] (float deltaTime) {
meshTrans.rotate(-45.0_deg * deltaTime, Raz::Axis::Y);
app.run([&] (const Raz::FrameTimeInfo& timeInfo) {
meshTrans.rotate(-45.0_deg * timeInfo.deltaTime, Raz::Axis::Y);
});
} catch (const std::exception& exception) {
Raz::Logger::error("Exception occurred: "s + exception.what());
Expand Down
10 changes: 3 additions & 7 deletions examples/ssrDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,12 @@ int main() {
// Starting application //
//////////////////////////

app.run([&] (float deltaTime) {
static float totalTime = 0.f;
app.run([&] (const Raz::FrameTimeInfo& timeInfo) {
const float sinTime = std::sin(timeInfo.globalTime);

const float sinTime = std::sin(totalTime);

redBoxTrans.rotate(Raz::Quaternionf(90_deg * deltaTime, Raz::Vec3f(0.f, 0.707106769f, 0.707106769f)));
redBoxTrans.rotate(Raz::Quaternionf(90_deg * timeInfo.deltaTime, Raz::Vec3f(0.f, 0.707106769f, 0.707106769f)));
greenBallTrans.setPosition(0.f, 2.f + sinTime, 0.f);
blueBallTrans.setPosition(2.f - sinTime, 1.f, 0.f);

totalTime += deltaTime;
});
} catch (const std::exception& exception) {
Raz::Logger::error("Exception occurred: "s + exception.what());
Expand Down
4 changes: 2 additions & 2 deletions examples/tessellationDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ int main() {
Raz::Renderer::setPolygonMode(Raz::FaceOrientation::FRONT_BACK, Raz::PolygonMode::FILL);
}, true);

app.run([&] (float deltaTime) {
transform.rotate(-45.0_deg * deltaTime, Raz::Axis::Y);
app.run([&] (const Raz::FrameTimeInfo& timeInfo) {
transform.rotate(-45.0_deg * timeInfo.deltaTime, Raz::Axis::Y);
});
} catch (const std::exception& exception) {
Raz::Logger::error("Exception occurred: "s + exception.what());
Expand Down
27 changes: 13 additions & 14 deletions include/RaZ/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ class Application {

const std::vector<WorldPtr>& getWorlds() const { return m_worlds; }
std::vector<WorldPtr>& getWorlds() { return m_worlds; }
float getDeltaTime() const { return m_deltaTime; }
float getGlobalTime() const { return m_globalTime; }
float getFixedTimeStep() const { return m_fixedTimeStep; }
const FrameTimeInfo& getTimeInfo() const { return m_timeInfo; }

void setFixedTimeStep(float fixedTimeStep) { assert("Error: Fixed time step must be positive." && fixedTimeStep > 0.f); m_fixedTimeStep = fixedTimeStep; }
void setFixedTimeStep(float fixedTimeStep) {
assert("Error: Fixed time step must be positive." && fixedTimeStep > 0.f);
m_timeInfo.substepTime = fixedTimeStep;
}

/// Adds a World into the Application.
/// \tparam Args Types of the arguments to be forwarded to the World.
/// \param args Arguments to be forwarded to the World.
/// \return Reference to the newly added World.
/// Adds a world into the application.
/// \tparam Args Types of the arguments to be forwarded to the world.
/// \param args Arguments to be forwarded to the world.
/// \return Reference to the newly added world.
template <typename... Args> World& addWorld(Args&&... args);
/// Runs the application.
void run();
/// Runs the application and call the given callable between each cycle.
/// \tparam FuncT Type of the callback to call between each cycle.
/// \param callback Callback to call between each cycle.
/// Runs the application and call the given callable on each cycle.
/// \tparam FuncT Type of the callback to call on each cycle.
/// \param callback Callback to call on each cycle.
template <typename FuncT> void run(FuncT&& callback);
/// Runs one cycle of the application.
/// \return True if the application is still running, false otherwise.
Expand All @@ -52,10 +53,8 @@ class Application {
std::vector<WorldPtr> m_worlds {};
Bitset m_activeWorlds {};

FrameTimeInfo m_timeInfo{ 0.f, 0.f, 0, 0.016666f }; ///< Time-related attributes for each cycle.
std::chrono::time_point<std::chrono::system_clock> m_lastFrameTime = std::chrono::system_clock::now();
float m_deltaTime {}; ///< Time elapsed since the application's last execution, in seconds.
float m_globalTime = 0.f; ///< Time elapsed since the application started, in seconds.
float m_fixedTimeStep = 0.016666f; ///< Time to be used by each fixed time step.
float m_remainingTime {}; ///< Extra time remaining after executing the systems' fixed step update.

bool m_isRunning = true;
Expand Down
4 changes: 2 additions & 2 deletions include/RaZ/Application.inl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ void Application::run(FuncT&& callback) {
#if defined(RAZ_PLATFORM_EMSCRIPTEN)
static auto emCallback = [this, callback = std::forward<FuncT>(callback)] () {
runOnce();
callback(m_deltaTime);
callback(m_timeInfo);
};

emscripten_set_main_loop_arg([] (void* lambda) {
(*static_cast<decltype(&emCallback)>(lambda))();
}, &emCallback, 0, 1);
#else
while (runOnce())
callback(m_deltaTime);
callback(m_timeInfo);
#endif
}

Expand Down
8 changes: 4 additions & 4 deletions scripts/demo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ local light = world:addEntity()
light:addComponent(Transform.new())
light:addComponent(Light.new(LightType.DIRECTIONAL, -Axis.Z, 1, ColorPreset.White))

app:run(function (deltaTime)
shieldTrans:rotate(Radiansf.new(Degreesf.new(90 * deltaTime)), Vec3f.new(1, 1, 1):normalize())
cubeTrans.position = Vec3f.new(-2, math.sin(app:getGlobalTime() * 3), 0)
sphereTrans.scaling = Vec3f.new(math.sin(app:getGlobalTime() * 2) * 0.25 + 0.75)
app:run(function (timeInfo)
shieldTrans:rotate(Radiansf.new(Degreesf.new(90 * timeInfo.deltaTime)), Vec3f.new(1, 1, 1):normalize())
cubeTrans.position = Vec3f.new(-2, math.sin(timeInfo.globalTime * 3), 0)
sphereTrans.scaling = Vec3f.new(math.sin(timeInfo.globalTime * 2) * 0.25 + 0.75)
end)
21 changes: 9 additions & 12 deletions src/RaZ/Application.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "RaZ/Application.hpp"
#include "RaZ/World.hpp"
#include "RaZ/Utils/Logger.hpp"

#if defined(RAZ_PLATFORM_EMSCRIPTEN)
Expand Down Expand Up @@ -28,29 +27,27 @@ void Application::run() {

bool Application::runOnce() {
const auto currentTime = std::chrono::system_clock::now();
m_deltaTime = std::chrono::duration<float>(currentTime - m_lastFrameTime).count();
m_timeInfo.deltaTime = std::chrono::duration<float>(currentTime - m_lastFrameTime).count();
m_timeInfo.globalTime += m_timeInfo.deltaTime;
m_lastFrameTime = currentTime;
m_globalTime += m_deltaTime;

int substepCount = 0;
m_remainingTime += m_deltaTime;
m_timeInfo.substepCount = 0;
m_remainingTime += m_timeInfo.deltaTime;

while (m_remainingTime >= m_fixedTimeStep) {
++substepCount;
m_remainingTime -= m_fixedTimeStep;
while (m_remainingTime >= m_timeInfo.substepTime) {
++m_timeInfo.substepCount;
m_remainingTime -= m_timeInfo.substepTime;
}

const FrameTimeInfo timeInfo{ m_deltaTime, m_globalTime, substepCount, m_fixedTimeStep };

for (std::size_t worldIndex = 0; worldIndex < m_worlds.size(); ++worldIndex) {
if (!m_activeWorlds[worldIndex])
continue;

if (!m_worlds[worldIndex]->update(timeInfo))
if (!m_worlds[worldIndex]->update(m_timeInfo))
m_activeWorlds.setBit(worldIndex, false);
}

return m_isRunning && !m_activeWorlds.isEmpty();
return (m_isRunning && !m_activeWorlds.isEmpty());
}

} // namespace Raz
17 changes: 8 additions & 9 deletions src/RaZ/Script/LuaCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ void LuaWrapper::registerCoreTypes() {
sol::usertype<Application> application = state.new_usertype<Application>("Application",
sol::constructors<Application(),
Application(std::size_t)>());
application["getWorlds"] = PickNonConstOverload<>(&Application::getWorlds);
application["getDeltaTime"] = &Application::getDeltaTime;
application["getGlobalTime"] = &Application::getGlobalTime;
application["fixedTimeStep"] = sol::property(&Application::getFixedTimeStep, &Application::setFixedTimeStep);
application["addWorld"] = &Application::addWorld<>;
application["run"] = sol::overload([] (Application& app) { app.run(); },
[] (Application& app, const std::function<void(float)>& func) { app.run(func); });
application["runOnce"] = &Application::runOnce;
application["quit"] = &Application::quit;
application["getWorlds"] = PickNonConstOverload<>(&Application::getWorlds);
application["getTimeInfo"] = &Application::getTimeInfo;
application["setFixedTimeStep"] = &Application::setFixedTimeStep;
application["addWorld"] = &Application::addWorld<>;
application["run"] = sol::overload([] (Application& app) { app.run(); },
[] (Application& app, const std::function<void(const FrameTimeInfo&)>& func) { app.run(func); });
application["runOnce"] = &Application::runOnce;
application["quit"] = &Application::quit;
}

{
Expand Down
6 changes: 2 additions & 4 deletions tests/src/RaZ/Script/LuaCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ TEST_CASE("LuaCore Application", "[script][lua][core]") {
application:addWorld()
assert(#application:getWorlds() == 1)
application.fixedTimeStep = 0.5
assert(application.fixedTimeStep == 0.5)
application:setFixedTimeStep(0.5)
assert(application:getTimeInfo().substepTime == 0.5)
application:run()
application:run(function () end)
assert(not application:runOnce())
assert(application:getDeltaTime() >= 0)
assert(application:getGlobalTime() >= 0)
application:quit()
)"));
}
Expand Down

0 comments on commit 3832b78

Please sign in to comment.