Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro 5 - Moving mouse speeds up game FPS

This thread is locked; no one can reply to it. rss feed Print
Allegro 5 - Moving mouse speeds up game FPS
RDMills
Member #15,478
January 2014

Hello all,

Just a quick question.

event_queue = al_create_event_queue();
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());

for(;;)
{
ALLEGRO_EVENT ev;
al_wait_for_event (event_queue, &ev);

if (ev.type == ALLEGRO_EVENT_TIMER)
{
if (ev.timer.source == timer)
redraw = true;
}
}

// GAME MECHANICS HERE //

if (redraw && al_is_event_queue_empty (fps_event_queue))
{
redraw = false;

REDRAW_SCREEN();
}
}

Is there a way to have the mouse moving and NOT speed up the game?
FPS and everything else works fine before I added the mouse_event_source, but now it just causes the main loop to skip updating because it ignores the redraw when there is an event in queue. I'm sure I'm making this harder than it needs to be, somehow.

Any help anyone could give would be super! Thanks in advance!
-RDMills

l j
Member #10,584
January 2009
avatar

Yes, update game state when handling the timer event.

RDMills
Member #15,478
January 2014

But I think the problem is that it's waiting for an event. I tried using two event handlers, but that didn't work at all.. ???

Kris Asick
Member #1,424
July 2001

1. Please use <code></code> tags to make your code more readable because...

2. There's more close-brackets than open-brackets in the code you posted so I get the feeling you haven't posted all the relevant bits. To that end, I'm having trouble figuring out where your loops are and how your event handling fits into everything.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

RDMills
Member #15,478
January 2014

#SelectExpand
1 timer = al_create_timer (1.0 / FPS); 2 fps_event_queue = al_create_event_queue(); 3 //al_register_event_source (fps_event_queue, al_get_display_event_source (display)); // Removed because we're running in full screen (not needed) 4 al_register_event_source (fps_event_queue, al_get_timer_event_source (timer)); 5 al_register_event_source (fps_event_queue, al_get_keyboard_event_source()); 6 al_register_event_source (fps_event_queue, al_get_mouse_event_source()); 7 8 al_flip_display (); 9 al_start_timer (timer); 10 11 for(;;) 12 { 13 ALLEGRO_EVENT ev; 14 al_wait_for_event (fps_event_queue, &ev); 15 16 // Timer Events 17 if (ev.type == ALLEGRO_EVENT_TIMER) 18 { 19 if (ev.timer.source == timer) 20 redraw = true; 21 } 22 if (ev.type == ALLEGRO_EVENT_KEY_DOWN) 23 { 24 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 25 break; 26 27 else if (ev.keyboard.keycode == ALLEGRO_KEY_A) 28 Playership.rotateLeft = true; 29 30 else if (ev.keyboard.keycode == ALLEGRO_KEY_D) 31 Playership.rotateRight = true; 32 33 else if (ev.keyboard.keycode == ALLEGRO_KEY_W) 34 Playership.moveForward = true; 35 36 else if (ev.keyboard.keycode == ALLEGRO_KEY_C) 37 if (Camera.locked == true) 38 Camera.locked = false; 39 else 40 Camera.locked = true; 41 42 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_6) 43 Camera.moveEast = true; 44 45 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_4) 46 Camera.moveWest = true; 47 48 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_8) 49 Camera.moveNorth = true; 50 51 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_2) 52 Camera.moveSouth = true; 53 54 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_MINUS) 55 Camera.zOut = true; 56 57 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_PLUS) 58 Camera.zIn = true; 59 } 60 if (ev.type == ALLEGRO_EVENT_KEY_UP) 61 { 62 if (ev.keyboard.keycode == ALLEGRO_KEY_A) 63 Playership.rotateLeft = false; 64 65 else if (ev.keyboard.keycode == ALLEGRO_KEY_D) 66 Playership.rotateRight = false; 67 68 else if (ev.keyboard.keycode == ALLEGRO_KEY_W) 69 Playership.moveForward = false; 70 71 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_6) 72 Camera.moveEast = false; 73 74 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_4) 75 Camera.moveWest = false; 76 77 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_8) 78 Camera.moveNorth = false; 79 80 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_2) 81 Camera.moveSouth = false; 82 83 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_MINUS) 84 Camera.zOut = false; 85 86 else if (ev.keyboard.keycode == ALLEGRO_KEY_PAD_PLUS) 87 Camera.zIn = false; 88 } 89 90 MOVE_SHIP_STUFF(); 91 MOVE_CAMERA_STUFF(); 92 93 // Redraw the screen if everything is all caught up 94 if (redraw && al_is_event_queue_empty (fps_event_queue)) 95 { 96 redraw = false; 97 98 RENDER_GFX(); 99 100 al_flip_display (); 101 al_clear_to_color (al_map_rgb (0, 0, 0)); 102 } 103 }

l j
Member #10,584
January 2009
avatar

MOVE_SHIP_STUFF();
MOVE_CAMERA_STUFF();

These two lines should probably be handled in the timer event.

if (ev.type == ALLEGRO_EVENT_TIMER)
{
   if (ev.timer.source == timer)
   {
       MOVE_SHIP_STUFF();
       MOVE_CAMERA_STUFF();
       redraw = true;
   }
}

RDMills
Member #15,478
January 2014

;D, the logic is obvious now... Sometimes I leave my brain behind.
Thank you for the advice, it's clear that I'm redrawing my screen every 60 frames now, but the game mechanics are system-speed dependent. WOW, I'm not smart... :(

AND... It works beautifully now...
Thanks!

-RDMills

Go to: