Bunshock
Bunshock

Reputation: 33

Pyglet window don't draw at 60FPS unless the rate of an update function is called in less than 1/60 of a second

So, started writing a small game in my free time and decided to use a library I never used before: pyglet. Im used to work with pygame and even arcade (which is implemented using pyglet) but thought I should try to use pyglet for once, specially because arcade isn't available in python 3.10 yet. So, the actual problem is, when creating the window and trying to draw something on it I noticed the image was flickering, so I decided to check how much FPS I was getting. The following code is just a test I was running to try to figure out what might have been happening.

import pyglet

window = pyglet.window.Window(800, 600, vsync=False)
fps_display = pyglet.window.FPSDisplay(window)

# Redundant just to be sure vsync was indeed disabled
window.set_vsync(False)


@window.event
def on_draw():
    window.clear()
    fps_display.draw()

def update(dt):
    print(f"FPS: {pyglet.clock.get_fps()}")

if __name__ == "__main__":
    pyglet.clock.schedule_interval(update, 1.0/60.0)
    pyglet.app.run()

So, with this simple code, through the FPSDisplay in the window I get around 30 FPS, but from the print funtion in the update method, that checks pyglet clock, I get hundreds (or thousands when moving my mouse around the screen, which I actually find odd as well) FPS. I tried enabling and disabling vsync on window creation and through the set_vsync function and nothing changed. The only thing that made the FPSDisplay (which was the speed stuff was actually beign drawed to the window) go up was setting the update function rate to something lower then 1/60 (even 1/70 was already enough to get around 60 FPS).

pyglet.clock.schedule_interval(update, 1.0/70.0)

So, I was wandering, is this the way pyglet actually works or maybe a problem with vsync?

Upvotes: 0

Views: 557

Answers (1)

Charlie
Charlie

Reputation: 792

Typically you don't want the game running at max FPS with little going on, otherwise you are overloading the card and wasting CPU cycles for no reason.

Basically the event loop is trying to optimize this for you. To answer the question why, Pyglet will only update the FPS if an event of some sort occurs. (This is being changed in Pyglet 2.0 since it's not really intuitive) You can see this by moving the mouse cursor around and see the FPS shoot up. Windows is pushing events into the window, causing the application to work more. By scheduling an update, you are forcing the loop to work more, which is why it increases.

If you want to force the max FPS even with vsync you could just do:

def update(dt):
    pass

pyglet.clock.schedule(update)

Generally once you start adding more demanding functionality and features, this shouldn't be an issue.

Upvotes: 1

Related Questions