Search code examples
javalwjglframe-rate

LWJGL framerate with and without Display.sync()


This is something I've been curious about for a while now. I have a game which is rather basic in terms of geometry (a few thousand quads in two dimensions), and when I don't use Display.sync(), I get a framerate of ~550. However, when the game needs to perform some extensive calculations (when the world is changed), the FPS drop to ~480. When I call Display.sync(60) each iteration, the FPS are capped at 60, but when the same calculations are performed, drop to about 52 and the game stutters a bit. It's more of an annoyance than an actual issue, but I'm still curious as to why this happens. Why exactly do the FPS drop below 60 when they're fully capable of staying above, and furthermore, is there a way to prevent this?


Solution

  • I do not have direct experience with LWJGL, but it's pretty clear that when you specify 60 FPS, it does not actually measure the realtime FPS and account for it. It probably just sleeps for the required time necessary for 60 FPS in the ideal case where no other work needs to done.

    As I said I don't know about the specifics of LWJGL, but it's typical to separate the main game thread from the graphics thread. This will improve the performance even on single core systems as the time spent by the graphics thread sleeping can be used for calculations on the game thread. Right now the time is spent sleeping no matter what. Using separate threads is also better because the speed of the game is no longer tied to the graphics FPS.