在GLFW窗口标题中显示FPS?

时间:2013-08-23 21:26:02

标签: c++ opengl glfw

我试图让我的FPS显示在窗口标题中,但我的程序却没有。

我的FPS代码

    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;
     }
}

我想要它也只是在版本之后

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

但是我不能称之为虚空我必须将它转换为char?或者什么?

3 个答案:

答案 0 :(得分:4)

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;
     }
}

只是一个音符,cout << 1000.0/double(nbFrames) << endl;不会给你“每秒帧数”(FPS),但会给你“每帧几毫秒”,如果你的速度为60 fps,很可能会给你16.666

答案 1 :(得分:3)

始终存在istringstream诀窍:

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

boost.lexical_cast

您可以使用std::string::c_str()将以空字符结尾的字符串传递给glfwSetWindowTitle()

答案 2 :(得分:3)

你考虑过这样的事吗?


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;
  }
}