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 <GLFW/glfw3.h>
#include <assert.h>
#include <stdlib.h>
#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);
}

View File

@@ -2,11 +2,5 @@
// Licensed under the MIT Licence. See LICENSE for details
#pragma once
#include <GLFW/glfw3.h>
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