Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL2 - Vsync not working

Tags:

c++

sdl-2

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?

like image 620
Erik W Avatar asked Jun 20 '15 12:06

Erik W


1 Answers

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);
    }
}
like image 159
Martijn Courteaux Avatar answered Oct 01 '22 09:10

Martijn Courteaux