Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Mouse Activity Question

This thread is locked; no one can reply to it. rss feed Print
Mouse Activity Question
variancegears
Member #23,382
June 2022

Hello,

I've been experimenting mouse implementation and have noticed that when I move my mouse within the display window, the incrementation speed also increases. Here is my code:

bool running = false;

int pos_x = 0;
int pos_y = 0;
int increment = 0;

int main()
{
al_init();
al_install_mouse();
al_install_keyboard();
al_init_image_addon();
al_init_font_addon();
al_init_ttf_addon();

ALLEGRO_DISPLAY* display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
ALLEGRO_FONT* font25 = al_load_ttf_font("slkscr.ttf", 25, 0);
ALLEGRO_TIMER* timer = al_create_timer(1.0 / MAX_FPS);

al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_mouse_event_source());

al_start_timer(timer);

while (!running)
{
ALLEGRO_EVENT ev;

al_wait_for_event(event_queue, &ev);

if (ev.type == ALLEGRO_EVENT_MOUSE_AXES)
{
pos_y = ev.mouse.y;
pos_x = ev.mouse.x;
}

al_clear_to_color(al_map_rgb(RED));

++increment;

al_draw_textf(font25, al_map_rgb(BLACK), 0, 0, 0, "POS X: %i", pos_x);
al_draw_textf(font25, al_map_rgb(BLACK), 0, 50, 0, "POS Y: %i", pos_y);

al_draw_textf(font25, al_map_rgb(BLACK), 0, 100, 0, "INC: %i", increment);
al_flip_display();
}
}

Why is this happening / how do I fix this?

Dizzy Egg
Member #10,824
March 2009
avatar

It's because you're incrementing every time there's a mouse event. You should only be drawing when there's a timer event. Update your pos_x/pos_y when there's a mouse event, and draw everything when there's a timer event.

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

torhu
Member #2,727
September 2002
avatar

I put your code in <code> </code> tags.

#SelectExpand
1bool running = false; 2 3int pos_x = 0; 4int pos_y = 0; 5int increment = 0; 6 7int main() { 8 al_init(); 9 al_install_mouse(); 10 al_install_keyboard(); 11 al_init_image_addon(); 12 al_init_font_addon(); 13 al_init_ttf_addon(); 14 15 ALLEGRO_DISPLAY * display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT); 16 ALLEGRO_EVENT_QUEUE * event_queue = al_create_event_queue(); 17 ALLEGRO_FONT * font25 = al_load_ttf_font("slkscr.ttf", 25, 0); 18 ALLEGRO_TIMER * timer = al_create_timer(1.0 / MAX_FPS); 19 20 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 21 al_register_event_source(event_queue, al_get_keyboard_event_source()); 22 al_register_event_source(event_queue, al_get_mouse_event_source()); 23 24 al_start_timer(timer); 25 26 while (!running) { 27 ALLEGRO_EVENT ev; 28 29 al_wait_for_event(event_queue, & ev); 30 31 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) { 32 pos_y = ev.mouse.y; 33 pos_x = ev.mouse.x; 34 } 35 36 al_clear_to_color(al_map_rgb(RED)); 37 38 ++increment; 39 40 al_draw_textf(font25, al_map_rgb(BLACK), 0, 0, 0, "POS X: %i", pos_x); 41 al_draw_textf(font25, al_map_rgb(BLACK), 0, 50, 0, "POS Y: %i", pos_y); 42 43 al_draw_textf(font25, al_map_rgb(BLACK), 0, 100, 0, "INC: %i", increment); 44 al_flip_display(); 45 } 46}

I've been experimenting mouse implementation and have noticed that when I move my mouse within the display window, the incrementation speed also increases.

Because it's incremented every time there is a mouse event. It's probably better to have the loop run at fixed speed, only update and draw when you get a timer event.

variancegears
Member #23,382
June 2022

Hi,

Thanks for the advice. How exactly do I separate these events properly(kind of lost, sorry), thanks again for the help!

DanielH
Member #934
January 2001
avatar

switch statements are one way

switch (ev.type)
{
case ALLEGRO_TIMER:
{
} break;

case ALLEGRO_EVENT_MOUSE_AXES:
{
} break;
}

