diff options
| -rw-r--r-- | dat.h | 2 | ||||
| -rw-r--r-- | game.c | 9 | ||||
| -rw-r--r-- | sdl3_main.c | 15 |
3 files changed, 21 insertions, 5 deletions
@@ -48,3 +48,5 @@ extern Entity *player; extern bool shot; extern int shotx, shoty; + +extern bool moveleft, moveright, moveup, movedown; @@ -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; } } } |
