Pyglet uses too much cpu

久未见 提交于 2019-12-11 03:21:10

问题


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.sprite.Sprite) is displayed at 60 frames per second. The problem is that this simple program is using up 50% of the CPU time somehow. I repeated the experiment with the sprite type found in the rabbyt library with the same result.

I decided to render 1000, then 10 000 sprites at 60 frames per second, and to my surprise the CPU usage stays at 50%. The only thing is that moving or animating a sprite results in slight stuttering.

Lastly, I tried running at 360 frames per second. Same result, 50% usage.

Here is the sample code:

import pyglet
import rabbyt


def on_draw(dt):
    window.clear()
    spr.render()

global window
window = pyglet.window.Window(800, 600)
spr = rabbyt.Sprite('ship.png')
spr.x = 100
spr.y = 100
pyglet.clock.schedule_interval(on_draw, 1.0/60.0)


if __name__ == '__main__':
    pyglet.app.run()

I am using a Core 2 Duo with an ATI HD 3500 card.

Any advice/ideas are appreciated.


回答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.




回答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.




回答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



来源:https://stackoverflow.com/questions/10524093/pyglet-uses-too-much-cpu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!