From f497ea0100a28f8dffed57eebe52ec58c556e1e1 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 30 Oct 2025 18:39:57 +0100 Subject: Implement mouse tracking. Make the object follow the mouse position Signed-off-by: Carlos Maiolino --- src/rb.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/rb.c') diff --git a/src/rb.c b/src/rb.c index 7486d93..83a7f28 100644 --- a/src/rb.c +++ b/src/rb.c @@ -47,6 +47,9 @@ initialize_allegro(struct rb **p) if (!al_install_keyboard()) goto err_alloc; + if (!al_install_mouse()) + goto err_alloc; + rb->timer = al_create_timer(rb->fps); if (!rb->timer) goto err_alloc; @@ -102,6 +105,9 @@ register_events(struct rb *rb) al_get_display_event_source(rb->display)); al_register_event_source(rb->event_queue, al_get_timer_event_source(rb->timer)); + al_register_event_source(rb->event_queue, + al_get_mouse_event_source()); + al_hide_mouse_cursor(rb->display); } void @@ -145,6 +151,11 @@ start_game(struct rb *rb) redraw = true; break; + case ALLEGRO_EVENT_MOUSE_AXES: + x = event.mouse.x; + y = event.mouse.y; + break; + case ALLEGRO_EVENT_KEY_DOWN: key[event.keyboard.keycode] = KEY_SEEN | KEY_DOWN; break; @@ -180,7 +191,7 @@ start_game(struct rb *rb) al_draw_textf(rb->font, al_map_rgb(255, 255, 255), 0, 0, 0, "X: %.1f Y: %.1f", x, y); - al_draw_filled_rectangle(x, y, x+10, y+10, + al_draw_filled_rectangle(x, y, x+40, y+120, al_map_rgb(255, 0, 0)); al_flip_display(); -- cgit v1.2.3