Refactor input polling

This commit is contained in:
var
2026-03-29 23:04:49 -05:00
parent 9a61a71b09
commit 7b40b8702c
5 changed files with 65 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include "SDL2/SDL.h" #include "SDL2/SDL.h"
#include "SDL2/SDL_timer.h"
#include "GL/glew.h" #include "GL/glew.h"
#include "SDL2/SDL_opengl.h" #include "SDL2/SDL_opengl.h"
#include "game.h" #include "game.h"
@@ -31,55 +32,9 @@ GameState *Game_New()
return gs; return gs;
} }
static void HandleKeyDown(GameState *gs, SDL_KeyCode sym)
{
switch (sym)
{
case SDLK_ESCAPE:
gs->running = false;
break;
case SDLK_BACKQUOTE:
SDL_bool currentMode = SDL_GetRelativeMouseMode();
SDL_SetRelativeMouseMode(currentMode == SDL_FALSE ? SDL_TRUE : SDL_FALSE);
break;
case SDLK_F11:
{
Uint32 windowFlags = SDL_GetWindowFlags(gs->window);
if ((windowFlags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0) windowFlags = 0;
else windowFlags = SDL_WINDOW_FULLSCREEN_DESKTOP;
SDL_SetWindowFullscreen(gs->window, windowFlags);
}
break;
}
}
static void HandleKeyUp(GameState *gs, SDL_KeyCode sym)
{
}
void Game_Update(GameState *gs) void Game_Update(GameState *gs)
{ {
SDL_Event sdlEvent; SDL_Delay(100);
while (SDL_PollEvent(&sdlEvent))
{
switch (sdlEvent.type)
{
case SDL_QUIT:
printf("Event: SDL_QUIT\n");
gs->running = false;
break;
case SDL_KEYDOWN:
if (sdlEvent.key.repeat == 0)
{
HandleKeyDown(gs, sdlEvent.key.keysym.sym);
}
break;
case SDL_KEYUP:
HandleKeyUp(gs, sdlEvent.key.keysym.sym);
break;
}
}
} }
void Game_Destroy(GameState *gs) void Game_Destroy(GameState *gs)

View File

@@ -1,3 +1,5 @@
#pragma once
#include <stdbool.h> #include <stdbool.h>
typedef struct typedef struct

54
src/input.c Normal file
View File

@@ -0,0 +1,54 @@
#include "SDL2/SDL.h"
#include "input.h"
#include "game.h"
static void HandleKeyDown(GameState *gs, SDL_KeyCode sym)
{
switch (sym)
{
case SDLK_ESCAPE:
gs->running = false;
break;
case SDLK_BACKQUOTE:
SDL_bool currentMode = SDL_GetRelativeMouseMode();
SDL_SetRelativeMouseMode(currentMode == SDL_FALSE ? SDL_TRUE : SDL_FALSE);
break;
case SDLK_F11:
{
Uint32 windowFlags = SDL_GetWindowFlags(gs->window);
if ((windowFlags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0) windowFlags = 0;
else windowFlags = SDL_WINDOW_FULLSCREEN_DESKTOP;
SDL_SetWindowFullscreen(gs->window, windowFlags);
}
break;
}
}
static void HandleKeyUp(GameState *gs, SDL_KeyCode sym)
{
}
void Input_Poll(GameState *gs)
{
SDL_Event sdlEvent;
while (SDL_PollEvent(&sdlEvent))
{
switch (sdlEvent.type)
{
case SDL_QUIT:
printf("Event: SDL_QUIT\n");
gs->running = false;
break;
case SDL_KEYDOWN:
if (sdlEvent.key.repeat == 0)
{
HandleKeyDown(gs, sdlEvent.key.keysym.sym);
}
break;
case SDL_KEYUP:
HandleKeyUp(gs, sdlEvent.key.keysym.sym);
break;
}
}
}

5
src/input.h Normal file
View File

@@ -0,0 +1,5 @@
#pragma once
#include "game.h"
void Input_Poll(GameState *gs);

View File

@@ -1,7 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include "SDL2/SDL.h" #include "SDL2/SDL.h"
#include "SDL2/SDL_timer.h"
#include "game.h" #include "game.h"
#include "input.h"
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
@@ -16,8 +16,8 @@ int main(int argc, char* argv[])
while (gs->running) while (gs->running)
{ {
Input_Poll(gs);
Game_Update(gs); Game_Update(gs);
SDL_Delay(100);
} }
printf("Shutting down...\n"); printf("Shutting down...\n");