From 8c78ffdca52f9ae5a1545ad1f49d0687acfa351e Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 14 Apr 2025 16:20:45 +0200 Subject: [PATCH] implement input and rework window implementation moved the window variable as a constant in the window module, and implement more of GLFW logic, including some basic input handling. --- src/io/input.c | 13 +++++++++++++ src/io/input.h | 7 +++++++ src/io/window.c | 34 ++++++++++++++++++++++++++++------ src/io/window.h | 10 ++-------- src/main.c | 13 ++++++++----- 5 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 src/io/input.c create mode 100644 src/io/input.h diff --git a/src/io/input.c b/src/io/input.c new file mode 100644 index 0000000..a25ad00 --- /dev/null +++ b/src/io/input.c @@ -0,0 +1,13 @@ +// Copyright (c) 2025 Quinn +// Licensed under the MIT Licence. See LICENSE for details +#include "input.h" + +#include + +void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods) { + (void)win, (void)key, (void)scancode, (void)action, (void)mods; // make the compiler shut up as this is fine +#ifndef NDEBUG + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) + glfwSetWindowShouldClose(win, 1); +#endif +} diff --git a/src/io/input.h b/src/io/input.h new file mode 100644 index 0000000..a70cba6 --- /dev/null +++ b/src/io/input.h @@ -0,0 +1,7 @@ +// Copyright (c) 2025 Quinn +// Licensed under the MIT Licence. See LICENSE for details +#pragma once +#include + +void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods); + diff --git a/src/io/window.c b/src/io/window.c index 8e0dd89..011cd5b 100644 --- a/src/io/window.c +++ b/src/io/window.c @@ -3,21 +3,43 @@ #include "window.h" #include +#include #include +#include "input.h" // macros for ease of access #define WIN_NAME "MCA Selector Lite" #define WIN_DEFAULT_WIDTH 640 #define WIN_DEFAULT_HEIGHT 480 -int window_init(struct renderdat* restrict rdat) { - GLFWwindow* const win = glfwCreateWindow(WIN_DEFAULT_WIDTH, WIN_DEFAULT_HEIGHT, WIN_NAME, NULL, NULL); - if (win == NULL) return 1; - rdat->win = win; +static GLFWwindow* win = NULL; + +int window_init(void) { + // initialize the window + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // TODO: read about opengl profiles + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); + win = glfwCreateWindow(WIN_DEFAULT_WIDTH, WIN_DEFAULT_HEIGHT, WIN_NAME, NULL, NULL); + if (!win) return 1; + + // make the current window the current context + glfwMakeContextCurrent(win); + + // configure callbacks + glfwSetKeyCallback(win, key_callback); + glfwSwapInterval(1); // wait 1 screen update for a redraw a.k.a. "vsync". (not really applicable in this case but eh) return 0; } -void window_free(struct renderdat* restrict rdat) { - glfwDestroyWindow(rdat->win); +void window_loop(void) { + assert(win != NULL); + + while (!glfwWindowShouldClose(win)) { + glfwWaitEvents(); // wait till an update has been given + } + + glfwDestroyWindow(win); } diff --git a/src/io/window.h b/src/io/window.h index 3bf5363..afa33d8 100644 --- a/src/io/window.h +++ b/src/io/window.h @@ -2,11 +2,5 @@ // Licensed under the MIT Licence. See LICENSE for details #pragma once -#include - -struct renderdat { - GLFWwindow* win; -}; - -int window_init(struct renderdat* restrict rdat); -void window_free(struct renderdat* restrict rdat); +int window_init(void); // initializes the global window, returns non-zero upon failure +void window_loop(void); // performs the window updates diff --git a/src/main.c b/src/main.c index bff7628..82812ec 100644 --- a/src/main.c +++ b/src/main.c @@ -1,17 +1,20 @@ // Copyright (c) 2025 Quinn // Licensed under the MIT Licence. See LICENSE for details -#include "GLFW/glfw3.h" +#include + #include "error.h" #include "io/window.h" -struct renderdat rdat; // contains the relevant data needed for rendering, contains rubbish data until init was +#define WIN_NAME "MCA Selector Lite" +#define WIN_DEFAULT_WIDTH 640 +#define WIN_DEFAULT_HEIGHT 480 static inline int init(void) { glfwSetErrorCallback(error_callback); glfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE); // disable joystick buttons - if (!glfwInit()) return 1; // initialize GLFW - if (window_init(&rdat)) return 1; // initialize the rendering and create a window + if (!glfwInit()) return 1; // initialize GLFW + if (window_init()) return 1; return 0; } @@ -23,6 +26,6 @@ static inline void quit(void) { int main(int argc, char** argv) { (void)argc, (void)argv; if (init()) fatal("failed to initialize!"); - while (1); + window_loop(); quit(); }