From 32ac497a0e97cf9a3fcc84c6e5267b305c074b27 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 23 Jul 2021 21:52:26 -0500 Subject: [PATCH] [nostalgia/core] Add focusWindow, cleanup GLFW leak into Userland, change shutdown to stop main loop --- src/nostalgia/core/config.hpp | 3 --- src/nostalgia/core/context.hpp | 11 ++++++++++ src/nostalgia/core/core.hpp | 2 ++ src/nostalgia/core/gba/gfx.cpp | 3 +++ src/nostalgia/core/gfx.hpp | 4 ++-- src/nostalgia/core/glfw/core.cpp | 15 +++++++++++-- src/nostalgia/core/glfw/gfx.cpp | 25 ++++++++++++---------- src/nostalgia/core/userland/gfx_opengl.cpp | 6 +++++- 8 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/nostalgia/core/config.hpp b/src/nostalgia/core/config.hpp index b19fc472..b519941f 100644 --- a/src/nostalgia/core/config.hpp +++ b/src/nostalgia/core/config.hpp @@ -9,9 +9,6 @@ #pragma once #if __has_include() -#define IMGUI_IMPL_OPENGL_ES3 -#include -#include #endif #include diff --git a/src/nostalgia/core/context.hpp b/src/nostalgia/core/context.hpp index 10cf3843..366cf7f5 100644 --- a/src/nostalgia/core/context.hpp +++ b/src/nostalgia/core/context.hpp @@ -26,6 +26,7 @@ class Context { ox::FileSystem *rom = nullptr; ox::Vector drawers; private: + void *m_customData = nullptr; void *m_windowerData = nullptr; void *m_rendererData = nullptr; @@ -36,6 +37,16 @@ class Context { Context(const Context &other) noexcept = delete; Context(const Context &&other) noexcept = delete; + constexpr void setCustomData(void *customData) noexcept { + m_customData = customData; + } + + template + [[nodiscard]] + constexpr T *customData() noexcept { + return static_cast(m_customData); + } + constexpr void setWindowerData(void *windowerData) noexcept { m_windowerData = windowerData; } diff --git a/src/nostalgia/core/core.hpp b/src/nostalgia/core/core.hpp index c861ca76..41902de0 100644 --- a/src/nostalgia/core/core.hpp +++ b/src/nostalgia/core/core.hpp @@ -33,4 +33,6 @@ void setEventHandler(Context *ctx, event_handler) noexcept; [[nodiscard]] uint64_t ticksMs(Context *ctx) noexcept; +void shutdown(Context *ctx) noexcept; + } diff --git a/src/nostalgia/core/gba/gfx.cpp b/src/nostalgia/core/gba/gfx.cpp index caf046a3..f66d64ef 100644 --- a/src/nostalgia/core/gba/gfx.cpp +++ b/src/nostalgia/core/gba/gfx.cpp @@ -103,6 +103,9 @@ ox::Error shutdownGfx(Context*) noexcept { void setWindowTitle(Context*, const char*) noexcept { } +void focusWindow(Context*) noexcept { +} + int getScreenWidth(Context*) noexcept { return 240; } diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index dc91f745..e8b47e67 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -74,14 +74,14 @@ struct Sprite { ox::Error initGfx(Context *ctx) noexcept; -ox::Error shutdownGfx(Context *ctx) noexcept; - void addCustomDrawer(Context *ctx, Drawer *cd) noexcept; void removeCustomDrawer(Context *ctx, Drawer *cd) noexcept; void setWindowTitle(Context *ctx, const char *title) noexcept; +void focusWindow(Context *ctx) noexcept; + [[nodiscard]] int getScreenWidth(Context *ctx) noexcept; diff --git a/src/nostalgia/core/glfw/core.cpp b/src/nostalgia/core/glfw/core.cpp index fa3a0393..db6af7f2 100644 --- a/src/nostalgia/core/glfw/core.cpp +++ b/src/nostalgia/core/glfw/core.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "core.hpp" @@ -19,11 +20,10 @@ namespace nostalgia::core { void draw(Context *ctx) noexcept; static void handleKeyPress(Context *ctx, int key) { - const auto id = ctx->windowerData(); switch (key) { case GLFW_KEY_ESCAPE: case GLFW_KEY_Q: - glfwSetWindowShouldClose(id->window, true); + shutdown(ctx); break; default: break; @@ -66,6 +66,11 @@ ox::Error run(Context *ctx) noexcept { draw(ctx); glfwSwapBuffers(id->window); } + // destroy GLFW window + oxReturnError(renderer::shutdown(ctx)); + glfwDestroyWindow(id->window); + ctx->setWindowerData(nullptr); + delete id; return OxError(0); } @@ -85,4 +90,10 @@ bool buttonDown(Key) noexcept { return false; } +void shutdown(Context *ctx) noexcept { + const auto id = ctx->windowerData(); + glfwSetWindowShouldClose(id->window, true); +} + + } diff --git a/src/nostalgia/core/glfw/gfx.cpp b/src/nostalgia/core/glfw/gfx.cpp index 6c1cbb62..9282995d 100644 --- a/src/nostalgia/core/glfw/gfx.cpp +++ b/src/nostalgia/core/glfw/gfx.cpp @@ -7,6 +7,9 @@ */ #include +#define IMGUI_IMPL_OPENGL_ES3 +#include +#include #include #include @@ -21,6 +24,10 @@ static void handleGlfwError(int err, const char *desc) { oxErrf("GLFW error ({}): {}\n", err, desc); } +void ImGui_Impl_NewFrame() noexcept { + ImGui_ImplGlfw_NewFrame(); +} + ox::Error initGfx(Context *ctx) noexcept { auto id = ctx->windowerData(); glfwSetErrorCallback(handleGlfwError); @@ -28,9 +35,9 @@ ox::Error initGfx(Context *ctx) noexcept { glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); if constexpr(ox::defines::OS == ox::defines::OS::Darwin) { - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); } - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); id->window = glfwCreateWindow(240 * Scale, 160 * Scale, "nostalgia", nullptr, nullptr); if (id->window == nullptr) { return OxError(1, "Could not open GLFW window"); @@ -48,20 +55,16 @@ ox::Error initGfx(Context *ctx) noexcept { return renderer::init(ctx); } -ox::Error shutdownGfx(Context *ctx) noexcept { - oxReturnError(renderer::shutdown(ctx)); - auto id = ctx->windowerData(); - glfwDestroyWindow(id->window); - ctx->setWindowerData(nullptr); - delete id; - return OxError(0); -} - void setWindowTitle(Context *ctx, const char *title) noexcept { const auto id = ctx->windowerData(); glfwSetWindowTitle(id->window, title); } +void focusWindow(Context *ctx) noexcept { + const auto id = ctx->windowerData(); + glfwFocusWindow(id->window); +} + int getScreenWidth(Context *ctx) noexcept { auto id = ctx->windowerData(); int w = 0, h = 0; diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp index 7ff13d0d..10ed56e1 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -8,6 +8,9 @@ #include +#define IMGUI_IMPL_OPENGL_ES3 +#include + #include #include @@ -18,6 +21,7 @@ namespace nostalgia::core { +void ImGui_Impl_NewFrame() noexcept; namespace renderer { @@ -267,7 +271,7 @@ void draw(Context *ctx) noexcept { const auto id = ctx->rendererData(); renderer::tickFps(id); ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); + ImGui_Impl_NewFrame(); if constexpr(config::ImGuiEnabled) { ImGui::NewFrame(); }