Next stop, how do i determine if my app loses focus?

OK, next step on my way to being the ultimate newb programmer..

If I have my app in windowed mode, how do I go about detecting if the application has lost focus, so that I can pause the game?


You don't need to do that manually. You can call set_display_switch_mode(SWITCH_PAUSE); and if it fails (returns != 0) use set_display_switch_mode(SWITCH_AMNESIA);. This pauses the game automatically when the window loses focus.


Yes but it also pauses sound and music and stuff which tends to be annoying/weird/bad.

There's a method you can use to trigger callbacks when the app gains/loses focus. I don't remember what it is, but looking in the manual is a good start. ;)



Ill check the manual, but manuals confuse the daylights out of me. Give me some code or an example to look at, disect and play around with, and I'll learn it a million times better than looking in a manual. Hence, I ask here. Thanks for the replies so far.

If it loses focus, I'm not sure if I would want to keep the music playing or not, so that will have to be a consideration. I can build my own pause routine if I can figure out when the app loses focus.


AFAIK Allegro programs pause by default when they lose focus so you only have to write extra code if you don't want that. You can do what ReyBrujo said to be extra certain though...


Ill check the manual, but manuals confuse the daylights out of me.

I suggest you get used to reading manuals very quickly otherwise there won't be long before people start being not very nice to you and reply with RTFM.


Ah well, I know it.. I do look at em, its just 100 times easier to look at a sample, than to dig through sometimes thousands of pages of docs. Time to dig out the books I suppose.

I've always wondered why people do that too. They will write out a 1000 word post that sums up to RTFM, when had they just answered it, it would have been simpler and quicker.. heh.. who knows.. =)

I'll figure out how to trap it. It auto-pauses when it loses focus, but I want to be able to display a blank screen when it loses it, plus I dont want it to auto-resume upon regaining focus. I'd rather have it wait for user input before it continued on. I'll do some more digging around on google.. There has to be a message sent that I can catch and process somehow..


Part of learning how to code is learning where to look for information. All you need to know is in the Allegro manual (assumnig you're using Allegro). Click on these functions and read the text:

int set_display_switch_mode(int mode);
int get_display_switch_mode();
int set_display_switch_callback(int dir, void (*cb)());
void remove_display_switch_callback(void (*cb)());

Some parts might be difficult to understand. If you have problems understanding the concepts, then ask here. But basically what you want to do is this:

  • set display switch mode to SWITCH_NONE (this isn't necessary, but it will make your music play in the background or whatever)

  • install a callback for the SWITCH_OUT event

  • in that function set a flag, something like GAME_PAUSED_AND_WAITING_FOR_USER_TO_PRESS_CONTINUE=true

  • in the main loop check if GAME_PAUSED_AND_WAITING_FOR_USER_TO_PRESS_CONTINUE==true, and if it is, don't do the game logic and all that, but instead display a "Press this button to continue" button or something like that. When the button is clicked (or a certain key is pressed or something), you set GAME_PAUSED_AND_WAITING_FOR_USER_TO_PRESS_CONTINUE=false and the game continues.


Also take note of the `Examples using this:' in the docs that point you to the proper example programme.


set display switch mode to SWITCH_NONE (this isn't necessary, but it will make your music play in the background or whatever)

No! That's the Abomination Mode, the Eater of UIs, The One Who Must Never Be Called. It's SWITCH_BACKGROUND or SWITCH_BACKAMNESIA that you want.


Oh yeah, that was a typo ;)

Thread #583627. Printed from