[nostalgia/core] Add focusWindow, cleanup GLFW leak into Userland, change shutdown to stop main loop
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include <nostalgia/core/config.hpp>
|
||||
#include <nostalgia/core/gfx.hpp>
|
||||
#include <nostalgia/core/input.hpp>
|
||||
#include <nostalgia/core/userland/gfx.hpp>
|
||||
|
||||
#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<GlfwImplData>();
|
||||
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<GlfwImplData>();
|
||||
glfwSetWindowShouldClose(id->window, true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
*/
|
||||
|
||||
#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/userland/gfx.hpp>
|
||||
@@ -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<GlfwImplData>();
|
||||
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<GlfwImplData>();
|
||||
glfwDestroyWindow(id->window);
|
||||
ctx->setWindowerData(nullptr);
|
||||
delete id;
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
void setWindowTitle(Context *ctx, const char *title) noexcept {
|
||||
const auto id = ctx->windowerData<GlfwImplData>();
|
||||
glfwSetWindowTitle(id->window, title);
|
||||
}
|
||||
|
||||
void focusWindow(Context *ctx) noexcept {
|
||||
const auto id = ctx->windowerData<GlfwImplData>();
|
||||
glfwFocusWindow(id->window);
|
||||
}
|
||||
|
||||
int getScreenWidth(Context *ctx) noexcept {
|
||||
auto id = ctx->windowerData<GlfwImplData>();
|
||||
int w = 0, h = 0;
|
||||
|
||||
Reference in New Issue
Block a user