diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index c53be1e..8ab98d6 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -96,7 +96,8 @@ int main(int argc, char *argv[]) { brick_e.add_component("examples/dev_testing/resources/bricks/bricks.fbx"); brick_e.add_component(); auto &brick_transf = brick_e.add_component(); - brick_transf.translate(6.f, -0.5f, 1.f); + brick_transf.translate(6.f, 0.f, 0.f); + //brick_transf.translate(6.f, -0.5f, 1.f); auto light = std::make_shared(core_shader, glm::vec3(0.f, 0.f, 0.f), glm::vec3(1.f, 1.f, 1.f)); game.add_event(light); diff --git a/include/simpleengine/game.h b/include/simpleengine/game.h index 0078a61..e3bcd55 100644 --- a/include/simpleengine/game.h +++ b/include/simpleengine/game.h @@ -66,10 +66,9 @@ namespace simpleengine { int fps_limit = -1; bool enable_vsync = true; - int max_engine_tps = 60; // The maximum engine TPS - double tps_accumulator = 0.f; - //int engine_ticks_second = 0; // The amount of ticks in a second - //double last_sec_engine_tick; // The time of the last second + int max_engine_tps = 120; // The maximum engine TPS + float fixed_delta_time = 1.f / (float) max_engine_tps; // The delta time from fixed timestep + float tps_accumulator = 0.f; float get_delta_time(); diff --git a/src/game.cpp b/src/game.cpp index 22a6573..bab41da 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -158,33 +158,31 @@ void simpleengine::Game::limit_framerate(const float& delta_time) const { int simpleengine::Game::run() { while (!glfwWindowShouldClose(window)) { // Get delta time first thing - float delta_time = get_delta_time(); - //std::cout << "Delta time: " << delta_time << std::endl; + float frame_time = get_delta_time(); // Poll input events glfwPollEvents(); + input_update(frame_time); // Update input on varying timestep - const double max_delta_time = 0.25; + tps_accumulator += frame_time; - tps_accumulator += delta_time; + // https://gafferongames.com/post/fix_your_timestep/ + while (tps_accumulator >= fixed_delta_time) { + update(fixed_delta_time); - input_update(delta_time); - - while (tps_accumulator >= max_delta_time) { - update(max_delta_time); - - tps_accumulator -= max_delta_time; + tps_accumulator -= fixed_delta_time; } - - const double interpolate_alpha = tps_accumulator / max_delta_time; - render_window(interpolate_alpha, delta_time); + // Alpha used for interpolating objects in rendering + float interpolate_alpha = tps_accumulator / fixed_delta_time; + + render_window(interpolate_alpha, frame_time); // End draw glfwSwapBuffers(window); glFlush(); - limit_framerate(delta_time); + limit_framerate(frame_time); } return 0;