the main loop - does everyone do the same ?

BlitzPlus Forums/BlitzPlus Programming/the main loop - does everyone do the same ?

Dax Trajero(Posted 2004) [#1]
I'm new to all this stuff so was wondering if I'm doing things correctly in my main loop

Function main_loop ()

While Not keyhit(1)

Update_Game_Timers()
Get_Player_input()
Check_Sprite_to_Screen_Collision()
Check_Sprite_to_Sprite_Collision()

WaitTimer(screentimer) ; timer runs at 60Hz
Draw_Screen()
Draw_Sprites()

Wend

End Function



As you can see from the above code I have a screentimer set at 60, so the screen is redrawn at 60Hz. Is this the best way to attain smooth graphics or am I using an incorrect method ?


Moore(Posted 2004) [#2]
Well I would say no, it does not utilize the full power of the computer, it limits frame rate try this:

say you want your gameObj to move across the screen at 100 pixels per second

speed = 100

newTime = Millisecs()
timeElapsed = newTime - OldTime
x = x + (speed/1000) * timeElapsed
oldTime = newTime

This allows the game to run at the optimal cycles while the gameObj moves at a constant rate. Whether you achieve 10 frames per sec or 200 the gameObj will always move at 100 pixels per sec.


morduun(Posted 2004) [#3]
I wouldn't use timers. Delta time's much more useful in the long run for frame timing -- tutorial on delta time here.


Dax Trajero(Posted 2004) [#4]
Thanks guys will definitely look into delta time

At the moment I want to get the main loop correct - just discovered vwait : flip false. Is it preferrable to use this?

What is happening during vwait : flip false ?


Dax Trajero(Posted 2004) [#5]
mordun, thanks for that Delta Time tutorial - certainly a more efficient way to run my code.

My question is this - regardless of whether I use delta time or Create Timer / Wait Timer methods...

...assuming my user has disabled the VSYNC on their video card, which means its best to not use VWAIT...

...if I use VWAIT : FLIP FALSE then how does this manage to sync my code with the users video card ?


morduun(Posted 2004) [#6]
Don't ever use VWAIT : FLIP FALSE. It hoses on certain drivers and causes unmanageably bad flickering. You're safest using Flip True. If the user has vwait notification turned off then delta time will still manage the framerate properly.


MSW(Posted 2004) [#7]
One potential problem with delta timeing is the millisec value will eventualy "roll over" back to zero...which in turn can lead to a game update basied upon a huge negative elapsed time...Of course this is very rare (like once every month or so if the PC is left continuesly on)...but the result can be pretty devistateing if not taken into account.

A simple check of elapsed time (if elapsedtime>0 then continue to update the game) is all you really would need...Sure it could lead to a skipped update when it occurs, but that is better then the player suddenly finding themselves and other game objects thrown across the map because elapsed time, used to calculate movements, winds up being some huge negative number during a update....so keep it in mind

also you really may not want your application to monopolise the CPU if you want it to live happily with a multitasking enviroment like Windows...course there are better functions then Blitz timers that are suited to maximizeing your use of the timeslice granted by the OS.