Displaying FPS in GLFW window title?
Asked Answered
R

3

6

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 ?

Revoke answered 23/8, 2013 at 21:26 Comment(1)
Please stop refering to functions returning a void as just "voids". There is no such thing as "a void". They're called "functions" (returning nothing, a.k.a. void).Slew
O
8
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.

Olio answered 15/2, 2015 at 3:52 Comment(0)
T
3

There's always the stringstream trick:

template< typename T >
std::string ToString( const T& val )
{
    std::ostringstream oss;
    oss << val;
    return oss.str();
}

Or boost.lexical_cast.

You can use std::string::c_str() to get a null-terminated string to pass in to glfwSetWindowTitle().

Tove answered 23/8, 2013 at 21:53 Comment(0)
V
3

Have you considered something like this?


void
setWindowFPS (GLFWwindow* win)
{
  // Measure speed
  double currentTime = glfwGetTime ();
  nbFrames++;

  if ( currentTime - lastTime >= 1.0 ){ // If last cout was more than 1 sec ago
    char title [256];
    title [255] = '\0';

    snprintf ( title, 255,
                 "%s %s - [FPS: %3.2f]",
                   GAME_NAME, VERSION, 1000.0f / (float)nbFrames );

    glfwSetWindowTitle (win, title);

    nbFrames = 0;
    lastTime += 1.0;
  }
}

Vargo answered 23/8, 2013 at 21:54 Comment(4)
In the Documentation, it says: This function may be called from secondary threads. So, where would be this secondary loop? It makes me guess that it should not be the main one.Daisey
It would not make much sense to measure the FPS using a different thread than the drawing thread. You want to increment the nbFrames counter at the beginning/end of every frame, which is usually at the beginning/end of an iteration of the main loop in the main thread.Vargo
I understand, and I completely agree if you. But then, why: "This function may be called from secondary threads"?Daisey
@EduardoReis: Actually, that means that the value does not depend on which thread you call it from. There are certain parts of GLFW that work differently depending on which thread they are called from (glfwCreateWindow (...) for example) will return NULL if called from a different thread than you called glfwInit (...) in.Vargo

© 2022 - 2024 — McMap. All rights reserved.