From d2d0886cf770488d1b4890a3a414bbf983e784a6 Mon Sep 17 00:00:00 2001 From: esquizo Date: Sat, 2 May 2026 14:19:31 -0300 Subject: desacoplado keys --- dat.h | 2 ++ game.c | 9 ++++----- sdl3_main.c | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/dat.h b/dat.h index 76653b6..723f1c2 100644 --- a/dat.h +++ b/dat.h @@ -48,3 +48,5 @@ extern Entity *player; extern bool shot; extern int shotx, shoty; + +extern bool moveleft, moveright, moveup, movedown; diff --git a/game.c b/game.c index fc126a1..fdf0125 100644 --- a/game.c +++ b/game.c @@ -81,18 +81,17 @@ void playerproc(Entity *e, float delta) { Entity *bullet; - const _Bool *kstate = SDL_GetKeyboardState(NULL); e->body.vel[0] = 0; e->body.vel[1] = 0; - if(kstate[SDL_SCANCODE_W]) + if(moveup) e->body.vel[1] -= SPEED; - if(kstate[SDL_SCANCODE_A]) + if(moveleft) e->body.vel[0] -= SPEED; - if(kstate[SDL_SCANCODE_S]) + if(movedown) e->body.vel[1] += SPEED; - if(kstate[SDL_SCANCODE_D]) + if(moveright) e->body.vel[0] += SPEED; if(shot) { diff --git a/sdl3_main.c b/sdl3_main.c index 709a37c..701dfe4 100644 --- a/sdl3_main.c +++ b/sdl3_main.c @@ -17,6 +17,7 @@ static Uint64 old_ticks, new_ticks; static float camera_x, camera_y; bool shot; +bool moveup, movedown, moveleft, moveright; int shotx, shoty; int @@ -159,6 +160,20 @@ process_events() shotx = event.button.x + camera_x; shoty = event.button.y + camera_y; break; + + case SDL_EVENT_KEY_UP: + case SDL_EVENT_KEY_DOWN: + #define DEFINE_KEY(X, VAR) \ + if(event.key.scancode == X) VAR = event.type == SDL_EVENT_KEY_DOWN + + DEFINE_KEY(SDL_SCANCODE_W, moveup); + DEFINE_KEY(SDL_SCANCODE_A, moveleft); + DEFINE_KEY(SDL_SCANCODE_S, movedown); + DEFINE_KEY(SDL_SCANCODE_D, moveright); + + #undef DEFINE_KEY + + break; } } } -- cgit v1.2.3