From f6d2d96ff92c01c1f29edb190cc2daaae7d85405 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 5 Nov 2023 15:42:49 -0600 Subject: [PATCH] [turbine] Cleanup --- src/turbine/glfw/gfx.cpp | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/turbine/glfw/gfx.cpp b/src/turbine/glfw/gfx.cpp index b2451a37..12eda172 100644 --- a/src/turbine/glfw/gfx.cpp +++ b/src/turbine/glfw/gfx.cpp @@ -15,15 +15,24 @@ namespace turbine { +[[nodiscard]] +inline GlfwContext &glctx(Context &ctx) noexcept { + if constexpr(ox::defines::Debug) { + return dynamic_cast(ctx); + } else { + return static_cast(ctx); + } +} + namespace gl { void addDrawer(Context &ctx, Drawer *cd) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); gctx.drawers.emplace_back(cd); } void removeDrawer(Context &ctx, Drawer *cd) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); for (auto i = 0u; i < gctx.drawers.size(); ++i) { if (gctx.drawers[i] == cd) { oxIgnoreError(gctx.drawers.erase(i)); @@ -65,7 +74,7 @@ static void handleKeyPress(Context *ctx, int key, bool down) noexcept { return map; }(); const auto eventHandler = keyEventHandler(*ctx); - auto &gctx = static_cast(*ctx); + auto &gctx = glctx(*ctx); const auto k = keyMap[static_cast(key)]; setKeyDownStatus(&gctx, k, down); if (eventHandler) { @@ -200,7 +209,7 @@ static void themeImgui() noexcept { } ox::Error initGfx(Context &ctx) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); glfwSetErrorCallback(handleGlfwError); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); @@ -238,40 +247,40 @@ ox::Error initGfx(Context &ctx) noexcept { } void setWindowTitle(Context &ctx, ox::CRStringView title) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); auto cstr = ox_malloca(title.bytes() + 1, char); ox_strncpy(cstr.get(), title.data(), title.bytes()); glfwSetWindowTitle(gctx.window, cstr); } void focusWindow(Context &ctx) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); glfwFocusWindow(gctx.window); } int getScreenWidth(Context &ctx) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); int w = 0, h = 0; glfwGetFramebufferSize(gctx.window, &w, &h); return w; } int getScreenHeight(Context &ctx) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); int w = 0, h = 0; glfwGetFramebufferSize(gctx.window, &w, &h); return h; } ox::Size getScreenSize(Context &ctx) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); int w = 0, h = 0; glfwGetFramebufferSize(gctx.window, &w, &h); return {w, h}; } ox::Bounds getWindowBounds(Context &ctx) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); ox::Bounds bnds; glfwGetWindowPos(gctx.window, &bnds.x, &bnds.y); glfwGetWindowSize(gctx.window, &bnds.width, &bnds.height); @@ -279,14 +288,14 @@ ox::Bounds getWindowBounds(Context &ctx) noexcept { } ox::Error setWindowBounds(Context &ctx, const ox::Bounds &bnds) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); glfwSetWindowPos(gctx.window, bnds.x, bnds.y); glfwSetWindowSize(gctx.window, bnds.width, bnds.height); return {}; } void setConstantRefresh(Context &ctx, bool r) noexcept { - auto &gctx = static_cast(ctx); + auto &gctx = glctx(ctx); gctx.constantRefresh = r; }