From 18109f696de9cae4d3fb887356968640307d4192 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 6 May 2025 23:56:49 -0500 Subject: [PATCH] [turbine/glfw] Treat close window event like other events with regard to a mandatory refresh period --- src/olympic/turbine/src/glfw/gfx.cpp | 9 +++++++++ src/olympic/turbine/src/glfw/turbine.cpp | 8 +------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/olympic/turbine/src/glfw/gfx.cpp b/src/olympic/turbine/src/glfw/gfx.cpp index 0e1177bb..d47b2c54 100644 --- a/src/olympic/turbine/src/glfw/gfx.cpp +++ b/src/olympic/turbine/src/glfw/gfx.cpp @@ -93,6 +93,14 @@ static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int } } +static void handleGlfwWindowCloseEvent(GLFWwindow *window) noexcept { + auto &ctx = *static_cast(glfwGetWindowUserPointer(window)); + ctx.mandatoryRefreshPeriodEnd = ticksMs(ctx) + config::MandatoryRefreshPeriod; + ctx.running = !ctx.shutdownHandler(ctx); + glfwSetWindowShouldClose(window, !ctx.running); + glfwPostEmptyEvent(); +} + #if TURBINE_USE_IMGUI static void themeImgui() noexcept { // Dark Ruda style by Raikiri from ImThemes @@ -210,6 +218,7 @@ ox::Error initGfx(Context &ctx) noexcept { glfwSetCursorPosCallback(ctx.window, handleGlfwCursorPosEvent); glfwSetMouseButtonCallback(ctx.window, handleGlfwMouseButtonEvent); glfwSetKeyCallback(ctx.window, handleGlfwKeyEvent); + glfwSetWindowCloseCallback(ctx.window, handleGlfwWindowCloseEvent); glfwSetWindowUserPointer(ctx.window, &ctx); glfwMakeContextCurrent(ctx.window); if (!gladLoadGLES2Loader(reinterpret_cast(glfwGetProcAddress))) { diff --git a/src/olympic/turbine/src/glfw/turbine.cpp b/src/olympic/turbine/src/glfw/turbine.cpp index f281dc7a..24ca1644 100644 --- a/src/olympic/turbine/src/glfw/turbine.cpp +++ b/src/olympic/turbine/src/glfw/turbine.cpp @@ -92,7 +92,7 @@ ox::Error run(Context &ctx) noexcept { ctx.wakeupTime = ticks + static_cast(st); sleepTime = static_cast(st); } else { - ctx.wakeupTime = ~uint64_t(0); + ctx.wakeupTime = ~uint64_t{0}; sleepTime = ctx.wakeupTime - ticks; } } else { @@ -104,12 +104,6 @@ ox::Error run(Context &ctx) noexcept { if (realSleepTime && ctx.mandatoryRefreshPeriodEnd <= ticks) { glfwWaitEventsTimeout(static_cast(realSleepTime) / 1000); } - if (glfwWindowShouldClose(ctx.window)) { - if (ctx.shutdownHandler(ctx)) { - break; - } - glfwSetWindowShouldClose(ctx.window, false); - } } shutdown(ctx); return {};