From 05b0361c69033a85125a04304cc115fd2fc364fb Mon Sep 17 00:00:00 2001 From: Warwick Date: Wed, 1 Oct 2025 13:55:45 +0100 Subject: [PATCH] First pass of window manager file --- src/main.c | 21 ++++++++------------- src/window.c | 6 ++++++ src/window.h | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index fba03e3..a0dfc14 100644 --- a/src/main.c +++ b/src/main.c @@ -14,8 +14,8 @@ const unsigned int SCR_HEIGHT = 600; int main(int argc, char *argv[]) { - wn_window wn_window = {0}; - if (!wn_window_init(&wn_window)) { + wn_window window = {0}; + if (!wn_window_init(&window)) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize wn_window"); } @@ -111,21 +111,16 @@ int main(int argc, char *argv[]) { // Initialise camera Camera camera = Camera_default; - // Lock and hide mouse - SDL_SetWindowRelativeMouseMode(wn_window.window, true); - SDL_GetRelativeMouseState(NULL, NULL); - bool wants_running = true; while (wants_running) { - - // handle input events - while (SDL_PollEvent(&wn_window.event)) { - switch (wn_window.event.type) { + // TODO: Create event handler files + while (SDL_PollEvent(&window.event)) { + switch (window.event.type) { case SDL_EVENT_QUIT: wants_running = false; break; case SDL_EVENT_KEY_DOWN: - if (wn_window.event.key.key == SDLK_ESCAPE) { + if (window.event.key.key == SDLK_ESCAPE) { wants_running = false; } break; @@ -155,7 +150,7 @@ int main(int argc, char *argv[]) { glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // glBindVertexArray(0); // no need to unbind it every time - SDL_GL_SwapWindow(wn_window.window); + wn_swapwindow(&window); } glDeleteVertexArrays(1, &VAO); @@ -163,7 +158,7 @@ int main(int argc, char *argv[]) { glDeleteBuffers(1, &EBO); glDeleteProgram(shaderProgram); - wn_window_deinit(&wn_window); + wn_window_deinit(&window); return EXIT_SUCCESS; } diff --git a/src/window.c b/src/window.c index 717a835..4de00a3 100644 --- a/src/window.c +++ b/src/window.c @@ -43,6 +43,10 @@ bool wn_window_init(wn_window *window) { "Couldn't make glcontext current: %s", SDL_GetError()); } + // hide and lock mouse + SDL_SetWindowRelativeMouseMode(window->window, true); + SDL_GetRelativeMouseState(NULL, NULL); + return true; } @@ -51,3 +55,5 @@ void wn_window_deinit(wn_window *window) { SDL_DestroyWindow(window->window); SDL_Quit(); } + +void wn_swapwindow(wn_window *window) { SDL_GL_SwapWindow(window->window); } diff --git a/src/window.h b/src/window.h index 4f38b38..9d55123 100644 --- a/src/window.h +++ b/src/window.h @@ -16,4 +16,4 @@ typedef struct wn_window_s wn_window; bool wn_window_init(wn_window *window); void wn_window_deinit(wn_window *window); -void wn_swapwindow(wn_window *); +void wn_swapwindow(wn_window *window);