Advertisement

clock() function

Started by May 10, 2002 09:53 AM
15 comments, last by SonicMouse2 22 years, 9 months ago
I take it no one knows how to make a FPS function?
You basically want to record how long a frame took, convert it to seconds, then divide 1 by this number.

That''s your fps. Be careful not to divide by zero.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Advertisement
Hey,

Just in an attempt to be helpful, here''s two classes I implemented kinda on the fly from this thread and past exp. There are two I came up with, for the 2 major types that have been discussed. Feel free to use them or whatever - you can most likely improve them, but it should get you started (and I tested them; they work reasonably well - at least as an example). I used timeGetTime (remember to link <b>winmm.lib</b>, but you can use whatever suits your needs.

Method 1 (find # of frames over a time interval)

<source>
#include <mmsystem.h>

class clock1{
public:
clock1(unsigned int s) : step(s), frameCount(0), fps(0){
curr = timeGetTime();
prev = curr;
}
~clock1(){};

float GetFPS(){
curr = timeGetTime();
float time = curr - prev;
if (time >= step){
prev = curr;
fps = 1000*(float)frameCount/time;
frameCount = 0;
}
else
frameCount++;
return fps;
};
private:
DWORD curr, prev;
unsigned int frameCount, step;
float fps;
};
</source>

Method 2 (solving the fps for one frame)
<source>
#include <mmsystem.h>

class clock2{
public:
clock2(){
curr = timeGetTime();
prev = curr;
};
~clock2(){};
float GetFPS(){
curr = timeGetTime();
float time = curr - prev;
prev = curr;
if (!time)
time = 1.0f;
return 1000.0f/time;
};
private:
DWORD curr;
DWORD prev;
};
</source>

All you need to do is make a global instance of this class, and stick the GetFPS in your display function. Or you can modify it to work differently.

best,

eriol
Hey,

Sorry about that ... I forgot about using the correct brackets ... I would have tried to edit it, ''cept I forgot my password (haven''t posted here in a while). Well, here''s the code, easier this time.

Method 1 (find # of frames over a time interval)


  #include <mmsystem.h>class clock1{public:	clock1(unsigned int s) : step(s), frameCount(0), fps(0){		curr = timeGetTime();		prev = curr;	}	~clock1(){};	float GetFPS(){		curr = timeGetTime();		float time = curr - prev;		if (time >= step){			prev = curr;			fps = 1000*(float)frameCount/time;			frameCount = 0;		}		else			frameCount++;		return fps;	};private:	DWORD curr, prev;	unsigned int frameCount, step;	float fps;};  


Method 2 (solving the fps for one frame)

  #include <mmsystem.h>class clock2{public:	clock2(){		curr = timeGetTime();		prev = curr;	};	~clock2(){};	float GetFPS(){		curr = timeGetTime();		float time = curr - prev;		prev = curr;		if (!time)			time = 1.0f;		return 1000.0f/time;	};private:	DWORD curr;	DWORD prev;};  


best,

eriol
eriol,

Thanks a million for the code, it works great (I am using the second version). I definitely need a new video card, since I only get 41 fps for a simple rotating cube with some lines through it.

Also, is there any trick to getting the fps counter to slow down, because it is pretty hard to read since it''s jumping around all over the place.
You could try only changing the number you display every so 10 frames or so. If you''re having trouble understanding this I think you could be starting too deep.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Advertisement
I have been trying out FPS counters recently and tried timeGetTime(), getTickCount() and QueryPerformance Counter and frankly I get just about the same result no matter which I use.

The only thing I have observed which appeared different was that getTickCount sometimes returned the same result as when previously checked.

It appeared as though is did not register a change of less than 10 ms. i.e if checked at 4 ms intervals it returned a change since the previos check of 0,0,12,0,20 etc..

Stephen

This topic is closed to new replies.

Advertisement