Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Next stop, how do i determine if my app loses focus?

This thread is locked; no one can reply to it. rss feed Print
Next stop, how do i determine if my app loses focus?
cdxrd
Member #7,061
March 2006

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?

ReyBrujo
Moderator
January 2001
avatar

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.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Marcello
Member #1,860
January 2002
avatar

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. ;)

Marcello

cdxrd
Member #7,061
March 2006

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.

miran
Member #2,407
June 2002

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...

Quote:

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.

--
sig used to be here

cdxrd
Member #7,061
March 2006

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..

miran
Member #2,407
June 2002

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.

--
sig used to be here

Evert
Member #794
November 2000
avatar

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

gnolam
Member #2,030
March 2002
avatar

Quote:

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.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

miran
Member #2,407
June 2002

Oh yeah, that was a typo ;)

--
sig used to be here

Go to: