From 22f8504e5a37c5841da32141d6e2032383ebf1f0 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 16 Jul 2023 14:16:52 -0500 Subject: [PATCH] [turbine] Make window render during resizing --- src/turbine/context.hpp | 2 +- src/turbine/glfw/gfx.cpp | 7 +------ src/turbine/glfw/turbine.cpp | 40 ++++++++++++++++++++++-------------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/turbine/context.hpp b/src/turbine/context.hpp index 9e0d7167..48d4a219 100644 --- a/src/turbine/context.hpp +++ b/src/turbine/context.hpp @@ -59,11 +59,11 @@ class Context { Context() noexcept = default; + public: Context(Context &other) noexcept = delete; Context(const Context &other) noexcept = delete; Context(const Context &&other) noexcept = delete; - public: virtual inline ~Context() noexcept { shutdown(*this); } diff --git a/src/turbine/glfw/gfx.cpp b/src/turbine/glfw/gfx.cpp index d589c37c..b2451a37 100644 --- a/src/turbine/glfw/gfx.cpp +++ b/src/turbine/glfw/gfx.cpp @@ -34,8 +34,6 @@ void removeDrawer(Context &ctx, Drawer *cd) noexcept { } -constexpr auto Scale = 5; - static void handleGlfwError(int err, const char *desc) noexcept { oxErrf("GLFW error ({}): {}\n", err, desc); } @@ -93,9 +91,6 @@ static void handleGlfwMouseButtonEvent(GLFWwindow *window, int, int, int) noexce ctx->uninterruptedRefreshes = 25; } -static void handleGlfwWindowResize(GLFWwindow*, int, int) noexcept { -} - static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int) noexcept { const auto ctx = static_cast(glfwGetWindowUserPointer(window)); ctx->uninterruptedRefreshes = 25; @@ -216,13 +211,13 @@ ox::Error initGfx(Context &ctx) noexcept { glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); auto cstr = ox_malloca(ctx.keelCtx.appName.bytes() + 1, char); ox_strncpy(cstr.get(), ctx.keelCtx.appName.data(), ctx.keelCtx.appName.bytes()); + constexpr auto Scale = 5; gctx.window = glfwCreateWindow(240 * Scale, 160 * Scale, cstr, nullptr, nullptr); if (gctx.window == nullptr) { return OxError(1, "Could not open GLFW window"); } glfwSetCursorPosCallback(gctx.window, handleGlfwCursorPosEvent); glfwSetMouseButtonCallback(gctx.window, handleGlfwMouseButtonEvent); - glfwSetWindowSizeCallback(gctx.window, handleGlfwWindowResize); glfwSetKeyCallback(gctx.window, handleGlfwKeyEvent); glfwSetWindowUserPointer(gctx.window, &ctx); glfwMakeContextCurrent(gctx.window); diff --git a/src/turbine/glfw/turbine.cpp b/src/turbine/glfw/turbine.cpp index 54e2e052..42ee93a5 100644 --- a/src/turbine/glfw/turbine.cpp +++ b/src/turbine/glfw/turbine.cpp @@ -14,6 +14,29 @@ namespace turbine { +static void draw(GlfwContext &gctx) noexcept { + // draw start + if constexpr(turbine::config::ImGuiEnabled) { + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + } + for (auto d : gctx.drawers) { + d->draw(gctx); + } + if constexpr(turbine::config::ImGuiEnabled) { + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + } + // draw end + glfwSwapBuffers(gctx.window); +} + +static void draw(GLFWwindow *window, int, int) noexcept { + auto &gctx = *static_cast(glfwGetWindowUserPointer(window)); + draw(gctx); +} + ox::Result> init(ox::UPtr fs, ox::CRStringView appName) noexcept { auto ctx = ox::make_unique(); oxReturnError(keel::init(&ctx->keelCtx, std::move(fs), appName)); @@ -21,6 +44,7 @@ ox::Result> init(ox::UPtr fs, ox::CRStringView ctx->startTime = duration_cast(system_clock::now().time_since_epoch()).count(); glfwInit(); oxReturnError(initGfx(*ctx)); + glfwSetWindowSizeCallback(ctx->window, draw); return ox::UPtr(ctx.release()); } @@ -55,21 +79,7 @@ ox::Error run(Context &ctx) noexcept { sleepTime = 10; } tickFps(gctx, ticks); - // draw start - if constexpr(turbine::config::ImGuiEnabled) { - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); - ImGui::NewFrame(); - } - for (auto d : gctx.drawers) { - d->draw(gctx); - } - if constexpr(turbine::config::ImGuiEnabled) { - ImGui::Render(); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - } - // draw end - glfwSwapBuffers(gctx.window); + draw(gctx); if (!gctx.constantRefresh) { if (gctx.uninterruptedRefreshes) { --gctx.uninterruptedRefreshes;