#include "timer.h"
LARGE_INTEGER timerLast;
LARGE_INTEGER timerThis;
LARGE_INTEGER timerFreq;
int timerReady = 0;
void TimerPrep(void)
{
QueryPerformanceFrequency(&timerFreq);
QueryPerformanceCounter(&timerThis);
timerReady = 1;
}
LONGLONG TimerTick(void)
{
if(timerReady == 0) {return 0;}
// LAST = THIS so that THIS can be updated and the difference returned
timerLast = timerThis;
// Update THIS
QueryPerformanceCounter(&timerThis);
// Return the Difference
return ((timerThis.QuadPart - timerLast.QuadPart) / timerFreq.QuadPart);
}
But i am battling to work with and display the LONGLONG used in this function and TBH i dont like this code because i'm continually having to mess around with it. Is there any very easy to use high resolution timing header available? I really need a high resolution timer without the fuss :-(
Any input would be appreciated. Thanks :-)
Performance Timing
About a year ago when i was trying to write game without any knowlege about the way C works, i used a performance timer that i got working (and wrote myself) ... posted below:
This doesn't answer your question, but I've heard that QueryPerformanceCounter/QueryPerformanceFrequency shouldn't be used anymore (the timer frequency changes mid-program). I could easily be wrong on this, so I'd wait for someone else to confirm it.
Quote:Original post by Daniel Miller
This doesn't answer your question, but I've heard that QueryPerformanceCounter/QueryPerformanceFrequency shouldn't be used anymore (the timer frequency changes mid-program). I could easily be wrong on this, so I'd wait for someone else to confirm it.
The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists. The frequency cannot change while the system is running.
Quote:Original post by Daniel Miller
This doesn't answer your question, but I've heard that QueryPerformanceCounter/QueryPerformanceFrequency shouldn't be used anymore (the timer frequency changes mid-program). I could easily be wrong on this, so I'd wait for someone else to confirm it.
This is mostly an issue with laptops, I believe. The processors can actually slow themselves down in order to conserve energy, but Windows queries the frequency once at startup and just remembers the value.
To the OP: I am unaware of anything that would help. I believe VC++ can automagically print LONGLONG values using cout, but I don't recall for sure. If not, create a string just like any other integer type. [hint: cout << x%10; x /=10...there are gotcha's, but that's a start]
CM
Quote:Original post by Conner McCloudQuote:Original post by Daniel Miller
This doesn't answer your question, but I've heard that QueryPerformanceCounter/QueryPerformanceFrequency shouldn't be used anymore (the timer frequency changes mid-program). I could easily be wrong on this, so I'd wait for someone else to confirm it.
This is mostly an issue with laptops, I believe. The processors can actually slow themselves down in order to conserve energy, but Windows queries the frequency once at startup and just remembers the value.
To the OP: I am unaware of anything that would help. I believe VC++ can automagically print LONGLONG values using cout, but I don't recall for sure. If not, create a string just like any other integer type. [hint: cout << x%10; x /=10...there are gotcha's, but that's a start]
CM
'eh ... i dont use VC++ and its written in C, so printf is how its all being displayed.
A lot of people have games that use timers, does anyone wanna share exactly which one they used?
Thanks for you input you two :-)
Ok, so i found a solution. Casting the LONGLONG stored in the _LARGE_INTEGER's QuadPart (man, that sounds wierd) to a DOUBLE which is really just a 8 byte integer where the second 4 bytes allow for decimal precision, you can do the math and return a float :-) which is much easier to use than a LARGE_INTEGER :-)
Anyways, posted below is a simple example on how to get this beast rolling.
Comments or improvements on this are appreciated :-) Thanks.
Anyways, posted below is a simple example on how to get this beast rolling.
#include <stdio.h>#include <stdlib.h>#include <windows.h>LARGE_INTEGER timerLast;LARGE_INTEGER timerThis;LARGE_INTEGER timerFreq;int timerReady = 0;/* * * */void TimerPrep(void){ QueryPerformanceFrequency(&timerFreq); QueryPerformanceCounter(&timerThis); timerReady = 1;}/* * * */float TimerTick(void){ if(timerReady == 0) {return 0;} // LAST = THIS so that THIS can be updated and the difference returned timerLast = timerThis; // Update THIS QueryPerformanceCounter(&timerThis); // Return the Difference return (((double)timerThis.QuadPart - (double)timerLast.QuadPart) / (double)timerFreq.QuadPart);}/* * * */int main(int argc, char *argv[]){ float laa; TimerPrep(); system("PAUSE"); laa = TimerTick(); printf("Took %f\n", laa); //printf("%d %d", sizeof(int), sizeof(double)); system("PAUSE"); return 0;}
Comments or improvements on this are appreciated :-) Thanks.
Quote:Original post by Conner McCloudQuote:Original post by Daniel Miller
This doesn't answer your question, but I've heard that QueryPerformanceCounter/QueryPerformanceFrequency shouldn't be used anymore (the timer frequency changes mid-program). I could easily be wrong on this, so I'd wait for someone else to confirm it.
This is mostly an issue with laptops, I believe. The processors can actually slow themselves down in order to conserve energy, but Windows queries the frequency once at startup and just remembers the value.
So it's not an issue with laptops. :)
It's guaranteed not to change, *except* some motherboards have a bug that lets the QPC suddenly skip ahead by a large amount.
There's a MSDN article about it somewhere...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement