SDL2 - Vsync not working

别来无恙 提交于 2021-01-03 03:29:41

问题


I use vsync in my program, and it works fine until I minimize the window. I did this when I created the renderer:

renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

And here is the game loop:

while (running)
{
    checkEvent();
    handleKeyboard(timer.getDelta());

    render();
}

It gives me a stable 60 frames per second, but I get more than 100000 frames per second when I minimize the window. Why is that happening?


回答1:


Probably just a bug in SDL. If you investigated the problem more: read docs do some tests, you can report the bug. It is likely going to be fixed pretty soon. Ryan and colleagues work great. :)

That being said. I would never assume that Vsync works on every system. You probably want to add in your own framerate limitation system. Relying on the hardware to limit the framerate is a bad idea.


EDIT: I use this in my game to limit the framerate:

while (!gameLoop->done)
{
    int start = SDL_GetTicks();
    gameLoop->update();
    int time = SDL_GetTicks() - start;
    if (time < 0) continue; // if time is negative, the time probably overflew, so continue asap

    int sleepTime = gameLoop->millisecondsForFrame - time;
    if (sleepTime > 0)
    {
        SDL_Delay(sleepTime);
    }
}


来源:https://stackoverflow.com/questions/30953858/sdl2-vsync-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!