在GLFW窗口标题中显示FPS?

Jam*_*ung 1 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;
     }
}
Run Code Online (Sandbox Code Playgroud)

我也希望它在版本之后

window = glfwCreateWindow(640, 480, GAME_NAME " " VERSION " ", NULL, NULL);
Run Code Online (Sandbox Code Playgroud)

但是我不能只将void ii转换为char?或者是什么 ?

mch*_*son 5

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;
     }
}
Run Code Online (Sandbox Code Playgroud)

请注意,它cout << 1000.0/double(nbFrames) << endl;不会为您提供“每秒帧数”(FPS),而是为您提供“每帧毫秒数”,如果您以60 fps的速度运行,则很可能为16.666。


gen*_*ult 3

总有一个stringstream窍门:

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

或者boost.lexical_cast

您可以使用std::string::c_str()来获取以 null 结尾的字符串以传递给glfwSetWindowTitle().