Skip to main content
gamedev.net
🔒 Locked

Game main loop...

Started by mweb 11 replies 700+ views
Original Post
mweb
mweb
Hi, i''m starting to program now... but i cannot find any good game programming topics... how can i create a game loop indipendent of machine speed ??? Thank you
Skibum
Skibum
Use a timer to define how far things move
VisualB4BigD
VisualB4BigD
The game loop is not usually independent of the computer speed. Because the loop will execute as fast as the computer will allow. You can slow it down by using

sleep(time in milliseconds)

But to make it so that you animation runs the same on all computers, you use timing commands to determine how much time has passed and then you update if the specified amount of time has passed. Try to learn as much about programming as you can before you go into making games. Good Luck, don't give up.

I sure hope this world isn't one big joke, because I don't get it.

[edited by - VisualB4BigD on April 16, 2002 8:32:08 PM]
netjockey
netjockey
Check out that article:
http://www.gamedev.net/reference/programming/features/gamemovement/default.asp

greetz,
netjockey
Vanz
Vanz

netjockey wrote:

"The game loop is not usually independent of the computer speed. Because the loop will execute as fast as the computer will allow. You can slow it down by using

sleep(time in milliseconds)"

I totally disagree with using the Sleep command. This may make it run fine on one machine but will totally mess things up on different machines that execute the main loop at different speeds.. just my opinion though. I use something similar to the following.


DWORD Time1, Delay=40;
static DWORD Time2=0;
//---------------------------------------------------------------------------------

Time1=GetTickCount();


MAIN LOOP HERE ...



Time2=GetTickCount();
while (((Time2-Time1)) < Delay)
{Time2=GetTickCount();}



My games run at the same speed on all machines unless the machine is real slow then it will run slower...

Rob





clabinsky
clabinsky
quote:
Original post by rhuala

I totally disagree with using the Sleep command.



I totally agree with you rhuala. Mainly because sleeping is a waste of resources and as you said, there is no way of knowing how many milliseconds the compuetr should sleep. But mainly, I repeat it - don''t waste precios time sleeping when you could do all sorts of pre calculations!

Use a timer.



Why make it simple when you can make it sooo nice and complicated?
Why make it simple when you can make it sooo nice and complicated?
IndirectX
IndirectX
This code

while (((Time2-Time1)) < Delay)
{Time2=GetTickCount();}

takes 100% CPU time doing nothing. It''s an incredible waste of resources. If you really want to do something like this, you''ll be much better off with

int Delay = desired_time_between_frames-time_spent_on_current_frame;
if (Delay > 0)
Sleep(Delay);

This will free the processor for other things while you''re waiting. But again, the best approach is to use timers so that people on faster machines can get higher framerates.
---visit #directxdev on afternet <- not just for directx, despite the name
Vanz
Vanz
I think we are talking 2 different things. To me frame rate and game loop speed are totally different. You always want highest possible frame rate of course.

Try playing an old DOS game on your 2.4 Ghz machine, it runs super fast and is uncontrollable, that''s what I am addressing the main game loop speed, nothing to do with frame rates. I am under the understanding he wants his game to run the same speed on all machines...
IndirectX
IndirectX
Ok, but the question is what do you do with the CPU cycles that you don''t use? I showed two alternatives, the first one literally wastes them while the second one allows to use them for other programs (ie, Winamp and download manager).
---visit #directxdev on afternet <- not just for directx, despite the name
Vanz
Vanz
Ya, I think this would work best, especially if you are using multiple threads:

int Delay = desired_time_between_frames-time_spent_on_current_frame;
if (Delay > 0)
Sleep(Delay);

I haven''t tried it so I''m not sure if Sleep will execute other threads while timing out the sleep... I''ll just have to take your word for it

Rob
IndirectX
IndirectX
quote:
Original post by rhuala
I''m not sure if Sleep will execute other threads while timing out the sleep...

It certainly will.
---visit #directxdev on afternet <- not just for directx, despite the name
Coderwannabe
Coderwannabe
I think you''re all going the wrong way here to make a game run with the same speed on every machine.
my sugestion is not doing a delay, since that will limit framerate, but instead make every speed, every screen motion depend on how much time has passed.

for instance, if you multiply a speed with the ellapsed time, in milliseconds, from the previous frame to the current one, and only then add that speed up to the position, you will make speed (and, thus, position) dependant on time, and it will move the same amount of space units, per one time unit.

delaying technique, or frame-rate limiter, can decrease the overall smoothness of a game
time dependent changes, instead of frame dependent changes, are, in my opinion, the smart way

*kiss*
Boop Boop Beedeebee Boop!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.