Using pyglet in python, why does my frame rate speed up if I mouse-drag?

前提是你 提交于 2019-12-11 03:15:00

问题


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

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