[nostalgia/core] Add focusWindow, cleanup GLFW leak into Userland, change shutdown to stop main loop

This commit is contained in:
Gary Talent 2021-07-23 21:52:26 -05:00
parent 7dfa2f5713
commit 32ac497a0e
8 changed files with 50 additions and 19 deletions

View File

@ -9,9 +9,6 @@
#pragma once #pragma once
#if __has_include(<imgui.h>) #if __has_include(<imgui.h>)
#define IMGUI_IMPL_OPENGL_ES3
#include <imgui_impl_opengl3.h>
#include <imgui_impl_glfw.h>
#endif #endif
#include <ox/std/types.hpp> #include <ox/std/types.hpp>

View File

@ -26,6 +26,7 @@ class Context {
ox::FileSystem *rom = nullptr; ox::FileSystem *rom = nullptr;
ox::Vector<Drawer*, 5> drawers; ox::Vector<Drawer*, 5> drawers;
private: private:
void *m_customData = nullptr;
void *m_windowerData = nullptr; void *m_windowerData = nullptr;
void *m_rendererData = nullptr; void *m_rendererData = nullptr;
@ -36,6 +37,16 @@ class Context {
Context(const Context &other) noexcept = delete; Context(const Context &other) noexcept = delete;
Context(const Context &&other) noexcept = delete; Context(const Context &&other) noexcept = delete;
constexpr void setCustomData(void *customData) noexcept {
m_customData = customData;
}
template<typename T>
[[nodiscard]]
constexpr T *customData() noexcept {
return static_cast<T*>(m_customData);
}
constexpr void setWindowerData(void *windowerData) noexcept { constexpr void setWindowerData(void *windowerData) noexcept {
m_windowerData = windowerData; m_windowerData = windowerData;
} }

View File

@ -33,4 +33,6 @@ void setEventHandler(Context *ctx, event_handler) noexcept;
[[nodiscard]] [[nodiscard]]
uint64_t ticksMs(Context *ctx) noexcept; uint64_t ticksMs(Context *ctx) noexcept;
void shutdown(Context *ctx) noexcept;
} }

View File

