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/include/rb.h | 4 ++-- src/rb.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/include/rb.h b/src/include/rb.h index fb3f617..cc321e6 100644 --- a/src/include/rb.h +++ b/src/include/rb.h @@ -3,8 +3,8 @@ #include -#define RB_WIDTH 640 -#define RB_HEIGHT 480 +#define RB_WIDTH 1600 +#define RB_HEIGHT 900 #define RB_FPS (1.0 / 30.0) /* 1 sec divided by 30 frames */ struct rb { 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