
FPS Counter
In response to all the requests for a way to calculate FPS I hereby give you a fully working nice and simple Speedometer class.
----------------------------------------------
FILE:Speedometer.h
----------------------------------------------
//////////////////////////////////////////////////////////////////////
//
// Author: Tristan "WytRaven" Ward 2000
// Speedometer.h: interface for the CSpeedometer class.
// Requires links to winmm.lib, dxguid.lib, ddraw.lib
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SPEEDOMETER_H__571FE742_F39D_11D3_8BE4_92DC7A4C1216__INCLUDED_)
#define AFX_SPEEDOMETER_H__571FE742_F39D_11D3_8BE4_92DC7A4C1216__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
class CSpeedometer
{
public:
CSpeedometer();
virtual ~CSpeedometer();
void Update();
void Report(LPDIRECTDRAWSURFACE7 pDDS, int xpos, int ypos);
private:
int StartFrameCount;
int FrameCount;
int FrameRate;
DWORD EndTime;
DWORD StartTime;
};
#endif // !defined(AFX_SPEEDOMETER_H__571FE742_F39D_11D3_8BE4_92DC7A4C1216__INCLUDED_)
----------------------------------------------
FILE:Speedometer.cpp
----------------------------------------------
//////////////////////////////////////////////////////////////////////
//
// Author: Tristan "WytRaven" Ward 2000
// Speedometer.cpp: implementation of the CSpeedometer class.
// Requires links to winmm.lib, dxguid.lib, ddraw.lib
//
//////////////////////////////////////////////////////////////////////
#include
#include
#include
#include
#include "Speedometer.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSpeedometer::CSpeedometer()
{
StartTime = timeGetTime();
EndTime = StartTime;
StartFrameCount = 0;
FrameCount = 0;
FrameRate = 0;
}
CSpeedometer::~CSpeedometer()
{
}
// Print current FPS to chosen surface
void CSpeedometer::Report(LPDIRECTDRAWSURFACE7 pDDS, int xpos, int ypos)
{
HDC hdc;
char pString[30];
sprintf(pString,"FPS : %d", FrameRate);
pDDS->GetDC(&hdc);
SetBkMode(hdc, OPAQUE);
SetTextColor(hdc, 0x000000);
TextOut(hdc, xpos, ypos, pString, strlen(pString));
pDDS->ReleaseDC(hdc);
}
// Calculate FPS
void CSpeedometer::Update()
{
EndTime = timeGetTime();
if (EndTime - StartTime > 1000)
{
FrameRate = (FrameCount - StartFrameCount) * 1000 / (EndTime - StartTime);
StartTime = EndTime;
StartFrameCount = FrameCount;
}
FrameCount++;
}
To use just place a call to the Report method in your renderer and a call to the Update method in your message loop.
Enjoy.
Please feel free to make any comments on how you like/dislike this but please refrain from commenting unless youve acctually tried it.
wytraven@kik.net

[email=wytraven@kik.net]wytraven@kik.net[/email]There is nothing real outside our perception of reality. Is there?
Sorry the board screwed up my includes, they should be as follows:
#include >windows.h<
#include >mmsystem.h<
#include >ddraw.h<
#include >stdio.h<
#include "Speedometer.h"
><'s are reversed to trick the board
I would also like to point out that I am a newbie to both C++ and DirectX, I am teaching myself about both so I don't claim that this is the ultimate way to go about this and I would be gratefull for any pointers to things that could be improved. Thanks.
wytraven@kik.net
Edited by - WytRaven on 3/6/00 6:01:15 PM
#include >windows.h<
#include >mmsystem.h<
#include >ddraw.h<
#include >stdio.h<
#include "Speedometer.h"
><'s are reversed to trick the board

I would also like to point out that I am a newbie to both C++ and DirectX, I am teaching myself about both so I don't claim that this is the ultimate way to go about this and I would be gratefull for any pointers to things that could be improved. Thanks.
wytraven@kik.net
Edited by - WytRaven on 3/6/00 6:01:15 PM
[email=wytraven@kik.net]wytraven@kik.net[/email]There is nothing real outside our perception of reality. Is there?
why bother creating a class for this? are you going to be creating many FPS objects? that seems like a lot of work just to calculate the framerate. i do it in about four or five lines in my engine...
_________________Gecko___
Gecko Design
_________________Gecko___
Gecko Design
_________________Gecko___Gecko Design
uhh, call me stupid, but I didn''t quite understand it. Maybe I''m just tired.
Anyway the part I didn''t understand was this:

