diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 65ea73f..24f390e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ add_library(fever_engine core/light.cpp core/render.cpp core/shader.cpp + core/time.cpp input/input.cpp scene/gltf_loader.cpp scene/scene.cpp diff --git a/src/bin/Controller.cpp b/src/bin/Controller.cpp index 2b41608..3793cef 100644 --- a/src/bin/Controller.cpp +++ b/src/bin/Controller.cpp @@ -56,8 +56,7 @@ void Controller::run() // This is the game loop while (glfwWindowShouldClose(&m_gameWindow->glfw_window()) == GLFW_FALSE) { // --- Timing --- - limit_framerate(); - update_delta_time(m_scene->registry()); + Time::update_delta_time(m_scene->registry()); // --- Check events, handle input --- m_gameWindow->clear_mouse_cursor_input(); @@ -88,44 +87,8 @@ void Controller::run() // Update window size if (m_gameWindow->dimensions_changed()) { - update_window_dimensions(); + auto dimensions = m_gameWindow->physical_dimensions(); + post_processing_framebuffer = Framebuffer(dimensions); } } } - -void Controller::limit_framerate() -{ - static double startingTime = 0.0; - static double lastTime = 0.0; - - lastTime = glfwGetTime() - startingTime; - - double frameTime = 1 / (double)MAX_FPS; - if (frameTime > lastTime) { - static constexpr auto MICROSECONDS_PER_SECOND = 1'000'000; - auto sleep_time_us = - static_cast((frameTime - lastTime) * MICROSECONDS_PER_SECOND); - std::this_thread::sleep_for(std::chrono::microseconds(sleep_time_us)); - } - - m_deltaTime = glfwGetTime() - startingTime; - startingTime = glfwGetTime(); -} - -void Controller::update_window_dimensions() -{ - // m_camera->updateAspectRatio(m_gameWindow->aspectRatio()); - // m_gameEventHandler->setFirstMouseInput(1); - - auto dimensions = m_gameWindow->physical_dimensions(); - post_processing_framebuffer = Framebuffer(dimensions); -} - -void Controller::update_delta_time(entt::registry& registry) const -{ - static constexpr auto MICROSECONDS_PER_SECOND = 1'000'000; - - registry.ctx().erase(); - registry.ctx().emplace( - std::chrono::microseconds(static_cast(m_deltaTime * MICROSECONDS_PER_SECOND))); -} diff --git a/src/bin/Controller.h b/src/bin/Controller.h index 67bf0fa..d1ee0d6 100644 --- a/src/bin/Controller.h +++ b/src/bin/Controller.h @@ -24,10 +24,6 @@ public: void run(); private: - void limit_framerate(); - void update_delta_time(entt::registry& registry) const; - void update_window_dimensions(); - std::shared_ptr m_gameWindow; std::shared_ptr m_scene; Shader skybox_shader{"skybox", "data/shaders"}; diff --git a/src/core/glad.cpp b/src/core/glad.cpp index c0ab670..462324f 100644 --- a/src/core/glad.cpp +++ b/src/core/glad.cpp @@ -151,6 +151,6 @@ void init_glad() // Enable multisampling (a bit redundant because most graphics drivers do this automatically) glEnable(GL_MULTISAMPLE); - // Disable VSync - glfwSwapInterval(0); + // Enable VSync + glfwSwapInterval(1); } diff --git a/src/core/time.cpp b/src/core/time.cpp new file mode 100644 index 0000000..7c0dfa4 --- /dev/null +++ b/src/core/time.cpp @@ -0,0 +1,11 @@ +#include "time.h" +#include + +void Time::update_delta_time(entt::registry& registry) +{ + auto current_time = std::chrono::system_clock::now(); + + auto& delta_time = registry.ctx().emplace(); + delta_time.delta = current_time - delta_time.last_time; + delta_time.last_time = current_time; +} diff --git a/src/core/time.h b/src/core/time.h index ae1d5bd..010eb83 100644 --- a/src/core/time.h +++ b/src/core/time.h @@ -1,12 +1,16 @@ #pragma once #include +#include namespace Time { struct Delta { std::chrono::duration delta; + std::chrono::time_point last_time; }; +void update_delta_time(entt::registry& registry); + } // namespace Time \ No newline at end of file