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.
This commit is contained in:
Quinn
2025-04-14 16:20:45 +02:00
committed by Quinn
parent 9ade61e006
commit 8c78ffdca5
5 changed files with 58 additions and 19 deletions

13
src/io/input.c Normal file
View File

@@ -0,0 +1,13 @@
// Copyright (c) 2025 Quinn
// Licensed under the MIT Licence. See LICENSE for details
#include "input.h"
#include <GLFW/glfw3.h>
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
}

7
src/io/input.h Normal file
View File

@@ -0,0 +1,7 @@
// Copyright (c) 2025 Quinn
// Licensed under the MIT Licence. See LICENSE for details
#pragma once
#include <GLFW/glfw3.h>
void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods);

View File

@@ -3,21 +3,43 @@
#include "window.h" #include "window.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include "input.h"
// macros for ease of access // macros for ease of access
#define WIN_NAME "MCA Selector Lite" #define WIN_NAME "MCA Selector Lite"
#define WIN_DEFAULT_WIDTH 640 #define WIN_DEFAULT_WIDTH 640
#define WIN_DEFAULT_HEIGHT 480 #define WIN_DEFAULT_HEIGHT 480
int window_init(struct renderdat* restrict rdat) { static GLFWwindow* win = NULL;
GLFWwindow* const win = glfwCreateWindow(WIN_DEFAULT_WIDTH, WIN_DEFAULT_HEIGHT, WIN_NAME, NULL, NULL);
if (win == NULL) return 1; int window_init(void) {
rdat->win = win; // 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; return 0;
} }
void window_free(struct renderdat* restrict rdat) { void window_loop(void) {
glfwDestroyWindow(rdat->win); assert(win != NULL);
while (!glfwWindowShouldClose(win)) {
glfwWaitEvents(); // wait till an update has been given
}
glfwDestroyWindow(win);
} }

View File

@@ -2,11 +2,5 @@
// Licensed under the MIT Licence. See LICENSE for details // Licensed under the MIT Licence. See LICENSE for details
#pragma once #pragma once
#include <GLFW/glfw3.h> int window_init(void); // initializes the global window, returns non-zero upon failure
void window_loop(void); // performs the window updates
struct renderdat {
GLFWwindow* win;
};
int window_init(struct renderdat* restrict rdat);
void window_free(struct renderdat* restrict rdat);

View File

@@ -1,17 +1,20 @@
// Copyright (c) 2025 Quinn // Copyright (c) 2025 Quinn
// Licensed under the MIT Licence. See LICENSE for details // Licensed under the MIT Licence. See LICENSE for details
#include "GLFW/glfw3.h" #include <GLFW/glfw3.h>
#include "error.h" #include "error.h"
#include "io/window.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) { static inline int init(void) {
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
glfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE); // disable joystick buttons glfwInitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE); // disable joystick buttons
if (!glfwInit()) return 1; // initialize GLFW if (!glfwInit()) return 1; // initialize GLFW
if (window_init(&rdat)) return 1; // initialize the rendering and create a window if (window_init()) return 1;
return 0; return 0;
} }
@@ -23,6 +26,6 @@ static inline void quit(void) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
(void)argc, (void)argv; (void)argc, (void)argv;
if (init()) fatal("failed to initialize!"); if (init()) fatal("failed to initialize!");
while (1); window_loop();
quit(); quit();
} }