// Calculate FPS
void CSpeedometer::Update()
{
EndTime = timeGetTime();
if (EndTime - StartTime > 1000)
{
FrameRate = (FrameCount - StartFrameCount) * 1000 / (EndTime - StartTime);
StartTime = EndTime;
StartFrameCount = FrameCount;
}
FrameCount++;
}
If anyone could explain this for me, I would be very greatful!
"Remember, I'm the monkey, and you're the cheese grater. So no fooling around."
-Grand Theft Auto, London
D:
void CSpeedometer::Update()
{
//Get current time
EndTime = timeGetTime();
//1000 milliseconds == 1 second, so see if one second has
//elapsed since last time we checked (StartTime)
if (EndTime - StartTime > 1000)
{
//if one second has gone by, find out how many frames we have
//rendered in that second
FrameRate = (FrameCount - StartFrameCount) * 1000 / (EndTime - StartTime);
//reset StartTime for the next update
StartTime = EndTime;
//reset StartFrameCount for the next update
StartFrameCount = FrameCount;
}
//Add another frame
FrameCount++;
}
_________________Gecko___
Gecko Design
{
//Get current time
EndTime = timeGetTime();
//1000 milliseconds == 1 second, so see if one second has
//elapsed since last time we checked (StartTime)
if (EndTime - StartTime > 1000)
{
//if one second has gone by, find out how many frames we have
//rendered in that second
FrameRate = (FrameCount - StartFrameCount) * 1000 / (EndTime - StartTime);
//reset StartTime for the next update
StartTime = EndTime;
//reset StartFrameCount for the next update
StartFrameCount = FrameCount;
}
//Add another frame
FrameCount++;
}
_________________Gecko___
Gecko Design
_________________Gecko___Gecko Design
Thanks for adding in the comments Gecko, I really should learn to comment my code shouldn''t I 
There are 2 reasons I choose to use a class:
1. It''s nice and neat and easy to add to any project.
2. Because I am new to C++ and wanted to get practice making classes.
Good enough excuse?
wytraven@kik.net

There are 2 reasons I choose to use a class:
1. It''s nice and neat and easy to add to any project.
2. Because I am new to C++ and wanted to get practice making classes.
Good enough excuse?

wytraven@kik.net
[email=wytraven@kik.net]wytraven@kik.net[/email]There is nothing real outside our perception of reality. Is there?
March 07, 2000 11:03 AM
Hi. I use the API to calculate the FPS. I always calculate it this way:
First you have to have four global variables:
LARGE_INTEGER Frequency,NewTime,LastTime;
double FPS;
Call
QueryPerformanceFrequency( &Frequency )
one time.
Then you always have to do like this in your Update - function or whatever you call it:
LastTime = NewTime;
QueryPerformanceTimer( &NewTime );
//FPS - calculation
FPS = (double)Frequency.QuadPart /
((double)NewTime.QuadPart - (double)LastTime.QuadPart);
Afterwards you''ve got the FPS in FPS.
But I''ve got a question, too:
How can I synchronize animations the right way if the FPS changes many times in a second.
Please tell me. I REALLY NEED HELP!!!!
my Mail: Centum@gmx.net
Thank you!
First you have to have four global variables:
LARGE_INTEGER Frequency,NewTime,LastTime;
double FPS;
Call
QueryPerformanceFrequency( &Frequency )
one time.
Then you always have to do like this in your Update - function or whatever you call it:
LastTime = NewTime;
QueryPerformanceTimer( &NewTime );
//FPS - calculation
FPS = (double)Frequency.QuadPart /
((double)NewTime.QuadPart - (double)LastTime.QuadPart);
Afterwards you''ve got the FPS in FPS.
But I''ve got a question, too:
How can I synchronize animations the right way if the FPS changes many times in a second.
Please tell me. I REALLY NEED HELP!!!!
my Mail: Centum@gmx.net
Thank you!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement