How to detect if the window loses focus?

BlitzMax Forums/BlitzMax Beginners Area/How to detect if the window loses focus?

BenKo(Posted 2007) [#1]
Hi!

I'm coding a platforms game and when the window the game is running on loses focus, the framerate falls a lot and some of my physics functions stop working --making the game unplayable when focus returns.

So, I'd like to pause the game whenever focus is lost (alt+tab/minimizing window/etc), but I don't know how to detect when focus is lost/obtained.

Any suggestions? Thanks in advance :)


GfK(Posted 2007) [#2]
AppSuspended()


BenKo(Posted 2007) [#3]
AppSuspended() returns always TRUE, regardless if I do ALT+TAB or minimize the window or whatever :(


WendellM(Posted 2007) [#4]
Could you please supply some code showing that behavior, BenKo? For example, this works OK for me, showing "Not Suspended" when it has focus and "SUSPENDED" when another app does:
Graphics 320,200
Local txt$
Repeat
	If AppSuspended() Then txt = "SUSPENDED" Else txt = "Not Suspended"
	Cls
	DrawText txt, 10,10
	Flip
	Delay 1
Until KeyHit(KEY_ESCAPE) Or AppTerminate()
Does it always show "SUSPENDED" for you?


BenKo(Posted 2007) [#5]
Ok, fixed now. Thanks for your replies. What happened was
that...

If AppSuspended Then Print "Suspended"


...always is true. If I add the brackets, and type AppSuspended() instead, then it works as it should. Don't know why, though...


*(Posted 2007) [#6]
because AppSuspended is treated as a variable where AppSuspended() is the function :). A created variable is always created with the value True so it always True.


BenKo(Posted 2007) [#7]
If AppSuspended (without brackets) is a variable... how does my program even compile? I work with SuperStrict to avoid this kind of stuff :\


WendellM(Posted 2007) [#8]
Actually, unless initialized, integer variables are (auto-)created with value of false (0). But that's not what's happening here (and BenKo is correct that SuperStrict would prevent that). It seems that using a function name without brackets always returns true:
If foo Then Print "foo true" Else Print "foo false"
If AppSuspended Then Print "appsuspended true" Else Print "appsuspended false"
If Sin Then Print "sin true" Else Print "sin false"
If AppTerminate Then Print "appterminate true" Else Print "appterminate false"
Sticking a "Strict" or "SuperStrict" at the top will stop foo from being auto-created, and you can't do much else with functions without brackets (trying to print one will give an error). I guess(?) what's happening is the compiler is checking for the existence of the function in that If condition.


Grey Alien(Posted 2007) [#9]
Yeah the function will have a memory address and thus will be non-zero.