From 827ba7dcdb47ead2651016774060084a446931ff Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 15 May 2021 01:39:41 -0500 Subject: [PATCH] [nostalgia/core/glfw] Move wakeup time to Context --- src/nostalgia/core/glfw/core.cpp | 38 ++++++++++++++------------------ src/nostalgia/core/glfw/core.hpp | 1 + 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/nostalgia/core/glfw/core.cpp b/src/nostalgia/core/glfw/core.cpp index 5e1ebea5..90508749 100644 --- a/src/nostalgia/core/glfw/core.cpp +++ b/src/nostalgia/core/glfw/core.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include @@ -17,23 +16,10 @@ namespace nostalgia::core { -static event_handler g_eventHandler = nullptr; -static uint64_t g_wakeupTime; - -void draw(Context *ctx); - -ox::Error init(Context *ctx) noexcept { - const auto id = new GlfwImplData; - ctx->setWindowerData(id); - using namespace std::chrono; - id->startTime = duration_cast(system_clock::now().time_since_epoch()).count(); - glfwInit(); - oxReturnError(initGfx(ctx)); - return OxError(0); -} +void draw(Context *ctx) noexcept; static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int) { - const auto ctx = ox::bit_cast(glfwGetWindowUserPointer(window)); + const auto ctx = static_cast(glfwGetWindowUserPointer(window)); const auto id = ctx->windowerData(); switch (action) { case GLFW_PRESS: @@ -46,6 +32,17 @@ static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int } } +ox::Error init(Context *ctx) noexcept { + const auto id = new GlfwImplData; + ctx->setWindowerData(id); + using namespace std::chrono; + id->startTime = duration_cast(system_clock::now().time_since_epoch()).count(); + glfwInit(); + oxReturnError(initGfx(ctx)); + glfwSetKeyCallback(id->window, handleGlfwKeyEvent); + return OxError(0); +} + ox::Error run(Context *ctx) noexcept { const auto id = ctx->windowerData(); id->running = true; @@ -54,16 +51,15 @@ ox::Error run(Context *ctx) noexcept { // oxTrace("nostalgia::core::sdl", "Could not enable adaptive vsync, falling back on vsync"); // SDL_GL_SetSwapInterval(1); // fallback on normal vsync //} - glfwSetKeyCallback(id->window, handleGlfwKeyEvent); while (id->running && !glfwWindowShouldClose(id->window)) { glfwPollEvents(); const auto ticks = ticksMs(ctx); - if (g_wakeupTime <= ticks && g_eventHandler) { - auto sleepTime = g_eventHandler(ctx); + if (id->wakeupTime <= ticks && id->eventHandler) { + auto sleepTime = id->eventHandler(ctx); if (sleepTime >= 0) { - g_wakeupTime = ticks + static_cast(sleepTime); + id->wakeupTime = ticks + static_cast(sleepTime); } else { - g_wakeupTime = ~uint64_t(0); + id->wakeupTime = ~uint64_t(0); } } draw(ctx); diff --git a/src/nostalgia/core/glfw/core.hpp b/src/nostalgia/core/glfw/core.hpp index 2226e46e..9c79e230 100644 --- a/src/nostalgia/core/glfw/core.hpp +++ b/src/nostalgia/core/glfw/core.hpp @@ -17,6 +17,7 @@ struct GlfwImplData { int64_t startTime = 0; bool running = false; event_handler eventHandler = nullptr; + uint64_t wakeupTime = 0; }; }