diff --git a/src/forge.c b/src/forge.c index fe58111..d598964 100644 --- a/src/forge.c +++ b/src/forge.c @@ -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); diff --git a/src/window.c b/src/window.c index f9da60d..c76498d 100644 --- a/src/window.c +++ b/src/window.c @@ -9,11 +9,7 @@ #define GLAD_GL_IMPLEMENTATION #include -// 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; } \ No newline at end of file diff --git a/src/window.h b/src/window.h index 98ce7f8..2a73274 100644 --- a/src/window.h +++ b/src/window.h @@ -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 \ No newline at end of file