r3dux.org

A number-pimping side project from the valleys in *NEW* upside-down flavour.

  • Home
  • ABOUT
  • OLD SITE
  • SEARCH
  • FEEDBACK

A Simple GLFW FPS Counter

r3dux | July 7, 2012

I’m fed up of solving the same problem over and over again, so the next time I need some FPS measurements, I’m going to use this…

The Include Requirements

#include <iostream>
#include <string>
#include <sstream>
#include <GL/glfw.h>

The Function

double calcFPS(double theTimeInterval = 1.0, std::string theWindowTitle = "NONE")
{
	// Static values which only get initialised the first time the function runs
	static double t0Value       = glfwGetTime(); // Set the initial time to now
	static int    fpsFrameCount = 0;             // Set the initial FPS frame count to 0
	static double fps           = 0.0;           // Set the initial FPS value to 0.0
 
	// Get the current time in seconds since the program started (non-static, so executed every time)
	double currentTime = glfwGetTime();
 
	// Ensure the time interval between FPS checks is sane (low cap = 0.1s, high-cap = 10.0s)
	// Negative numbers are invalid, 10 fps checks per second at most, 1 every 10 secs at least.
	if (theTimeInterval < 0.1)
	{
		theTimeInterval = 0.1;
	}
	if (theTimeInterval > 10.0)
	{
		theTimeInterval = 10.0;
	}
 
	// Calculate and display the FPS every specified time interval
	if ((currentTime - t0Value) > theTimeInterval)
	{
		// Calculate the FPS as the number of frames divided by the interval in seconds
		fps = (double)fpsFrameCount / (currentTime - t0Value);
 
		// If the user specified a window title to append the FPS value to...
		if (theWindowTitle != "NONE")
		{
			// Convert the fps value into a string using an output stringstream
			std::ostringstream stream;
			stream << fps;
			std::string fpsString = stream.str();
 
			// Append the FPS value to the window title details
			theWindowTitle += " | FPS: " + fpsString;
 
			// Convert the new window title to a c_str and set it
			const char* pszConstString = theWindowTitle.c_str();
			glfwSetWindowTitle(pszConstString);
		}
		else // If the user didn't specify a window to append the FPS to then output the FPS to the console
		{
			std::cout << "FPS: " << fps << std::endl;
		}
 
		// Reset the FPS frame counter and set the initial time to be now
		fpsFrameCount = 0;
		t0Value = glfwGetTime();
	}
	else // FPS calculation time interval hasn't elapsed yet? Simply increment the FPS frame counter
	{
		fpsFrameCount++;
	}
 
	// Return the current FPS - doesn't have to be used if you don't want it!
	return fps;
}

Usage Examples

Call any of these in your main loop…

string windowTitle = "My Lovely App: "; // You might want to have string with window title hanging around...
 
cout << calcFPS() << endl;              // Print the FPS to the console once per second
cout << calcFPS(2.0) << endl;           // Print the FPS to the console every 2 seconds
calcFPS(1.0, windowTitle);              // Update the window title to include the FPS details once per second
calcFPS(2.0, windowTitle);              // Update the window title to include the FPS details every 2 seconds
calcFPS(3.0, "Current FPS: ");          // Update the window title to the string literal "Current FPS: " + the FPS details every 3 seconds

Suggestions?

I think that’s pretty usable and clean – if you’ve got any suggestions I’d really be interested in hearing them – I simply don’t want to re-implement a FPS counter in C++ ever again.

Related posts:

  1. 2D C++ OpenGL/GLFW Basecode
  2. Simple texture loading with DevIL revisited
  3. How To: Create a Simple Fireworks Effect in OpenGL and SDL
  4. Simple OpenGL Keyboard and Mouse FPS Controls
  5. A simple C++/SDL_net chat server & client rewritten
Categories
Coding
Tags
C++, Calculation, Counter, FPS, Frames Per Second, GLFW
Comments rss
Comments rss
Trackback
Trackback
Print This Post Print This Post

« New Order – Age of Consent Fix for the Microsoft Word error “A file error has occurred” while saving »

2 Responses to “A Simple GLFW FPS Counter”

  1. FpsManager – A C++ helper class for framerate independent movement | r3dux.org says:
    December 1, 2012 at 4:43 pm

    [...] wrote only a few months back that I didn’t want to write another piece of FPS code, ever. But this was before I started taking framerate independent movement seriously. In my past [...]

    Reply
  2. Javan says:
    April 7, 2013 at 5:38 am

    This is great!! so simple yet so useful , thanks =D!

    Reply

Leave a Reply

Click here to cancel reply.

Translate

Categories

Archives

Tags

3D ActionScript ActionScript 3.0 Adobe AI Ballarat Bash C++ Class Convert CS4 Effect Error Film Flash FPS GLFW Glitch GLSL Hack How-To install Java Kinect Linux Live Mash-Up Microsoft Motion mount OpenGL Particle Problem PS3 Remix Retro script Slides Sound Ubuntu Video VirtualBox Wii Windows XBox

Gamercard

OpenR3dux

Misc.

Flattr this

RSS Feed

r3dux twitter feed



“To measure up to all that is demanded of him, a man must overestimate his capacities.”

 - Goethe

rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox