Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

144hz and VSync #61

Open
turric4n opened this issue Feb 24, 2020 · 5 comments
Open

144hz and VSync #61

turric4n opened this issue Feb 24, 2020 · 5 comments

Comments

@turric4n
Copy link

Hi Marc,

Samples and main game loop are unplayable under 144Hz (aka 144 fps). VSync is not working here...

I don't know if caused by SDL2 or by Tilengine... How can be addressed?

@megamarc
Copy link
Owner

Hi!
By default TLN_CreateWindow() sets vsync flag. If it's not working for you, you should modify Window.c and remove CWF_VSYNC from line 426:
https://github.com/megamarc/Tilengine/blob/master/src/Window.c#L426

Then you'll have to do frame throttling on your game loop to adjust to your target frame rate, for example using SDL_Delay() + SDL_GetTicks() if you're using libSDL.

@turric4n
Copy link
Author

Thanks Marc,

Please take a look here :

https://stackoverflow.com/questions/40854974/sdl-opengl-game-runs-too-fast-on-144hz-screen-cant-use-vsync

Today 144hz screens are common things under gaming scene, so, ther is my two cents :

  • Why VSync is not working? It's quite strange.
  • That code will do the job?
int32_t tickInteval = 1000/FPS; // frequency in Hz to period in ms
uint32_t lastUpdateTime = 0;
int32_t deltaTime = 0;
while (running) { // running condition
    uint32_t currentTime = SDL_GetTicks();
    deltaTime = currentTime - lastUpdateTime;

    int32_t timeToSleep = tickInteval - deltaTime;
    if(timeToSleep > 0)
    {
        SDL_Delay(timeToSleep); // energy saving
    }

    update(deltaTime); // game logic
    lastUpdateTime = currentTime;
}


@megamarc
Copy link
Owner

Hi again,
Your frame throttling should work, the basic idea is measuring the time it takes to process one frame (game logic, render, etc) and delay/sleep until the time to next frame at your target FPS.
Why is vsync not working for you? I don't know as I don't have such 144 Hz monitor, mine is standard 60 Hz. But I guess is more related to G-sync and adaptive refresh rates than to a fixed 60/144 Hz issue. SDL2 just asks the underlying graphics API to enable vsync, but it's ultimately graphics driver and hardware to do the job.

@turric4n
Copy link
Author

Hi again,
Your frame throttling should work, the basic idea is measuring the time it takes to process one frame (game logic, render, etc) and delay/sleep until the time to next frame at your target FPS.
Why is vsync not working for you? I don't know as I don't have such 144 Hz monitor, mine is standard 60 Hz. But I guess is more related to G-sync and adaptive refresh rates than to a fixed 60/144 Hz issue. SDL2 just asks the underlying graphics API to enable vsync, but it's ultimately graphics driver and hardware to do the job.

Thanks Mark, I will take a look over Nvidia low level things like GSync and how affect to SDL2 Window handling.

Because Tilengine demos are affected too, we need to think about maintaining compatibility with more screen refresh frequencies before making serious projects.

Greetings

@megamarc
Copy link
Owner

Maybe this article on how SDL2 handles adaptive sync may help:
https://wiki.libsdl.org/SDL_GL_SetSwapInterval
However this is not a tilengine issue, but a general windowing/vsync one on modern monitors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants