在 GLFW 窗口标题中显示 FPS?

新手上路,请多包涵

我试图让我的 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);

但我不能只调用 void 我必须将其转换为 char 吗?或者是什么 ?

原文由 James Young 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
1 个回答
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。

原文由 mchiasson 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