2013-08-23 65 views
1

我试图让我的FPS显示在窗口标题,但我的程序只是没有它。在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?或者是什么 ?

+1

请停止引用函数*返回void *作为“空白”。没有“空白”这样的东西。他们被称为“功能”(没有返回,也就是无效)。 – datenwolf

回答

2

总是有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()来得到一个以NULL结尾的字符串来传递给glfwSetWindowTitle()

2

你有没有考虑过这样的事情?

 

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

在[Documentation](http://www.glfw.org/docs/latest/group__time.html)中,它表示:可以从辅助线程调用此函数。 那么,这个二级循环会在哪里?这让我猜测它不应该是主要的。 –

+0

使用与绘制线程不同的线程来测量FPS是没有多大意义的。你想在每个帧的开始/结束处增加'nbFrames'计数器,这通常在主线程主循环迭代的开始/结束处。 –

+0

我明白了,我完全同意你的看法。但是,为什么:“这个函数可能会从辅助线程调用”? –

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),而且还会让你“毫秒每帧”来代替,最有可能的16.666如果你是在60 fps。