Jump to content
  • Advertisement
Sign in to follow this  

Realtime FPS counter

This topic is 2975 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!