Code archives/Miscellaneous/HasFocus()

This code has been declared by its author to be Public Domain code.

Download source code

HasFocus() by sswift
(Posted 23 years ago)
This function tells you if your game has been tabbed out of. It should work in all versions of Blitz, it has been tested in Blitz 3D.

All you need to do is take the code below, and place it in a text file in your userlibs directory, and name it "user32.decls". The function will then be available for you to use, and you don't even need to include a DLL with your game, or put the user32.dll in the userlib folder, because it's part of Windows.

I reccomend that you use code similar to the following to pause your game:

If Not HasFocus() ; If the game lost focus...

Repeat ; Pause the game until focus has been regained.
Delay(100) ; Using delay dramatically reduces the amount of CPU power the game uses!
Until HasFocus()

Time_Now = MilliSecs() ; Correct the time.

EndIf


Note how I reset Current_Time. Current_Time contains the time the current frame started. I need to reset this value, because otherwise upon returning to the game, it would think that the last frame took several seconds, minutes, or even hours to render, and everyhting in the game would be moved accoridngly, which would wreak havoc. This way, the game thinks no time passed at all. Of course there are even better ways to handle this situation which would take into account the possiblity of the millisecond timer wrapping while your game is paused, but as most people don't even worry about that with their regular timing routines, I think this is good enough.

PS:
Thanks go to Fredborg for this function!
; User32.decls

; Place in your Blitz Userlibs folder

.lib "user32.dll"

HasFocus%():"GetActiveWindow"

Comments

None.

Code Archives Forum