opengl 是否在GLFW窗口标题中显示FPS?

mitkmikd  于 2022-11-04  发布在  其他
关注(0)|答案(3)|浏览(305)

我试图让我的FPS显示在窗口标题,但我的程序只是没有它。
我FPS代码

  1. void showFPS()
  2. {
  3. // Measure speed
  4. double currentTime = glfwGetTime();
  5. nbFrames++;
  6. if ( currentTime - lastTime >= 1.0 ){ // If last cout was more than 1 sec ago
  7. cout << 1000.0/double(nbFrames) << endl;
  8. nbFrames = 0;
  9. lastTime += 1.0;
  10. }
  11. }

我也希望它在这里的版本之后

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

但是我不能只叫void,我必须把它也转换成char,或者什么?

rwqw0loc

rwqw0loc1#

  1. void showFPS(GLFWwindow *pWindow)
  2. {
  3. // Measure speed
  4. double currentTime = glfwGetTime();
  5. double delta = currentTime - lastTime;
  6. nbFrames++;
  7. if ( delta >= 1.0 ){ // If last cout was more than 1 sec ago
  8. cout << 1000.0/double(nbFrames) << endl;
  9. double fps = double(nbFrames) / delta;
  10. std::stringstream ss;
  11. ss << GAME_NAME << " " << VERSION << " [" << fps << " FPS]";
  12. glfwSetWindowTitle(pWindow, ss.str().c_str());
  13. nbFrames = 0;
  14. lastTime = currentTime;
  15. }
  16. }

请注意,cout << 1000.0/double(nbFrames) << endl;不会给予你“每秒帧数”(FPS),而是给你“每帧毫秒数”,如果你是60 fps,最有可能是16.666。

展开查看全部
xuo3flqw

xuo3flqw2#

你考虑过这样的事情吗?

  1. void
  2. setWindowFPS (GLFWwindow* win)
  3. {
  4. // Measure speed
  5. double currentTime = glfwGetTime ();
  6. nbFrames++;
  7. if ( currentTime - lastTime >= 1.0 ){ // If last cout was more than 1 sec ago
  8. char title [256];
  9. title [255] = '\0';
  10. snprintf ( title, 255,
  11. "%s %s - [FPS: %3.2f]",
  12. GAME_NAME, VERSION, 1000.0f / (float)nbFrames );
  13. glfwSetWindowTitle (win, title);
  14. nbFrames = 0;
  15. lastTime += 1.0;
  16. }
  17. }
展开查看全部
new9mtju

new9mtju3#

总是有stringstream技巧:

  1. template< typename T >
  2. std::string ToString( const T& val )
  3. {
  4. std::ostringstream oss;
  5. oss << val;
  6. return oss.str();
  7. }

或者boost.lexical_cast
您可以使用std::string::c_str()来取得以null结尾的字串,以传入glfwSetWindowTitle()

相关问题