clean window.c

This commit is contained in:
2025-09-13 16:10:05 +02:00
parent df96d9cdba
commit e0095e9910
3 changed files with 43 additions and 19 deletions
+1 -1
View File
@@ -62,7 +62,7 @@ void forge_run(Parameters params) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
init_window(&window, params, error_callback, key_callback); window = init_window(params, error_callback, key_callback);
ShaderProgram program = init_program(fragment_shader); ShaderProgram program = init_program(fragment_shader);
+39 -15
View File
@@ -9,11 +9,7 @@
#define GLAD_GL_IMPLEMENTATION #define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h> #include <glad/gl.h>
// TODO split into smaller functions void init_glfw(void (*error_callback)(int, const char *)) {
// TODO custom struct to remove glfw in signature
void *init_window(Window **window, Parameters params,
void (*error_callback)(int, const char *),
void (*key_callback)(Window *, int, int, int, int)) {
// set errors handler // set errors handler
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
@@ -26,45 +22,73 @@ void *init_window(Window **window, Parameters params,
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// add context to window before creation // Context related hints
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_DECORATED, 0); }
GLFWmonitor *get_monitor(unsigned char screen_index) {
// detect monitors // detect monitors
int count; int count;
GLFWmonitor **monitors = glfwGetMonitors(&count); GLFWmonitor **monitors = glfwGetMonitors(&count);
// check selected monitor availability // check selected monitor availability
if (params.screen >= count) { if (screen_index >= count) {
fprintf(stderr, "Screen %d is out of range [0-%d]\n", params.screen, fprintf(stderr, "Screen %d is out of range [0-%d]\n", screen_index,
count - 1); count - 1);
glfwTerminate(); glfwTerminate();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return monitors[screen_index];
}
GLFWwindow *create_window(GLFWmonitor *monitor) {
// Window related hints
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
// create fullscreen window in selected monitor // create fullscreen window in selected monitor
(*window) = glfwCreateWindow(1, 1, PACKAGE " " VERSION, GLFWwindow *window =
monitors[params.screen], NULL); glfwCreateWindow(1, 1, PACKAGE " " VERSION, monitor, NULL);
// handle window creation fail // handle window creation fail
if (!(*window)) { if (!window) {
fprintf(stderr, "[GLFW] Window or context creation failed\n"); fprintf(stderr, "[GLFW] Window or context creation failed\n");
glfwTerminate(); glfwTerminate();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
return window;
}
void use_window(GLFWwindow *window,
void (*key_callback)(Window *, int, int, int, int)) {
// use current window // use current window
glfwMakeContextCurrent((*window)); glfwMakeContextCurrent(window);
// link GLAD and GLFW window // link GLAD and GLFW window
gladLoadGL(glfwGetProcAddress); gladLoadGL(glfwGetProcAddress);
// set keyboard handler // set keyboard handler
glfwSetKeyCallback((*window), key_callback); glfwSetKeyCallback(window, key_callback);
// hide cursor // hide cursor
glfwSetInputMode((*window), GLFW_CURSOR, GLFW_CURSOR_HIDDEN); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
// vsync // vsync
glfwSwapInterval(1); glfwSwapInterval(1);
}
Window *init_window(Parameters params,
void (*error_callback)(int, const char *),
void (*key_callback)(Window *, int, int, int, int)) {
GLFWwindow *window;
GLFWmonitor *monitor;
init_glfw(error_callback);
monitor = get_monitor(params.screen);
window = create_window(monitor);
use_window(window, key_callback);
return window; return window;
} }
+3 -3
View File
@@ -3,8 +3,8 @@
#ifndef WINDOW_H #ifndef WINDOW_H
#define WINDOW_H #define WINDOW_H
void *init_window(Window **window, Parameters params, Window *init_window(Parameters params,
void (*error_callback)(int, const char *), void (*error_callback)(int, const char *),
void (*key_callback)(Window *, int, int, int, int)); void (*key_callback)(Window *, int, int, int, int));
#endif #endif