2012-01-14 37 views
2

我希望我的游戏能以固定的FPS在我家附近的多台计算机上运行。我有一个简单的问题:我如何调试我当前的FPS调整后调整/调试当前FPS吗?

我正在调节我的FPS像这样:

SDL_Init(SDL_INIT_EVERYTHING); 
const unsigned FPS = 20; 
Uint32 start = SDL_GetTicks(); // How much time (milliseconds) has passed after SDL_Init was called. 

while (true) 
{ 
    start = SDL_GetTicks(); 

    if (1000/FPS > SDL_GetTicks() - start) 
    SDL_Delay(1000/FPS - (SDL_GetTicks() - start)); // Delays program in milliseconds 

我觉得应该去调整自己的FPS。问题是,我如何获得当前的fps?

我试图

std::stringstream fps; 
fps << 1000/FPS - (SDL_GetTicks() - start); 
SDL_WM_SetCaption(fps.str().c_str(), NULL); // Set the window caption 

fps << start/1000; // and vice versa 

但他们都不给我我想要的。

+2

看过这个吗? http://gafferongames.com/game-physics/fix-your-timestep/ – 2012-01-14 08:28:02

+0

不,但它看起来非常有趣。我会读这个。提前致谢。 – evolon696 2012-01-14 08:28:57

回答

1

给该块一个镜头:

while(true) 
{ 
    // render 
    // logic 
    // etc... 

    // calculate (milli-)seconds per frame and frames per second 
    static unsigned int frames = 0; 
    static Uint32 start = SDL_GetTicks(); 
    Uint32 current = SDL_GetTicks(); 
    Uint32 delta = (current - start); 
    frames++; 
    if(delta > 1000) 
    { 
     float seconds = delta/1000.0f; 
     float spf = seconds/frames; 
     float fps = frames/seconds; 
     cout << "Milliseconds per frame: " << spf * 1000 << endl; 
     cout << "Frames per second: " << fps << endl; 
     cout << endl; 

     frames = 0; 
     start = current; 
    } 
} 

注意uint/uint将导致uint,而uint/floatfloat