Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying FPS in GLFW window title?

Tags:

c++

opengl

glfw

Im trying to get my FPS to display in the window title but my program is just not having it.

my FPS code

    void showFPS()
{
     // Measure speed
     double currentTime = glfwGetTime();
     nbFrames++;
     if ( currentTime - lastTime >= 1.0 ){ // If last cout was more than 1 sec ago
         cout << 1000.0/double(nbFrames) << endl;
         nbFrames = 0;
         lastTime += 1.0;
     }
}

and i want it too go just after the Version here

window = glfwCreateWindow(640, 480, GAME_NAME " " VERSION " ", NULL, NULL);

but i cant just call the void i i have to convert it too a char ? or what ?

like image 877
James Young Avatar asked Dec 06 '22 05:12

James Young


1 Answers

void showFPS(GLFWwindow *pWindow)
{
    // Measure speed
     double currentTime = glfwGetTime();
     double delta = currentTime - lastTime;
     nbFrames++;
     if ( delta >= 1.0 ){ // If last cout was more than 1 sec ago
         cout << 1000.0/double(nbFrames) << endl;

         double fps = double(nbFrames) / delta;

         std::stringstream ss;
         ss << GAME_NAME << " " << VERSION << " [" << fps << " FPS]";

         glfwSetWindowTitle(pWindow, ss.str().c_str());

         nbFrames = 0;
         lastTime = currentTime;
     }
}

Just a note, cout << 1000.0/double(nbFrames) << endl; wouldn't give you "frame-per-second" (FPS), but would give you "milliseconds-per-frames" instead, most likely 16.666 if you are at 60 fps.

like image 94
mchiasson Avatar answered Dec 30 '22 00:12

mchiasson