Sign in to follow this  
TutenStain

Realtime FPS counter

Recommended Posts

As the title suggest. I need/want an FPS counter for my current project. I want one like FRAPS that updates almost instantly. I have tried an tried but I have no idea how to do it. Any suggestions? Using C++, SDL and OpenGL.

Share this post


Link to post
Share on other sites
SDL has some timer stuf iirc.

but a timer is easy really.

Quote:


struct Stopwatch
{
public:
Stopwatch()
{
m_startTime = 0;
m_stopTime = now();
}

void start()
{
m_startTime = now();
}

bool started() const
{
return (m_startTime > m_stopTime);
}

void stop()
{
m_stopTime = now();
}
u_long ticks(u_long period)
{
// not started yet. start.
if(!started())
{
start();
}

u_long time_elapsed = elapsed();

// restart ticker.
if(time_elapsed > period)
{
start();
}

return time_elapsed / period;
}

u_long elapsed() const
{
return (started())? now() - m_startTime : now() - m_stopTime;
}

u_long reached(u_long limit)
{
return started() && (elapsed() >= limit);
}

u_long now() const
{
static LARGE_INTEGER frequency = { 0, 0 };
static LARGE_INTEGER count = { 0, 0 };

if(frequency.QuadPart == 0)
{
if(QueryPerformanceFrequency(&frequency) == false)
return 0;
}

if(QueryPerformanceCounter(&count) == false)
return 0;

u_long now = (count.QuadPart / (frequency.QuadPart / (1000 * 1000)));

// microseconds
return now;
}

u_long m_startTime;
u_long m_stopTime;
};

Share this post


Link to post
Share on other sites
Quote:
Original post by TutenStain
As the title suggest. I need/want an FPS counter for my current project. I want one like FRAPS that updates almost instantly. I have tried an tried but I have no idea how to do it. Any suggestions? Using C++, SDL and OpenGL.


There are two methods of measuring fps: One is instantaneous, and the other is actually counting the number of processed frames per second.


unsigned currentTick = 0;
unsigned lastTick = 0;
unsigned elapsedTicks = 0;

currentTick = lastTick = SDL_GetTicks();

unsigned fpsTimer = 0;
unsigned fpsFrameCount = 0;
unsigned fpsAccurate = 0;
unsigned fpsInstantaneous = 0;

// Game loop:
bool isRunning = true;
while ( isRunning ) {
lastTick = currentTick;
currentTick = SDL_GetTicks();
elapsedTicks = currentTick - lastTick;

float elapsedTime = elapsedTicks / 1000.f;

// Logic ...
// Rendering ...

// For the accurate fps counter, count the actual number
// of frames that were processed in the past second
++fpsFrameCount;
fpsTimer += elapsedTicks;
if ( fpsTimer >= 10000 ) {
fpsAccurate = fpsFrameCount;
fpsFrameCount = 0;
fpsTimer = 0;
}

// For the instantaneous fps counter, count how many
// frames *could* be processed in a second, if all
// frames in that second took exactly "elapsedTime" seconds
// or "elapsedTicks" ms.
if ( elapsedTicks )
fpsInstantaneous = 1000 / elapsedTicks;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
Quote:
Original post by TutenStain
As the title suggest. I need/want an FPS counter for my current project. I want one like FRAPS that updates almost instantly. I have tried an tried but I have no idea how to do it. Any suggestions? Using C++, SDL and OpenGL.


There are two methods of measuring fps: One is instantaneous, and the other is actually counting the number of processed frames per second.

*** Source Snippet Removed ***


That really helped! Thanks a bunch!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this