Skip to main content
gamedev.net
🔒 Locked

handling FPS

Started by Gilzu 12 replies 2.2k views
Original Post
Gilzu
Gilzu
see, i''m using a timer on my game to draw&flip. but as i see it, its not the optimal method (not that i know of others) due to the different speed of each computer (faster ones need small interval and slower ones needs faster) suggestions? Gil Micro$oft beta testing: "Does the splash screen works? SHIP IT!!!"
[email=gil@gilzu.com]Gil Zussman[/email]Check out Goose Chase, Coming this fall!http://www.gilzu.com
Julio
Julio
don''t lock your frame rate. keep track of the starting frame''s time and the ending frame''s time. get the difference between the two and use that for your moving distance like this:
Delta=StartTime-EndTime

object.x=Delta*0.01f;
object.y=Delta*0.01f;

you can change the second parameter to anything you like. It will move the same speed on all machines. Check out my timer class on my page for more reference.



How many Microsoft employees does it take to screw in a light bulb?
None, they just declare drakness as a new standard.
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
Gilzu
Gilzu
I still dont understand...
i know that the fps may vary from time to time
and to do what you proposed, i need to have linar
program, not event driven like windows''s....


gil
[email=gil@gilzu.com]Gil Zussman[/email]Check out Goose Chase, Coming this fall!http://www.gilzu.com
Julio
Julio
yes, the distance the objects will move will remain constant. if the frame draws slower, (takes more time), then more will be taken off of that time when it''s multiplied by 0.01. when the frame rendering time is fast, less distance is taken off when multiplied by 0.01. example:
100 ms * 0.01=1 -difference=99
10 ms * 0.01=.1 -different=9.9



How many Microsoft employees does it take to screw in a light bulb?
None, they just declare drakness as a new standard.
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
simon_brown75
simon_brown75
Or to put it more simply, scale object movements and rotations (including the camera, if we're talking 3d) by the current frame time. Windows applications are event driven but that doesn't mean you should write event driven DirectX programs.

You should be drawing a new frame whether an event has occured or not.

Edited by - simon_brown75 on August 3, 2001 7:26:35 PM
Gilzu
Gilzu
let me see if i got you guys straight:

i need to draw by event?
i.e. mouse move

ok, with you so far, np.


BUT
i have this HeartBeat function,
which calculate the next screen
for all objects in tha game (2d btw)


what should call it?
a WM_TIMER command? i cant change its interval,
and if i got you correctly, i should send my
heart beat how much time has passed? (the interval)
but the time passed here is constant! and if i want
the fps to change, i need to change the interval....


gosh, i lost smting here.
someone, help?

Gil

Micro$oft beta testing:
"Does the splash screen works? SHIP IT!!!"
[email=gil@gilzu.com]Gil Zussman[/email]Check out Goose Chase, Coming this fall!http://www.gilzu.com
Gilzu
Gilzu
i wouldnt of had that problem if i didnt use windows... (

again, how to do that in windows?

Gil

Micro$oft beta testing:
"Does the splash screen works? SHIP IT!!!"
[email=gil@gilzu.com]Gil Zussman[/email]Check out Goose Chase, Coming this fall!http://www.gilzu.com
phueppl1
phueppl1
No...

You should always redraw you frame...

inorder that the movement is the same on fast and slower machines, use the code Julio mentioned above...

It''s framerate independent... you scale your movement down so that it fits the Framerate..

cya,
Phil


Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states
Visit Rarebyte! and no!, there are NO kangaroos in Austria (I got this question a few times over in the states ;) )
DarkTerrax
DarkTerrax
Just to make sure this is as clear as possible for you:

You can call your heartbeat function as often as you can (giving you the best frame-rate possible on that machine).

BUT, if you are calling this function as often as you can and you are updating positions of objects in your world in such a method as: ObjectX = ObjectX + SpeedX, then you will have a problem because people on faster machines will execute this function more times per second then people with slower machines, so that means objects in their game will be moving faster. To compenstate for this you must instead store the object''s speed as a velocity (distance/second). This way, in each call to your main game loop you find out how long it has been from the last call to the game loop, and move your sprites accordingly.

For instance, if for some horrible reason your game loop only executed twice per second on my 486, and your movement velocity is 10 pixels/second. your code would look like:

ObjextX = ObjectX + (timeElapsed * VelocityX);

Since timeElapsed is .5 seconds (2 times per second), and velocity is 10, the your loop will move your object 5 pixels. Which is correct. If someone elses computer did the function 10 times a second, then timeElapsed would be .1 second, and the result would be moving the object 1 pixel 10 times a second.

Is this clear?

So you don''t need to use windows timers, or any sort of message loop, just store your movement parameters as velocities, and keep track of time between each game loop call.


Hope this helps,
-Terrax
Gilzu
Gilzu
Thanks to Terrax i got the idea.
maybe because it''s 4:13 AM here and i only
had 2 hrs of sleep last night (day?)

anyway, fully understood.
thanks again

Gil



Micro$oft beta testing:
"Does the splash screen works? SHIP IT!!!"
[email=gil@gilzu.com]Gil Zussman[/email]Check out Goose Chase, Coming this fall!http://www.gilzu.com
DarkTerrax
DarkTerrax
Anonymous Poster:

I think having a fixed time game loop can get a bit sticky. The main reason is lets say you want your loop to execute ever 30ms. You can get the time at the top of your game function, then retrieve the time at the bottom of your function and do the following:

timeElapsed = GetCurrentTime() - startFunctionTime;
if (timeElapsed < 30)
Sleep(30-timeElapsed);

With this method if your game loop gets over in 5 milliseconds, then it will sleep for 25, so your loop will execute ever 30 milliseconds no matter what.

The problem is, what if I''m over here with your program on my POS 386, and I can do the main game function in 40 milliseconds, what now?

Also, you get Joe Shmoe with his 1.5 gHz computer and he can only get 30 fps because your limiting his frame-rate.
KalvinB
KalvinB
You have frames displayed per second and game loop executions per second.

Instead of using while or sleep or seperate threads, just use ifs. It takes two ifs to lock the displayed frames per second while allowing the game loop to be executed as fast the the processor will allow.

Tombstone displays at most 30fps (drops if necessary) but runs through the game loop 264,000 times per second.

Ben

Icarus Independent

Jump Down The Rabbithole

Topic Locked

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

Sign in to reply to this topic.