Timing and EVENT_APPSUSPEND

BlitzMax Forums/BlitzMax Programming/Timing and EVENT_APPSUSPEND

JoshK(Posted 2006) [#1]
Let's say you have a real-time 3D program running with a standard windows interface. Your loop looks like this:

Repeat
	While PeekEvent()
		Select WaitEvent()
			< Evaluate events here >
		EndSelect
	Wend
	RenderWorld()
	Flip
Forever


This loop will respond to all events immediately, but it will never pause the program to wait for an event.

The program uses delta timing, calculating the time elapsed each loop. Now if the user selects a menu, the program will be paused for a long period of time. I have a function that will pause the game timing. However, BlitzMax doesn't give me any indication that the user has been selecting a menu, moving a window, selecting a gadget, or doing any of the other things that can pause the program for a long period of time. If these actions generated an APPSUSPENDED event, I could just pause the game timing, but they don't.

This is a serious issue. If I want real-time physics and/or rendering in a windowed application, user input will create enormous delta timing values, which will most certainly result in problems. Capping the elapsed time value is not a good solution, because I want the application to pick up from right when the user paused the application.


JoshK(Posted 2006) [#2]
Here's the solution. This works even better, because it completely cuts the event evaluation time out of the loop:
Repeat
	eventstarttime=Millisecs()
	While PeekEvent()
		Select WaitEvent()
			< Evaluate events here >
		EndSelect
	Wend
	eventelapsedtime=Millisecs()-eventstarttime
	GAMETIME_LASTUPDATETIME=GAMETIME_LASTUPDATETIME+eventelapsedtime
	RenderWorld()
	Flip
Forever

Function UpdateGameTime()
	time=MilliSecs()
	GAMETIME_ELAPSED=time-GAMETIME_LASTUPDATETIME
	GAMETIME_VALUE=GAMETIME_VALUE+GAMETIME_ELAPSED
	GAMETIME_LASTUPDATETIME=time
	Return GAMETIME_VALUE
EndFunction

Function GameTime()
	Return GAMETIME_VALUE
EndFunction