From 005d4c349510d78ab10f1e7fa1c9f5f5f265d8e9 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 30 Oct 2025 18:10:43 +0100 Subject: Implement movement Add simple movement according to Allegro manual. Signed-off-by: Carlos Maiolino --- src/rb.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) (limited to 'src/rb.c') diff --git a/src/rb.c b/src/rb.c index d0630ab..7486d93 100644 --- a/src/rb.c +++ b/src/rb.c @@ -107,10 +107,16 @@ register_events(struct rb *rb) void start_game(struct rb *rb) { +#define KEY_SEEN 1 +#define KEY_DOWN 2 + bool done = false; bool redraw = true; float x, y; ALLEGRO_EVENT event; + unsigned char key[ALLEGRO_KEY_MAX]; + memset(key, 0, sizeof(key)); + x = 100; y = 100; @@ -121,11 +127,46 @@ start_game(struct rb *rb) switch(event.type) { case ALLEGRO_EVENT_TIMER: + if (key[ALLEGRO_KEY_UP]) + y--; + if (key[ALLEGRO_KEY_DOWN]) + y++; + if (key[ALLEGRO_KEY_LEFT]) + x--; + if (key[ALLEGRO_KEY_RIGHT]) + x++; + + if (key[ALLEGRO_KEY_ESCAPE]) + done = true; + + for (int i = 0; i < ALLEGRO_KEY_MAX; i++) + key[i] &= ~KEY_SEEN; + redraw = true; break; case ALLEGRO_EVENT_KEY_DOWN: - /* fallthrough */ + key[event.keyboard.keycode] = KEY_SEEN | KEY_DOWN; + break; + case ALLEGRO_EVENT_KEY_UP: + key[event.keyboard.keycode] &= ~KEY_DOWN; + break; + +#if 0 + case ALLEGRO_EVENT_KEY_CHAR: + if (event.keyboard.keycode == ALLEGRO_KEY_UP) + y--; + if (event.keyboard.keycode == ALLEGRO_KEY_DOWN) + y++; + if (event.keyboard.keycode == ALLEGRO_KEY_LEFT) + x--; + if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT) + x++; + + if (event.keyboard.keycode != ALLEGRO_KEY_ESCAPE) + break; +#endif + case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; -- cgit v1.2.3