问题
I wrote a simple image display using python's pyglet package. On my Linux laptop, the code worked how I expected, displaying a constant 60 frames per second.
On my Windows 7 desktop (reasonably new from @Xi with a GeForce GTX 550 Ti), however, the frame rate is very very low (~10 FPS or less). I don't think this is a hardware limitation, however, because mouse-drag events speed the frame rate up drastically (60 FPS or more).
Why is my frame rate so low on Windows when I'm not mouse-dragging, and so fast when I am?
Here's the simplified code I use to produce this behavior:
import pyglet
from pyglet.window import mouse
image_1 = pyglet.resource.image('1.png')
image_2 = pyglet.resource.image('2.png')
fps_display = pyglet.clock.ClockDisplay()
image_x, image_y = 0, 0
frame = 0
window = pyglet.window.Window(image_1.width, image_2.height)
@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
global image_x, image_y
if buttons == mouse.LEFT:
image_x += dx
image_y += dy
@window.event
def on_draw():
global frame
frame += 1
window.clear()
if frame%2 == 0:
image = image_1
else:
image = image_2
image.blit(x=image_x, y=image_y,
height=image.height,
width=image.width)
fps_display.draw()
if __name__ == '__main__':
pyglet.app.run()
'1.png' and '2.png' have the same pixel dimensions, they're just different images so I can see the frame flipping. I'm using python 2.7.2 and pyglet version 1.2dev. I'm happy to add any additional information that would be helpful.
回答1:
Been a while since I did any pyglet, but looking back at some old code, I see it all seems to use a pyglet clock setup with
clock.schedule_interval(self.update,1.0/75.0)
clock.set_fps_limit(75)
in a subclassed pyglet Window to take control of the update rate (where update is a window method which advances the game world by a timestep parameter, and invalidates the window). I don't think there is anything in pyglet which particularly guarantees a regular "ticker" update rate otherwise.
来源:https://stackoverflow.com/questions/10523931/using-pyglet-in-python-why-does-my-frame-rate-speed-up-if-i-mouse-drag