FPS with Pyglet half of monitor refresh rate

前端 未结 2 575
说谎
说谎 2021-01-14 23:41

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

相关标签:
2条回答
  • 2021-01-15 00:09

    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()
    
    0 讨论(0)
  • 2021-01-15 00:12
    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.

    0 讨论(0)
提交回复
热议问题