🔒 Locked
handling FPS
Advertisement
Original Post
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!!!"
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.
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.
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.
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.
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
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
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!!!"
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!!!"
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
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
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
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
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.
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.
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
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.
Advertisement