timing code

Started by
6 comments, last by Maxamor 16 years, 8 months ago
I want to be able to get two kinds of time values: (1) The "delta time" between two frames. (2) The total time elapsed relative to some base time (say the application start). For (2), I also need to add that I don't want the time when the application is paused to be counted. There seems to be two ways I can implement (2). Use a double and add time incrementally to it every frame, provided the application is not paused: if( !paused ) timeElapsed += timeDelta; Another way is to save the base time when the application starts, and then look at the difference between that time and a later time: baseTime = getTime(); // init at app start ... timeNow = getTime(); timeElapsed = timeNow - baseTime; This second approach will require me to adjust baseTime by the amount of paused time so that paused time is not counted. Is there any preference between these two methods? I naturally like the first for no real reason, but the DXSDK's CDXUTTimer uses the second method, so I'm wondering if there is any good reason.
-----Quat
Advertisement
This is how I'd do it.
(Session = period between pauses)

void begin() {    app_start_time = time();    session_start_time = app_start_time;    session_elapsed = 0;}void pause() {    pause_time = time();    session_elapsed += pause_time - session_start_time;}void resume() {    session_start_time = time();}double get_elapsed() {    return session_elapsed + (time() - session_start_time)}


If you're really pedantic you might want to consider the possibility that the OS for some reason forgets about your app for an extended period of time like 3 secs (I know some background apps running on this XP freeze the PC for about 3s). In which case you can check to make sure time between frames isn't more than a maximum value - but that's being really pedantic.

HTH
Thanks for your reply. I understand how to do it like that. My question was on whether that style had any advantages over just doing this:

if( !paused )
timeElapsed += timeDelta;


-----Quat
If you do it the way you're suggesting, timeElapsed won't really be the time that has elapsed since the start of the program. It will be the aggregate of all time the game has been active. It loses it's purpose. It becomes lonely.

You should keep track of it even when the game is paused and just give yourself a maximum time span, say 250ms. You know, if delta > 250 then delta = 250;

That way, you're giving yourself a realistic length of time for the game to catch up. Otherwise, the game could be paused for... 10 minutes and will really suffer when you unpause.

Does this make sense?
Quote:
If you do it the way you're suggesting, timeElapsed won't really be the time that has elapsed since the start of the program. It will be the aggregate of all time the game has been active.


That's what I want here. It is like a stop watch. It starts at zero. You start it. When the app is paused you stop the watch. When the app starts up again, you start the watch from where you left off.

Quote:
Otherwise, the game could be paused for... 10 minutes and will really suffer when you unpause.


Why is that? The idea is to not do anything when paused and to resume from where it left off.
-----Quat
Then I don't understand how you're making use of a delta time. If the value of the delta time doesn't matter, why have one?

I guess I don't understand why you would want a stopwatch effect instead of an actual time-elapsed effect.
Quote:
I guess I don't understand why you would want a stopwatch effect instead of an actual time-elapsed effect.


Suppose I wish to animate the position of a particle as a function of t (time). If I allowed t to grow when the app is paused, the object would continue to animate while paused.
-----Quat
I think my real question is then are you updating your objects by telling them how much time has elapsed, or what the last delta time is?
if( !paused )timeElapsed += timeDelta;
This will make sure that timeElapsed excludes paused time, but couldn't timeDelta possibly be huge? Is this the desired behavior?

Why not update times while the game is paused, but not pass the info on to the objects? This seems reasonable.

This topic is closed to new replies.

Advertisement