View Full Version : Effective Time-Keeper

11-28-2002, 02:54 PM
Hi dudes,
I am trying to create a simulation program, in that i need an effective time keeping using the glutTimerFunc for a 30fps.

can i call this timer function from my RenderScene function?

if possible try to explain me how it works? i mean for 30fps it take 33millisec to wait n return to the timerfunction to execute.

the objective of my program is to create a driving simulation with rain effect. here droplet is fed into the windowpane and it begin to flow down. i wrote the code for everything but the timer not function properly.

if any clever head like 2 gimme some idea?

regards http://www.opengl.org/discussion_boards/ubb/wink.gif

11-28-2002, 09:01 PM
You should get time in miliseconds and compare (time==n*(1/33))?proceed:sleep; But I'm working with MSVC++ 6.0 and I havn't found way how to get time in miliseconds & get sleep to work, it seems that computer simply hangs http://www.opengl.org/discussion_boards/ubb/frown.gif If you'll find out about those functions, please let me know.

11-29-2002, 05:39 AM
thanx dude... will let u know, if it works fine. http://www.opengl.org/discussion_boards/ubb/wink.gif

12-11-2002, 10:14 PM
If guys ready to gimme suggestion regarding the timer function, pls do it so...

thanx. http://www.opengl.org/discussion_boards/ubb/smile.gif

12-11-2002, 10:44 PM
I think that I've found solution -> www.gametutorials.com (http://www.gametutorials.com) There was somthing about time based movement, unfortunately it uses additional lib.
Is there realy no way to record milisecond in MSVC. like time.ti_hund in Borland Builder (HATE THAT THNG) http://www.opengl.org/discussion_boards/ubb/mad.gif

12-12-2002, 02:13 AM
GLFW (http://hem.passagen.se/opengl/glfw/) - sub microsecond resolution. Check the source for how it's done (lib/win32/time.c).

Almost all example programs that are included use time-based movement, and some (which use differential equations to propagate movement) also have special handling for low frame rates (which can lead to unstable simulations).

It's quite simple, basically:

#define MAX_DELTA_T 1.0e-3 // Maximum propagation = 1 ms

double t, t_old, dt_total, dt;

t = glfwGetTime();
dt_total = t - t_old;
t_old = t;
while( dt_total > 0.0 )
dt = dt_total > MAX_DELTA_T ? MAX_DELTA_T : dt_total;
dt_total -= dt;
UpdateState( dt );

...where UpdateState is your physics propagation, collision detection etc. (NO rendering!)

In this example, we will propagate the simulation exactly the amount of time that has passed since we last updated the state (=realtime). If the elapsed time between two rendered frames is too large (limit here is 1 ms), then we iterate in 1 ms steps until we have completed dt_total seconds. That way we avoid an unstable simulation when the system can not maintain a high enough rendering speed, like some dude trying to run the simulation in 1600x1200 fullscreen with software OpenGL rendering (=zero point nought FPS).

Note that glfwGetTime() returns the time in seconds as a double precision floating point value. The timer resolution is usually better than 1 us. Bonus: it's portable (!)

[This message has been edited by marcus256 (edited 12-12-2002).]

12-12-2002, 04:42 AM
If you just want Win32 functions to get milliseconds, you can look at timeGetTime, or the more accurate functions QueryPerformanceCounter and QueryPerformanceFrequency.

For a more portable method, you can use ftime(), which uses a timeb struct that contains seconds and milliseconds. I don't think that the accuracy of this method is that great, though.