What is the best method to code a game loop : using a timer or calling a function in the message processing loop ? What are the differences between the two ? Is it best to use a timer or to write code to have a constant speed ?
Thanks.
Posted on 2001-10-17 15:57:42 by dr.manhattan
Depends on the game doesn't it! Also depends on what frame rate you're looking for. If you want about 18 or less fps then use a timer. You won't be able to get a higher fps out of win98 but I believe I've read here that the accuracy for win2k is greater allowing or a much wider range of fps.

If you don't want a set fps but rather want the best the system can provide then don't used a standard message processing loop. Try one like the following which I use
.data?

msg MSG {?}

.code
gl: ; ** Game loop here **

ml: xor eax,eax
lea edi,msg
invoke PeekMessage,edi,eax,eax,eax,PM_REMOVE
test eax,eax
jz gl
push edi
push edi
call TranslateMessage
call DispatchMessage
jmp ml

You escape out of it by the following APIs in response to a WM_DESTRY message
	invoke PostQuitMessage,0

invoke ExitProcess,0
Note that his method has 100% cpu usage according to win2k, for a game thats probably a good thing.

Final Note, since the fps will vary from system to system you can introduce a real time modifer to make things move at the same speed by multiplying every distance to be traveled, rotated, recharged, etc by the follow; x / TicksPassedPreviousFrame. X can be any number.

I usually use 1000 as it allows you to think of distance as units traveled per second.
Posted on 2001-10-17 16:51:48 by Eóin
Im trying to code a simple tetris game (without directX). I don't think the frame rate will be a problem. I will try both methods. Thanks a lot!
Posted on 2001-10-18 02:07:58 by Dr. Manhattan
For something like tetris I'd highly recommend a timer. Its simpler to use and your app will waste less cpu time :)
Posted on 2001-10-18 03:29:38 by Eóin