variancegears
Member #23,382
June 2022

something like this?

#SelectExpand
1 switch (ev.type) 2 { 3 case ALLEGRO_EVENT_MOUSE_AXES: 4 { 5 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) 6 { 7 pos_y = ev.mouse.y; 8 pos_x = ev.mouse.x; 9 } 10 } 11 12 case ALLEGRO_EVENT_TIMER: 13 { 14 ++increment; 15 } 16 17 }

Dizzy Egg
Member #10,824
March 2009
avatar

You will want a flag to know if it's time to draw/update, so more like:

#SelectExpand
1bool updateFlag = false; 2 3//....code 4 5switch (ev.type) 6{ 7 case ALLEGRO_EVENT_MOUSE_AXES: 8 { 9 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) 10 { 11 pos_y = ev.mouse.y; 12 pos_x = ev.mouse.x; 13 } 14 } 15 16 case ALLEGRO_EVENT_TIMER: 17 { 18 updateFlag = true; 19 } 20 21} 22 23//...code 24 25if (updateFlag) 26{ 27 ++increment; 28 //...draw everything 29 updateFlag = false; 30}

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

variancegears
Member #23,382
June 2022

Hi,

Thanks for the help all. This is the current code within the loop:

#SelectExpand
1 while (!running) 2 { 3 ALLEGRO_EVENT ev; 4 5 al_wait_for_event(event_queue, &ev); 6 7 switch (ev.type) 8 { 9 case ALLEGRO_EVENT_MOUSE_AXES: 10 { 11 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) 12 { 13 pos_y = ev.mouse.y; 14 pos_x = ev.mouse.x; 15 } 16 } 17 18 case ALLEGRO_EVENT_TIMER: 19 { 20 updateFlag = true; 21 } 22 23 } 24 25 if (updateFlag) 26 { 27 ++increment; 28 29 updateFlag = false; 30 } 31 32 al_clear_to_color(al_map_rgb(RED)); 33 34 al_draw_textf(font25, al_map_rgb(BLACK), 0, 0, 0, "POS X: %i", pos_x); 35 al_draw_textf(font25, al_map_rgb(BLACK), 0, 50, 0, "POS Y: %i", pos_y); 36 37 al_draw_textf(font25, al_map_rgb(BLACK), 0, 100, 0, "INC: %i", increment); 38 al_flip_display(); 39 }

The increment value is still being affected by the movement of the mouse (incrementing faster with mouse movement within display window). Also tried to draw within the if statement and still getting the same issue:

#SelectExpand
1 while (!running) 2 { 3 ALLEGRO_EVENT ev; 4 5 al_wait_for_event(event_queue, &ev); 6 7 switch (ev.type) 8 { 9 case ALLEGRO_EVENT_MOUSE_AXES: 10 { 11 if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) 12 { 13 pos_y = ev.mouse.y; 14 pos_x = ev.mouse.x; 15 } 16 } 17 18 case ALLEGRO_EVENT_TIMER: 19 { 20 updateFlag = true; 21 } 22 23 } 24 25 if (updateFlag) 26 { 27 ++increment; 28 29 updateFlag = false; 30 al_clear_to_color(al_map_rgb(RED)); 31 32 al_draw_textf(font25, al_map_rgb(BLACK), 0, 0, 0, "POS X: %i", pos_x); 33 al_draw_textf(font25, al_map_rgb(BLACK), 0, 50, 0, "POS Y: %i", pos_y); 34 35 al_draw_textf(font25, al_map_rgb(BLACK), 0, 100, 0, "INC: %i", increment); 36 al_flip_display(); 37 38 } 39 }

Erin Maus
Member #7,537
July 2006
avatar

You're missing a break statement after the body of each case. Without a break statement, the execution "falls through" from the ALLEGRO_EVENT_MOUSE_AXES case block to the ALLEGRO_EVENT_TIMER case block. Just add a break after line 16 and one after line 21. Remember, as you add more cases, you'll want to add these break statements as well to prevent this issue (or a different issue) in the future.

---
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
they / she

Go to: