summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-11-29 17:32:25 +0100
committerCarlos Maiolino <[email protected]>2025-11-29 17:32:25 +0100
commit4242664fcd5d2c3e0fe24ffec038c3767276f3e5 (patch)
tree8d6ca226f58fe9f7601f83bec8cb8c2c9b04ce67
parentf497ea0100a28f8dffed57eebe52ec58c556e1e1 (diff)
Change mouse input to use mouse speed
Instead of directly setting the object accordingly to the mouse's cursor orientation, use a 'speed' trick to move the object. X and Y coordinates are now incremented according to the speed instead of hard setting them to the current x*y hardware coordinates. Use al_set_mouse_xy() to always reset the real mouse position (not the object). This will avoid the mouse position to hit one of the limits preventing the object to be moved further. Signed-off-by: Carlos Maiolino <[email protected]>
-rw-r--r--src/include/rb.h4
-rw-r--r--src/rb.c44
2 files changed, 35 insertions, 13 deletions
diff --git a/src/include/rb.h b/src/include/rb.h
index aba3488..abbeb0e 100644
--- a/src/include/rb.h
+++ b/src/include/rb.h
@@ -3,8 +3,8 @@
#include <allegro5/allegro5.h>
-#define RB_WIDTH 2240
-#define RB_HEIGHT 1400
+#define RB_WIDTH 640
+#define RB_HEIGHT 480
#define RB_FPS (1.0 / 80.0) /* 1 sec divided by 30 frames */
struct rb {
diff --git a/src/rb.c b/src/rb.c
index 83a7f28..0307d57 100644
--- a/src/rb.c
+++ b/src/rb.c
@@ -108,6 +108,7 @@ register_events(struct rb *rb)
al_register_event_source(rb->event_queue,
al_get_mouse_event_source());
al_hide_mouse_cursor(rb->display);
+ al_grab_mouse(rb->display);
}
void
@@ -119,11 +120,14 @@ start_game(struct rb *rb)
bool done = false;
bool redraw = true;
float x, y;
+ float dx, dy; /* mouse velocity - change in coordinate since prev event*/
ALLEGRO_EVENT event;
unsigned char key[ALLEGRO_KEY_MAX];
memset(key, 0, sizeof(key));
+ dx = 0;
+ dy = 0;
x = 100;
y = 100;
al_start_timer(rb->timer);
@@ -133,18 +137,35 @@ 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++;
+ x += dx;
+ y += dy;
+
+ if (x < 0) {
+ x *= -1;
+ dx *= -1;
+ }
+
+ if (x > rb->width) {
+ x -= (x - rb->width) * 2;
+ dx *= -1;
+ }
+
+ if (y < 0) {
+ y *= -1;
+ dy *= -1;
+ }
+
+ if (y > rb->height) {
+ y -= (y - rb->height) * 2;
+ dy *= -1;
+ }
if (key[ALLEGRO_KEY_ESCAPE])
done = true;
+ dx *= 0.9;
+ dy *= 0.9;
+
for (int i = 0; i < ALLEGRO_KEY_MAX; i++)
key[i] &= ~KEY_SEEN;
@@ -152,8 +173,9 @@ start_game(struct rb *rb)
break;
case ALLEGRO_EVENT_MOUSE_AXES:
- x = event.mouse.x;
- y = event.mouse.y;
+ dx += event.mouse.dx * 0.1;
+ dy += event.mouse.dy * 0.1;
+ al_set_mouse_xy(rb->display, 320, 240);
break;
case ALLEGRO_EVENT_KEY_DOWN:
@@ -190,7 +212,7 @@ start_game(struct rb *rb)
al_clear_to_color(al_map_rgb(0, 0, 0));
al_draw_textf(rb->font, al_map_rgb(255, 255, 255),
0, 0, 0,
- "X: %.1f Y: %.1f", x, y);
+ "X: %.1f Y: %.1f DX: %f DY: %f", x, y, dx, dy);
al_draw_filled_rectangle(x, y, x+40, y+120,
al_map_rgb(255, 0, 0));
al_flip_display();