Created
August 9, 2009 00:39
-
-
Save Madsy/164562 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TimeAccum | |
{ | |
public: | |
TimeAccum(int frequency) : time_previous(0), accum_delta_error(0), accum_time(0), time(0) | |
{ | |
/* frequency is updates / sec, for example 30 or 60 fps */ | |
/* almost everything is 15.16Q fixedpoint */ | |
timestep = 65536 / frequency; | |
} | |
void advanceTime() | |
{ | |
int time_current = SDL_GetTicks(); | |
int deltatmp = (time_current - time_previous) * 65536; /* 65536000/s */ | |
time_previous = time_current; | |
/*SDL_GetTicks returns milliseconds (1/1000). We want 1/65536 */ | |
int delta = deltatmp / 1000; | |
int delta_error = deltatmp % 1000; | |
/* save the error (remainder)*/ | |
accum_delta_error += delta_error; | |
if(accum_delta_error >= 1000){ | |
++delta; /* Increase delta by 1, if the accumulated remainders becomes "one whole" */ | |
accum_delta_error -= 1000; | |
} | |
accum_time += delta; | |
while(accum_time >= timestep) | |
{ | |
accum_time -= timestep; | |
time += timestep; | |
think(time); /* remember to divide by 65536 */ | |
} | |
} | |
private: | |
int time_previous; | |
int timestep; | |
int accum_delta_error; | |
int accum_time; | |
int time; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment