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?
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.
I put your code in <code> </code> tags.
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.
Hi,
Thanks for the advice. How exactly do I separate these events properly(kind of lost, sorry), thanks again for the help!
switch statements are one way
switch (ev.type) { case ALLEGRO_TIMER: { } break; case ALLEGRO_EVENT_MOUSE_AXES: { } break; }
something like this?
You will want a flag to know if it's time to draw/update, so more like:
Hi,
Thanks for the help all. This is the current code within the loop:
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:
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.