Code archives/Miscellaneous/HasFocus()

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

Download source code

HasFocus() by sswift2003
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

WildCat2009
It seems to not work within debug mode. Or if I do something wrong?


Zethrax2013
Not sure what the hell this code is meant to achieve, but the code below is what I'm currently using to recover from an alt-tab.

; Requires:-
; user32.decls - http://www.blitzbasic.com/codearcs/codearcs.php?code=1179

; Place this file into your Blitz3D 'userlibs' folder (eg. C:\Program Files (x86)\Blitz3D\userlibs).

;------

; Place the code below at the top of your main loop.
; InitializeInputAndGraphics and SyncTiming functions will need to be provided by you.

; If an alt-tab occurred then re-synchronize the input and timing.
If api_GetActiveWindow() <> G_app_handle
	While api_GetActiveWindow() <> G_app_handle : Delay( 20 ) : Wend
	; -- Perform the re-synchronization functions you would normally perform after coming back from a pause here.
	InitializeInputAndGraphics
	SyncTiming
	;------
EndIf



Code Archives Forum