A Simple GLFW FPS Counter

Update – September 2013: Need something that enforces a given frame rate rather than just reports the current frame rate? Try this: https://r3dux.org/2012/12/fpsmanager-a-c-helper-class-for-framerate-independent-movement/

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();
		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
	// 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


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.