Enable VSync by default

This commit is contained in:
2023-06-09 20:04:02 +02:00
parent cf7d2ee7bf
commit 3fcf6414f8
6 changed files with 21 additions and 46 deletions

View File

@@ -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

View File

@@ -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<unsigned>((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<Time::Delta>();
registry.ctx().emplace<Time::Delta>(
std::chrono::microseconds(static_cast<unsigned>(m_deltaTime * MICROSECONDS_PER_SECOND)));
}

View File

@@ -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<Window> m_gameWindow;
std::shared_ptr<Scene> m_scene;
Shader skybox_shader{"skybox", "data/shaders"};

View File

@@ -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);
}

11
src/core/time.cpp Normal file
View File

@@ -0,0 +1,11 @@
#include "time.h"
#include <chrono>
void Time::update_delta_time(entt::registry& registry)
{
auto current_time = std::chrono::system_clock::now();
auto& delta_time = registry.ctx().emplace<Delta>();
delta_time.delta = current_time - delta_time.last_time;
delta_time.last_time = current_time;
}

View File

@@ -1,12 +1,16 @@
#pragma once
#include <chrono>
#include <entt/entt.hpp>
namespace Time {
struct Delta
{
std::chrono::duration<double> delta;
std::chrono::time_point<std::chrono::system_clock> last_time;
};
void update_delta_time(entt::registry& registry);
} // namespace Time