From e383f8bf93c904378f345f5443526c40f42d6cfa Mon Sep 17 00:00:00 2001 From: Warwick Date: Mon, 18 Aug 2025 15:30:53 +0100 Subject: [PATCH] Fixed event polling loop which fixed laggy input --- src/main.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index 8d84aa4..40111f3 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,11 @@ -#include #include +#include #include #include #include +#include "SDL3/SDL_events.h" +#include "SDL3/SDL_keycode.h" #include "camera.h" #include "cglm/io.h" @@ -144,17 +146,26 @@ int main(int argc, char *argv[]) { // Initialise camera Camera camera = Camera_default; - - // Lock and hide mouse to window + + // Lock and hide mouse SDL_SetWindowRelativeMouseMode(window, true); SDL_GetRelativeMouseState(NULL, NULL); + bool wants_running = true; + while (wants_running) { - while (1) { - // handle input - SDL_PollEvent(&event); - if (event.type == SDL_EVENT_QUIT) { - break; + // handle input events + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_EVENT_QUIT: + wants_running = false; + break; + case SDL_EVENT_KEY_DOWN: + if (event.key.key == SDLK_ESCAPE) { + wants_running = false; + } + break; + } } // Handle camera @@ -163,7 +174,7 @@ int main(int argc, char *argv[]) { // TODO: make shader file mat4 mvp; camera_calc_mvp(&mvp, &camera); - //glm_mat4_print(mvp,stdout); + // glm_mat4_print(mvp,stdout); int mvp_uniform_location = glGetUniformLocation(shaderProgram, "MVP"); glUniformMatrix4fv(mvp_uniform_location, 1, GL_FALSE, &mvp[0][0]);