Main Loop - Best Practices

BlitzMax Forums/BlitzMax Beginners Area/Main Loop - Best Practices

blackwater(Posted 2011) [#1]
Hi all, I feel like a total newb for asking this, probably because I'm still a noob! But what does everyone recommend for the main loop? Right now I have a repeat / forever loop, and inside that WaitForEvent for everything. This includes a timer to update update the screen every 60 times per second, mouse and keyboard input, everything. Is this the recommend way to do it? Or should I just do away with the wait for event, update the screen with the "root" loop, and grab the mouse and keyboard input as they come in with SelectEventID()?

Would love to hear some suggestions. :)


Zacho(Posted 2011) [#2]
I am a pretty beginner programmer like yourself. What I initially did when I programmed was put a

Delay #


inside my main loop to get the timing right with the animations but now as I look back on that I do not want to limit my computer. Instead of:

player.x + 10
 
...

Delay 60


'I USE NOW

player.x + 3

...

Delay 0 


I figured if I can change the position (to decrease it) and take out the Delay function, I can keep the same game without limiting the cpu.


Redspark(Posted 2011) [#3]
If you don't go with a constant rate either from a timer or waiting on the vSync Flip(), then you should use a between calculation to determine what frame you are on. That way, the game appears to have a consistent frame rate for things like animation. Otherwise, the speed of you animation will change from computer to computer.

IMHO, it is best to use a constant loop rate if your game is 2D. Using the vsync page Flip() is often a simple way of doing that. Even still, it is best to use a 'Tween style approach as vsyncs can be missed during high calculations. Limiting the CPU usage also cuts down on battery drain of laptops, heat build up in all computers and returns some CPU cycles to the OS for it to deal with its own issues.

But that's just my opinion. I'd rather use a hammer where appropriate instead of a Sledge hammer -- especially if it isn't needed. ;)