@ -103,6 +103,9 @@ ox::Error shutdownGfx(Context*) noexcept {
void setWindowTitle(Context*, const char*) noexcept { void setWindowTitle(Context*, const char*) noexcept {
} }
void focusWindow(Context*) noexcept {
}
int getScreenWidth(Context*) noexcept { int getScreenWidth(Context*) noexcept {
return 240; return 240;
} }

View File

@ -74,14 +74,14 @@ struct Sprite {
ox::Error initGfx(Context *ctx) noexcept; ox::Error initGfx(Context *ctx) noexcept;
ox::Error shutdownGfx(Context *ctx) noexcept;
void addCustomDrawer(Context *ctx, Drawer *cd) noexcept; void addCustomDrawer(Context *ctx, Drawer *cd) noexcept;
void removeCustomDrawer(Context *ctx, Drawer *cd) noexcept; void removeCustomDrawer(Context *ctx, Drawer *cd) noexcept;
void setWindowTitle(Context *ctx, const char *title) noexcept; void setWindowTitle(Context *ctx, const char *title) noexcept;
void focusWindow(Context *ctx) noexcept;
[[nodiscard]] [[nodiscard]]
int getScreenWidth(Context *ctx) noexcept; int getScreenWidth(Context *ctx) noexcept;

View File

@ -11,6 +11,7 @@
#include <nostalgia/core/config.hpp> #include <nostalgia/core/config.hpp>
#include <nostalgia/core/gfx.hpp> #include <nostalgia/core/gfx.hpp>
#include <nostalgia/core/input.hpp> #include <nostalgia/core/input.hpp>
#include <nostalgia/core/userland/gfx.hpp>
#include "core.hpp" #include "core.hpp"
@ -19,11 +20,10 @@ namespace nostalgia::core {
void draw(Context *ctx) noexcept; void draw(Context *ctx) noexcept;
static void handleKeyPress(Context *ctx, int key) { static void handleKeyPress(Context *ctx, int key) {
const auto id = ctx->windowerData<GlfwImplData>();
switch (key) { switch (key) {
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
case GLFW_KEY_Q: case GLFW_KEY_Q:
glfwSetWindowShouldClose(id->window, true); shutdown(ctx);
break; break;
default: default:
break; break;
@ -66,6 +66,11 @@ ox::Error run(Context *ctx) noexcept {
draw(ctx); draw(ctx);
glfwSwapBuffers(id->window); glfwSwapBuffers(id->window);
} }
// destroy GLFW window
oxReturnError(renderer::shutdown(ctx));
glfwDestroyWindow(id->window);
ctx->setWindowerData(nullptr);
delete id;
return OxError(0); return OxError(0);
} }
@ -85,4 +90,10 @@ bool buttonDown(Key) noexcept {
return false; return false;
} }
void shutdown(Context *ctx) noexcept {
const auto id = ctx->windowerData<GlfwImplData>();
glfwSetWindowShouldClose(id->window, true);
}
} }

View File

@ -7,6 +7,9 @@
*/ */
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#define IMGUI_IMPL_OPENGL_ES3
#include <imgui_impl_opengl3.h>
#include <imgui_impl_glfw.h>
#include <nostalgia/core/config.hpp> #include <nostalgia/core/config.hpp>
#include <nostalgia/core/userland/gfx.hpp> #include <nostalgia/core/userland/gfx.hpp>
@ -21,6 +24,10 @@ static void handleGlfwError(int err, const char *desc) {
oxErrf("GLFW error ({}): {}\n", err, desc); oxErrf("GLFW error ({}): {}\n", err, desc);
} }
void ImGui_Impl_NewFrame() noexcept {
ImGui_ImplGlfw_NewFrame();
}
ox::Error initGfx(Context *ctx) noexcept { ox::Error initGfx(Context *ctx) noexcept {
auto id = ctx->windowerData<GlfwImplData>(); auto id = ctx->windowerData<GlfwImplData>();
glfwSetErrorCallback(handleGlfwError); glfwSetErrorCallback(handleGlfwError);
@ -28,9 +35,9 @@ ox::Error initGfx(Context *ctx) noexcept {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if constexpr(ox::defines::OS == ox::defines::OS::Darwin) { 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); id->window = glfwCreateWindow(240 * Scale, 160 * Scale, "nostalgia", nullptr, nullptr);
if (id->window == nullptr) { if (id->window == nullptr) {
return OxError(1, "Could not open GLFW window"); return OxError(1, "Could not open GLFW window");
@ -48,20 +55,16 @@ ox::Error initGfx(Context *ctx) noexcept {
return renderer::init(ctx); return renderer::init(ctx);
} }
ox::Error shutdownGfx(Context *ctx) noexcept {
oxReturnError(renderer::shutdown(ctx));
auto id = ctx->windowerData<GlfwImplData>();
glfwDestroyWindow(id->window);
ctx->setWindowerData(nullptr);
delete id;
return OxError(0);
}
void setWindowTitle(Context *ctx, const char *title) noexcept { void setWindowTitle(Context *ctx, const char *title) noexcept {
const auto id = ctx->windowerData<GlfwImplData>(); const auto id = ctx->windowerData<GlfwImplData>();
glfwSetWindowTitle(id->window, title); glfwSetWindowTitle(id->window, title);
} }
void focusWindow(Context *ctx) noexcept {
const auto id = ctx->windowerData<GlfwImplData>();
glfwFocusWindow(id->window);
}
int getScreenWidth(Context *ctx) noexcept { int getScreenWidth(Context *ctx) noexcept {
auto id = ctx->windowerData<GlfwImplData>(); auto id = ctx->windowerData<GlfwImplData>();
int w = 0, h = 0; int w = 0, h = 0;

View File

@ -8,6 +8,9 @@
#include <array> #include <array>
#define IMGUI_IMPL_OPENGL_ES3
#include <imgui_impl_opengl3.h>
#include <nostalgia/glutils/glutils.hpp> #include <nostalgia/glutils/glutils.hpp>
#include <ox/std/bit.hpp> #include <ox/std/bit.hpp>
@ -18,6 +21,7 @@
namespace nostalgia::core { namespace nostalgia::core {
void ImGui_Impl_NewFrame() noexcept;
namespace renderer { namespace renderer {
@ -267,7 +271,7 @@ void draw(Context *ctx) noexcept {
const auto id = ctx->rendererData<renderer::GlImplData>(); const auto id = ctx->rendererData<renderer::GlImplData>();
renderer::tickFps(id); renderer::tickFps(id);
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame(); ImGui_Impl_NewFrame();
if constexpr(config::ImGuiEnabled) { if constexpr(config::ImGuiEnabled) {
ImGui::NewFrame(); ImGui::NewFrame();
} }