Skip to content Skip to sidebar Skip to footer

Pyglet Uses Too Much Cpu

I recently startet getting into pyglet and rabbyt from pygame, but I have hit something of a brick wall. I created a basic example where one Sprite (of the type found in pyglet.sp

Solution 1:

Be aware that the default pyglet event handler will fire an 'on_draw' event every time it clears the event queue.

http://www.pyglet.org/doc/programming_guide/the_application_event_loop.html

The pyglet application event loop dispatches window events (such as for mouse and keyboard input) as they occur and dispatches the on_draw event to each window after every iteration through the loop.

This means that any event can trigger a redraw.

So if you're moving the mouse about or doing anything that fires events, you will get massive slow down as it begins to trigger render calls.

This also caused problems because I was doing my own render calls, so I would get the two buffers fighting which created a 'ghost' effect on the screen. Took me a while to realise this was the cause.

I monkey patched the event loop to not do this. https://github.com/adamlwgriffiths/PyGLy/blob/master/pygly/monkey_patch.py

Be aware that this patched event loop will no longer render on it's own, you must manually flip the buffers or trigger an 'on_draw' event.

It might be the case that, although you've hooked in at 60fps, but the internal render loop is ticking at the maximum possible rate.

I dislike code that takes away control, hence my patch lets me decide when render events occur.

Solution 2:

Hmm.. You might want to know the fps at which the game runs, if it helps:

cldis = pyglet.clock.ClockDisplay()

Then add this to your on_draw function:

cldis.draw()

it draws the current fps at the bottomleft corner of the screen in a semi-transparent color.

Solution 3:

I know that in Pygame there is the built-in called "Clock". You can put a limit on how many times the game loops per second using the tick method. In my example I have put a limit of 30 FPS. This prevents your CPU being on constant demand.

clock = pygame.time.Clock() 

While 1:

    clock.tick(30) # Puts a limit of 30 frames per second on the loop

In pyglet there appears to be something similar:

    pyglet.clock.schedule_interval(on_draw, 1.0/60.0)
    clock.set_fps_limit(60)

Hope that helps!

edit: documentation on fps limit: http://pyglet.org/doc/api/pyglet.clock-module.html#set_fps_limit

Post a Comment for "Pyglet Uses Too Much Cpu"