C++ OpenGL Game Timer
I am attempting to make a simple g开发者_如何转开发ame using C++ and OpenGL and I haven't been able to find any good tutorials on creating a simple tick or a timer system for a game i also would like to use an little external librarys as possible. Any help is appreciated. I am using Win32 and C::B IDE...MingW complier.
Have you tried:
void glutTimerFunc( unsigned int msecs, void (*func)(int value), value);
Check this pages:
http://wiki.allegro.cc/index.php?title=Timers#How_to_use_them.3F
http://www.koonsolo.com/news/dewitters-gameloop/
Maybe you can get more answers on Gamedev Stackexchange.
Take a look at boost::date_time
.
You could try QueryPerformanceCounter and QueryPerformanceFrequency if you want it ultra-precise.
Edit: I think the docs are pretty sufficient, you get the count of timer ticks and the timer's frequency. But anyway, here's an example:
Not tested, but should give you the idea.
int main(int argc, char *argv[])
{
LARGE_INTEGER freq;
LARGE_INTEGER start;
LARGE_INTEGER current;
double t;
QueryPerformanceFrequency(&freq); // initialize frequency
QueryPerformanceCounter(&start); // initialize start time
while (!terminated) // game main loop
{
QueryPerformanceCounter(¤t); // get current time
t = (end.QuadPart - start.QuadPart) / (double)freq.QuadPart; // transform to seconds
/* do stuff here with t */
}
return 0;
}
You could also use any 64-bit integer type instead of LARGE_INTEGER
, but this is how they intended it.
精彩评论