al_stop_timer when lost focus?
duncan perham

I notice that when debugging, the timer is still running (because its on a seperate thread). Problem is, when the game resumes it trys to catch up on lost time giving like 400 fps for a while. Is there anyway to make it pause when it loses focus?

Aikei_c

You can use ALLEGRO_EVENT_DISPLAY_SWITCH_OUT and ALLEGRO_EVENT_DISPLAY_SWITCH_IN.
Or you can make it so that when the difference between the current time and the last run time is greater than some value, say, 1 second, then this difference shall be, say, 1/60 of a second anyway. Then you should use this difference for all your game logic. This way, after a long pause it should be considered one tick anyway.
Probably there are better ways, but that's what came to my mind.

duncan perham

Thanks for the ideas, the problem is, I have no control over the timing other than to set it to whatever FPS I want.

al_wait_for_event(queue,&event);
if (event.type == ALLEGRO_EVENT_TIMER)
redraw = true;

it seems that the al_wait_for_event just spams ALLEGRO_EVENT_TIMER events until it catches up. It dosent seem to realise that focus was lost.

l j

Handle the ALLEGRO_EVENT_DISPLAY_SWITCH_OUT and ALLEGRO_EVENT_DISPLAY_SWITCH_IN events.

if (event.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT)
   al_stop_timer(timer);
else if (event.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN)
   al_start_timer(timer);

duncan perham

unfortunatley taron, that wont work, as the debugger stops the main thread before it can send the switchout event, so the timer just keeps counting. It does work as a pause when the game dosent have focus, ie alt tab out etc.

The debugger breaking into the game dosent give it a chance to respond as it freezes the main thread, but while its being debugged the update commands are still being added because the timming thread is still running!.

If that sort of code was embeded into the timing thread it would work great. But I dont think it is. So the only other solution I can think of is... if there is someway of detecting that there is a few hundred events sitting on the que, then ignoring all the update events.

al_flush_event_queue(ALLEGRO_EVENT_QUEUE *queue).
But how can I get the number of events on the queue.

well I set it up like this...........
LastTime=al_get_timer_count(timer);
while(!Quit)
{
//al_get_next_event(queue, &event);
//processing

al_wait_for_event(queue,&event);
ThisTime=al_get_timer_count(timer);
if(ThisTime-LastTime>10)
al_flush_event_queue(queue);
LastTime=ThisTime;

which seems to do the trick, since I set the FPS as 60, I am assuming that the timer counts 60 times a second?. In theory, it should only ever flush the Que when teh debugger stops the code. If anyone has any better ideas let me know.

Aikei_c

Aa I said, you can store the time of last run through the loop and check if the difference between the current time and last run time is too big. And do anything you want if it is. Like flushing the event queue.

Thread #613537. Printed from Allegro.cc