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);
}
init_window(&window, params, error_callback, key_callback);
window = init_window(params, error_callback, key_callback);
ShaderProgram program = init_program(fragment_shader);
+39 -15
View File
@@ -9,11 +9,7 @@
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>
// TODO split into smaller functions
// 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)) {
void init_glfw(void (*error_callback)(int, const char *)) {
// set errors handler
glfwSetErrorCallback(error_callback);
@@ -26,45 +22,73 @@ void *init_window(Window **window, Parameters params,
exit(EXIT_FAILURE);
}
// add context to window before creation
// Context related hints
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_DECORATED, 0);
}
GLFWmonitor *get_monitor(unsigned char screen_index) {
// detect monitors
int count;
GLFWmonitor **monitors = glfwGetMonitors(&count);
// check selected monitor availability
if (params.screen >= count) {
fprintf(stderr, "Screen %d is out of range [0-%d]\n", params.screen,
if (screen_index >= count) {
fprintf(stderr, "Screen %d is out of range [0-%d]\n", screen_index,
count - 1);
glfwTerminate();
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
(*window) = glfwCreateWindow(1, 1, PACKAGE " " VERSION,
monitors[params.screen], NULL);
GLFWwindow *window =
glfwCreateWindow(1, 1, PACKAGE " " VERSION, monitor, NULL);
// handle window creation fail
if (!(*window)) {
if (!window) {
fprintf(stderr, "[GLFW] Window or context creation failed\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
return window;
}
void use_window(GLFWwindow *window,
void (*key_callback)(Window *, int, int, int, int)) {
// use current window
glfwMakeContextCurrent((*window));
glfwMakeContextCurrent(window);
// link GLAD and GLFW window
gladLoadGL(glfwGetProcAddress);
// set keyboard handler
glfwSetKeyCallback((*window), key_callback);
glfwSetKeyCallback(window, key_callback);
// hide cursor
glfwSetInputMode((*window), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
// vsync
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;
}
+3 -3
View File
@@ -3,8 +3,8 @@
#ifndef WINDOW_H
#define WINDOW_H
void *init_window(Window **window, Parameters params,
void (*error_callback)(int, const char *),
void (*key_callback)(Window *, int, int, int, int));
Window *init_window(Parameters params,
void (*error_callback)(int, const char *),
void (*key_callback)(Window *, int, int, int, int));
#endif