From 0d76129a0eb680f0a6e79b1525703013357a21b1 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 1 Feb 2022 02:15:49 -0600 Subject: [PATCH] [nostalgia/core/glfw] Fix keyboard support for ImGui --- src/nostalgia/core/glfw/core.cpp | 19 ------------------- src/nostalgia/core/glfw/gfx.cpp | 21 ++++++++++++++++++++- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/nostalgia/core/glfw/core.cpp b/src/nostalgia/core/glfw/core.cpp index cdfe9d1f..952b1652 100644 --- a/src/nostalgia/core/glfw/core.cpp +++ b/src/nostalgia/core/glfw/core.cpp @@ -15,24 +15,6 @@ namespace nostalgia::core { void draw(Context *ctx) noexcept; -static void handleKeyPress(Context *ctx, int key) { - switch (key) { - case GLFW_KEY_ESCAPE: - case GLFW_KEY_Q: - shutdown(ctx); - break; - default: - break; - } -} - -static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int) { - const auto ctx = static_cast(glfwGetWindowUserPointer(window)); - if (action == GLFW_PRESS) { - handleKeyPress(ctx, key); - } -} - ox::Result> init(ox::UniquePtr fs, const char *appName) noexcept { auto ctx = ox::make_unique(); ctx->rom = std::move(fs); @@ -43,7 +25,6 @@ ox::Result> init(ox::UniquePtr fs, const id->startTime = duration_cast(system_clock::now().time_since_epoch()).count(); glfwInit(); oxReturnError(initGfx(ctx.get())); - glfwSetKeyCallback(id->window, handleGlfwKeyEvent); return ctx; } diff --git a/src/nostalgia/core/glfw/gfx.cpp b/src/nostalgia/core/glfw/gfx.cpp index 6f8c4aee..aa6aa189 100644 --- a/src/nostalgia/core/glfw/gfx.cpp +++ b/src/nostalgia/core/glfw/gfx.cpp @@ -15,7 +15,7 @@ namespace nostalgia::core { constexpr auto Scale = 5; -static void handleGlfwError(int err, const char *desc) { +static void handleGlfwError(int err, const char *desc) noexcept { oxErrf("GLFW error ({}): {}\n", err, desc); } @@ -23,6 +23,24 @@ void ImGui_Impl_NewFrame() noexcept { ImGui_ImplGlfw_NewFrame(); } +static void handleKeyPress(Context *ctx, int key) noexcept { + switch (key) { + case GLFW_KEY_ESCAPE: + case GLFW_KEY_Q: + shutdown(ctx); + break; + default: + break; + } +} + +static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int) noexcept { + const auto ctx = static_cast(glfwGetWindowUserPointer(window)); + if (action == GLFW_PRESS) { + handleKeyPress(ctx, key); + } +} + ox::Error initGfx(Context *ctx) noexcept { auto id = ctx->windowerData(); glfwSetErrorCallback(handleGlfwError); @@ -37,6 +55,7 @@ ox::Error initGfx(Context *ctx) noexcept { if (id->window == nullptr) { return OxError(1, "Could not open GLFW window"); } + glfwSetKeyCallback(id->window, handleGlfwKeyEvent); glfwSetWindowUserPointer(id->window, ctx); glfwMakeContextCurrent(id->window); if constexpr(config::ImGuiEnabled) {