summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-10-30 18:10:43 +0100
committerCarlos Maiolino <[email protected]>2025-10-30 18:10:43 +0100
commit005d4c349510d78ab10f1e7fa1c9f5f5f265d8e9 (patch)
treefa78d35de551921a8ad3759c2c325f1930fab45d
parent5e7ef9236c51593074da22f42d25b1d622939707 (diff)
Implement movement
Add simple movement according to Allegro manual. Signed-off-by: Carlos Maiolino <[email protected]>
-rw-r--r--src/include/rb.h4
-rw-r--r--src/rb.c43
2 files changed, 44 insertions, 3 deletions
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 <allegro5/allegro5.h>
-#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;