FPS with Pyglet half of monitor refresh rate

血红的双手。 提交于 2019-12-01 07:27:59

问题


I'm new to working with Pyglet and I've written a small program which moves a ball around the screen. Right now I'm having difficulty establishing a steady frame rate of 60 fps. While Pyglet is supposed to sync with my monitor's refresh rate of 60Hz, Pyglet is setting my fps to half of my refresh rate (ex. when 60Hz, 30 fps). Is there something wrong in my code that is causing this?

import pyglet
import physicalobject
import random
from pyglet.window import mouse

pyglet.resource.path = ['./resources']
pyglet.resource.reindex()

ball_image = pyglet.resource.image("ball2.png")

#sets clock format
fps_display = pyglet.clock.ClockDisplay(format='%(fps).2f fps')

def center_image(image):
    image.anchor_x = image.width/2
    image.anchor_y = image.height/2

center_image(ball_image)
ball = physicalobject.PhysicalObject(img=ball_image, x = 400, y = 300)
ball.scale = .2
ball.velocity_x = random.randint(-4,4)*150
ball.velocity_y = random.randint(-4,4)*150

#Calls update function to set new ball position based on velocity
def update(dt):
     ball.update(dt)

@window.event
def on_mouse_drag(x, y, dx, dy, button, modifiers):
     ball.x = x
     ball.y = y
     ball.velocity_x = dx * 20
     ball.velocity_y = dy * 20

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

def main():
     pyglet.clock.schedule_interval(update, 1/120.0)
     pyglet.app.run()

if __name__ == '__main__':
     main()

回答1:


Pyglet simply doesn't handle it correctly on some systems, you have to disable your application window's vsync in order to get it to work. Here's an example script you can run to get a feel for how it works:

import pyglet

# Show FPS
fps = pyglet.clock.ClockDisplay()

# The game window
class Window(pyglet.window.Window):
    def __init__(self):
        super(Window, self).__init__(vsync = False)
        # Run "self.update" 128 frames a second and set FPS limit to 128.
        pyglet.clock.schedule_interval(self.update, 1.0/128.0)
        pyglet.clock.set_fps_limit(128)

    # You need the dt argument there to prevent errors,
    # it does nothing as far as I know.
    def update(self, dt): 
        pass

    def on_draw(self):
        pyglet.clock.tick() # Make sure you tick the clock!
        self.clear()
        fps.draw()

# Create a window and run
win = Window()
pyglet.app.run()



回答2:


import pyglet
from time import time, sleep

class Window(pyglet.window.Window):
    def __init__(self, refreshrate):
        super(Window, self).__init__(vsync = False)
        self.frames = 0
        self.framerate = pyglet.text.Label(text='Unknown', font_name='Verdana', font_size=8, x=10, y=10, color=(255,255,255,255))
        self.last = time()
        self.alive = 1
        self.refreshrate = refreshrate

    def on_draw(self):
        self.render()

    def render(self):
        self.clear()
        if time() - self.last >= 1:
            self.framerate.text = str(self.frames)
            self.frames = 0
            self.last = time()
        else:
            self.frames += 1
        self.framerate.draw()
        self.flip()

    def on_close(self):
        self.alive = 0

    def run(self):
        while self.alive:
            self.render()
            event = self.dispatch_events()
            sleep(1.0/self.refreshrate)

win = Window(23) # set the fps
win.run()

Note the lack of the clock feature. Also, try setting vsync = True and removing the sleep(1.0/self.refreshrate), this will lock the refresh rate to your monitor.

Also, note that i don't use pyglet.app.run() to lock the rendering process, i call self.dispatch_events() instead.. it doesn't really do anything except let the graphic "poll" and move on, without it.. pyglet waits for a poll to occur, which pyglet.app.run() normally does.



来源:https://stackoverflow.com/questions/10583701/fps-with-pyglet-half-of-monitor-refresh-rate

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