简体   繁体   English

用gettimeofday固定FPS

[英]Clamping FPS with gettimeofday

I'm trying to clamp my game loop to a specific FPS by employing gettimeofday. 我试图通过使用gettimeofday将游戏循环限制在特定的FPS上。 It's a very rudimentary game, so it keeps chewing up all my processing power. 这是一个非常基本的游戏,因此它不断消耗着我所有的处理能力。

Regardless of how low I set my FRAMES_PER_SECOND, it continues to try to run as fast as possible. 无论我将FRAMES_PER_SECOND设置为多低,它都会继续尝试以最快的速度运行。

I got a pretty good handle on what deWiTTERS has to say about game loops, but am using gettimeofday instead of GetTickCount b/c I'm on a Mac. 我对deWiTTERS关于游戏循环的说法有很好的了解,但是我使用的是gettimeofday而不是Mac上的GetTickCount b / c。

Also, I'm running OSX and using C++, GLUT. 另外,我正在运行OSX并使用C ++,GLUT。

This is what my main looks like: 这是我的主要样子:

int main (int argc, char **argv)
{
    while (true)
    {
    timeval t1, t2;
    double elapsedTime;
    gettimeofday(&t1, NULL); // start timer

    const int FRAMES_PER_SECOND = 30;
    const int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
    double next_game_tick = elapsedTime;
    int sleep_time = 0;

    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); 
    glutInitWindowSize (windowWidth, windowHeight); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window"); 
    glutDisplayFunc (display); 
    glutIdleFunc (idle); 
    glutReshapeFunc (reshape);
    glutPassiveMotionFunc(mouseMovement); //check for mouse movement
    glutMouseFunc(buttonPress); //check for button press

    gettimeofday(&t2, NULL); // stop timer after one full loop
    elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // compute sec to ms
    elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // compute us to ms

    next_game_tick += SKIP_TICKS;
    sleep_time = next_game_tick - elapsedTime;
    if( sleep_time >= 0 )
        {
        sleep( sleep_time );
        }

    glutMainLoop (); 
    }
}

I've tried placing my gettimeofday and sleep functions in multiple locations, but I can't seem to find the sweet spot for them (given that my code is even correct). 我试图将我的gettimeofday和sleep函数放置在多个位置,但是我似乎找不到它们的最佳选择(假设我的代码正确)。

That will only ever get called once. 那只会被调用一次。 You need to put the FPS logic inside the display function I believe because glutMainLoop will never return. 我相信您需要将FPS逻辑放入显示函数中,因为glutMainLoop永远不会返回。 (which also means your while loop is not needed. ) (这也意味着不需要您的while循环。)

edit: or more likely it should go inside your idle function. 编辑:或更可能它应该在您的空闲函数内。 It has been awhile since I have used glut. 自从我使用过剩食品以来已经有一段时间了。

glutMainLoop() : glutMainLoop()

glutMainLoop enters the GLUT event processing loop. glutMainLoop进入GLUT事件处理循环。 This routine should be called at most once in a GLUT program. 该例程在GLUT程序中最多应调用一次。 Once called, this routine will never return . 一旦调用, 该例程将永远不会返回 It will call as necessary any callbacks that have been registered. 它将在必要时调用已注册的任何回调。

elapsed_time初始化next_game_tick ,您还没有初始化elapsed_time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM