Skip to main content
GameDev.net gamedev.net
🔒 Locked

Another Time Question...

Started by Gamester3333 Feb 27, 2006 at 10:25 PM 9 replies 800+ views
Original Post
Gamester3333
Gamester3333
Ok, I think I understand the separation of game code and draw code, but my game still runs funny... Basically, what I want to do is stall the processor until a certain amount of time has passed, then run the whole game cycle, stall, run cycle, stall, ect. I figure, if the processor is only doing one short thing, it will run really fast, at about 500+ internal FPS, then it will be very accurate as a timer, so when I run the code, it will run at a steady rate, both internal and external. Any suggestion on how to do this? I am making a real time combat oriented RPG (Please don't flame me, I know how to do it). I have almost everything done, but it runs really fast on my comp, and really slow on my school comp...
Auron
Auron
Not knowing anything about your implementation language, intended platform or anything the like, there's only so much I can advise here.

First, you definitely don't want to artificially stall your game by running some small irrelevant task. It sounds like you want to pull something akin to the old busy loop trick, where you write some small chunk of code that just does some math, and run it a huge amount of times, checking for how long that takes, then using that to stall.

DON'T DO THIS! It will probably choke up your computer since your program is running just to stall itself.

There are two solutions: one is to create a timer that forces frame updates however often per second you please (commonly people go for 60 FPS here), and does not let you do more than that even if the computer is fast enough to do it. You can do this with the system time functions available in almost every language/environment, by waiting until some fraction of a second (such as 1/60th) has passed since the last update and using whatever calls are appropriate for yielding to the operating system until that occurs. This will regulate the speed for you on a faster computer, but on a slower computer it will still run too slowly to keep up with this. You could then balance this with frame skipping, whereby the game will only perform display updates (but still perform all other internal updates at the desired FPS) as often as it can. This way your app will progress at the same rate regardless of what computer it's run on.

A more commonly used approach to this though, is to use a time-based update scheme, where anything relating to animation or progression with time, will occur based on the amount of time elapsed since the last frame was processed, and not occur simply because it's a new frame. So if you're looking for a sword swing that lasts one second, you do some mathemagic (usually in the form of linear interpolation) to decide how to display the sword swing if an arbitrary fraction of a second has passed regardless of what it looked like the previous frame. This will also cause the game to run at the exact same speed on all computers, but has the added bonus that it won't cause any hindrance to your game if you run it on a faster computer.

Look for an article here on Framerate-Independent Updates. It should give a brief explanation of the latter solution I just explained.

-Auron
Omega147
Omega147
Auron's right; don't stall the processor, just let it run and only update after certain amounts of time. However, be careful how you handle this, because it's usually not best to apply such a technique to everything; things like mouse movement or keyboard input in an rpg you may want to process faster than you're drawing your scene.
simon10k
simon10k
I do something like this:
while(msg.message != WM_QUIT){    if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))	        DispatchMessage(&msg);	    if(!Timer(16))        continue;    DoFrame();			}//edit: which is pretty much the same aswhile(msg.message != WM_QUIT){    if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))	        DispatchMessage(&msg);	    if(Timer(16))        DoFrame();			}	


This allows your program to process messages as fast as your processor allows, you have to define the timer yourself though, it checks if a certain time has elasped if so, returns true, else false.

[Edited by - simon10k on February 28, 2006 5:03:25 AM]
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Demus79
Demus79
This time thing pops up like every week, should there be a place for it in some forum FAQ so that it doesn't have to be explained every week again and again ?

I wouldn't do like Simon10k does (ie. doing some timer thing for some arbitrary time).
simon10k
simon10k
Quote:
Original post by Demus79
I wouldn't do like Simon10k does (ie. doing some timer thing for some arbitrary time).


What would you do?

-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Demus79
Demus79
Quote:
Original post by simon10k
Quote:
Original post by Demus79
I wouldn't do like Simon10k does (ie. doing some timer thing for some arbitrary time).


What would you do?


check http://www.gamedev.net/community/forums/topic.asp?topic_id=378258

It has all the answers
simon10k
simon10k
Quote:
Original post by Demus79
I wouldn't do like Simon10k does (ie. doing some timer thing for some arbitrary time).


Sorry if this sounds stupid, I'm not sure what you mean by "doing some timer thing for some arbitrary time" I even looked arbitrary up in the dictionary :)

The timer logic would work like:
if(time has elapsed)//if time elapse return true    return true;return false; //else return false//and not...while(time has not elapsed); //wait in loop until time elapse
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Genjix
Genjix
There are special timer functions to pause the processor without running a spinlock- use them instead as they don't eat up all the CPU cycles.
simon10k
simon10k
Why would you want to pause the processor? and wouldn't pausing the processor freeze the cpu cycles. I cant see how this would help unless it was a multi-threaded app. If you are wanting your game to run at optimal speed you want the messages to be processed as they come in not after a brief pause.
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Gamester3333
Gamester3333
Thank you! I don't truly understand any of this 100%, but I'll give the sources a try.

Oh yeah, also, I am using C++ with allegro.

Topic Locked

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

Sign in to reply to this topic.