Hey guys,
please take a look at the following demonstration code and tell me if you get the same result as I do (tested on Win10):
So what this code should do is to show a green blinking screen if you hold down the left mouse button. When I hold down the left mouse button for some seconds, release it and start all over I get:
hold down mouse button - blinking screen
hold down mouse button - blinking screen
hold down mouse button - no blinking screen
hold down mouse button - no blinking screen
hold down mouse button - blinking screen
hold down mouse button - blinking screen
Any idea what's going on here?
You should always redraw on the timer event, and then set redraw to false.
Easier logic would be :
OK, your approach would avoid this effect.
However, as I understand it (maybe I'm missing something
), using this approach drawing a frame will never be skipped once the timer event occured. But if you run this loop on a slow computer that can't handle all draw calls you want to have frames skipped until the logic catches up. E.g. an explosion that last only one frame happens but cannot be drawn instantly then the explosion shouldn't be drawn 10 frames later.
The approach you used is bad because if you have multiple timer events in the queue, they can skip the draw = 1 call and you don't turn green. If you get a timer event (a draw timer) then redraw. Process all the remaining events first and limit your logical update call to once per graphic frame. That will slow down gracefully if you get too many timer events, and won't skip any frames. Or process all your logic at once, thus possibly skipping frames and then redraw.
You could also make it without polling the mouse. With events only.
I have edited your code to add extra color to the flashing effect. Added another timer to the mix.
And this will also skip frames when your computer is slow:
Edgar, I see pros and cons for both approaches. I'm migrating from Allegro 4 where I am used to have frames skipped How_can_I_make_my_game_run_at_the_same_speed_on_any_computer_ and when using Allegro 4 this maked sense for me. I'm going to rethink that now working with Allegro 5...
By the way, I still don't understand why in my approach the flashing is not visible sometimes event when some frames are skipped. If you modify the example and add one line to indicate frame skipping like this:
if (al_is_event_queue_empty(queue)) { al_clear_to_color(al_map_rgb(0,0,0)); if (draw) al_clear_to_color(al_map_rgb(0,128,0)); al_flip_display(); } else printf("Frame skipped\n");
you'll see that frames are skipped rarely, but the flashing occurs 10 times per second so at a much higher frequency than frames are skipped so that's the strange thing for me.
Izul, your approach looks quite similar to what Edgar suggested...
[EDIT]
To make it more clear that something strange is happening, try this code:
What I see is that "D" is printed to the console without blinking screen
You have two options - run one graphical frame per logical frame, slowing down gracefully, or run one graphical frame for all logic ticks, skipping frames.
If there is more than one timer event when delay reaches zero, draw will be set to 1 in the first event, and then set to 0 in the second event, as if it never happened. You're skipping frames when you don't need to.
I ran your latest example. I think it's probably just a case of unsynced output. I get 'D.......D.......D.......' too, and the green flash isn't visible sometimes.
OK guys, I think I figured it out by playing around with the ALLEGRO_VSYNC option: Everything is actually drawn, however, sometimes it is not visible on the screen because Vsync is disabled by default. Therefore it may or may not happen that the green screen that should be shown for one frame only is "skipped" by the monitor refreshing.
In any case, thanks for your input 
[EDIT]
I think it's probably just a case of unsynced output
OK somebody was faster than me...
Aliasing:
{"name":"alias.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/5\/d54a0a82e57d58d59ccbe5a77e9e25a5.png","w":477,"h":248,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/5\/d54a0a82e57d58d59ccbe5a77e9e25a5"}
Notice how if you look at the red dots, it appears to be a lower frequency.