Setting up a timer : infinite loop???

Started by
6 comments, last by guepe 18 years, 8 months ago
I'm trying to set up a timer in my app, here is the declaration of const. :
double debut, fin,freq;								//for timer : start, end, frequency...
	float delta_time;	//delta in sec. between start/end

another const. is fps, a float. and here is how i implemented the timer :
QueryPerformanceCounter((LARGE_INTEGER*)&debut); //capture the timer before drawing 
			do
			{
				if(active && !DrawGLScene())
					done = true;
				QueryPerformanceCounter((LARGE_INTEGER*)&fin); //capture the timer after drawing
				delta_time = (fin-debut)/QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
			}while(delta_time==0);
			fps=1/delta_time;

it compiles with a warning : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data in fact, the app freeze and stay in the infinite in the do..while loop... i suppose it's a type problem - see warning - but i don't know what to do : i tried many things, but at the best the fps is a small 0
Advertisement
The first problem I see is that you cast the pointer to the double fin to a LARGE_INTEGER pointer. This will result in garbage beeing stored in fin. So the varaibale you pass as argument to QueryPerformanceCounter must be of type LARGE_INTEGER. The same holds for QueryPerformanceFrequency.
But if i define fin, debut, freq to LARGE_INTEGER, i can't do the operation : fin-debut, because :

error C2676: binary '-' : 'union _LARGE_INTEGER' does not define this operator or a conversion to a type acceptable to the predefined operator

how can i handle the results of QueryPerformanceCounter and QueryPerformanceFrequency?

[edit] i've corrected a line that wasn't good :

QueryPerformanceCounter((LARGE_INTEGER*)&debut); //capture le timer avant dessin			do			{				if(active && !DrawGLScene())					done = true;				QueryPerformanceCounter((LARGE_INTEGER*)&fin); //capture le timer apres dessin				delta_time = (fin-debut)/freq;  //changed that line			}while(delta_time==0.0f);			fps=delta_time;
LARGE_INTEGER is a union. You should be able to use the QuadPart member to do calculations.
Quote:LARGE_INTEGER
The LARGE_INTEGER structure is used to represent a 64-bit signed integer value.

typedef union _LARGE_INTEGER {
struct {
DWORD LowPart;
LONG HighPart;
};
LONGLONG QuadPart;
} LARGE_INTEGER;

Members
LowPart
Specifies the low-order 32 bits.
HighPart
Specifies the high-order 32 bits.
QuadPart
Specifies a 64-bit signed integer.
Remarks
The LARGE_INTEGER structure is actually a union. If your compiler has built-in support for 64-bit integers, use the QuadPart member to store the 64-bit integer. Otherwise, use the LowPart and HighPart members to store the 64-bit integer.



great....

I tried to declare my vars. in __int64..
works fine with that code :
QueryPerformanceCounter((LARGE_INTEGER*)&debut); //capture le timer avant dessin			do			{				if(active && !DrawGLScene())					done = true;				QueryPerformanceCounter((LARGE_INTEGER*)&fin); //capture le timer apres dessin							}while((fin-debut)*1.0/freq==0.0);


but i can't store the result of that operation : (fin-debut)*1.0/freq, because if it's declared as a float, it simply doesn't mean anything (a float is too short for it, so i have a negative numbre :-( ), so i can't determine the fps, the time time that pass painting the frame... :-(
any idea?

thanks a lot !
i managed to print "something" at the screen with that code (all vars. are int_64)

do			{				if(active && !DrawGLScene())					done = true;				QueryPerformanceCounter((LARGE_INTEGER*)&fin); //capture le timer apres dessin				delta_time = (fin.QuadPart-debut.QuadPart);			}while(delta_time==0);

if i print delta_time "as is" i have 249/250 ...
but i have to divide it by freq, initialised by a QueryPerformanceFrequency, and it simply return 0... even if i multiply delta_time by 1000000000 (!)

What can i do??? i really need that stuff...
thx.
you need something like this
double cTimeManager::GetTime(){	QueryPerformanceCounter(&this->QPCret);	return((double)(this->QPCret.QuadPart)/this->QPFret.QuadPart);}


using
LARGE_INTEGER QPCret;
LARGE_INTEGER QPFret;

and calling

QueryPerformanceFrequency(&QPFret);

at initilisation.

Hope that helps:)

EDIT:the "this->" are because ive just ripped this out of my timer class:)
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
thanks a lot !!!

it's working, i have just a small bug but i think i'll manage with it ;-)

yaouuhh it's working !!!!!!!!

you're my hero ;-)

This topic is closed to new replies.

Advertisement