Question

I have to evaluate the performance/functionality of Pyopengl vs Pyglet. The main concern is that it can't drop any frame while using high FPS. Before I start learning one of then, I need to see if it can do what the client needs.

I am trying to alternate (on vsync) between red and green (in fullscreen mode). If anybody can give me a good tutorial site, or help me with an example, it would be very nice.

I have looked at this post (and more): FPS with Pyglet half of monitor refresh rate

Made a modification, but can't see how to switch from one color to another one on Vsync.

import pyglet
from pyglet.gl import *


fps = pyglet.clock.ClockDisplay()

# The game window
class Window(pyglet.window.Window):
    def __init__(self):
        super(Window, self).__init__(fullscreen=True, vsync = False)
        self.flipScreen = 0
        glClearColor(1.0, 1.0, 1.0, 1.0)
        # 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)


def update(self, dt):
    self.flipScreen = not self.flipScreen
    pass

def on_draw(self):
    pyglet.clock.tick() # Make sure you tick the clock!
    if self.flipScreen == 0:
        glClearColor(0, 1, 0, 1.0)
    else:
        glClearColor(1, 0, 0, 1.0)
    self.clear()
    fps.draw()

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

I have seen a lot of tutorials, but I haven't been able to understand how to run this one test.

Thanks for your help.

Was it helpful?

Solution

Here is a code that does it, using pyglet. It has been tested on 3 monitors at 60 Hz and 120Hz. Please note that the use of global variable is bad. It may not be very clean, but it clearly shows that vsync is taken into account.This is the purpose of the code to test the functionnality of vsync.

import pyglet
from pyglet.gl import *
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *

# Direct OpenGL commands to this window.
config = Config(double_buffer = True)
window = pyglet.window.Window(config = config)
window.set_vsync(True)
window.set_fullscreen(True)

colorSwap = 0
fullscreen = 1

fps_display = pyglet.clock.ClockDisplay()

def on_draw(dt):
    global colorSwap
    glClear(GL_COLOR_BUFFER_BIT)  # Clear the color buffer
    glLoadIdentity()              # Reset model-view matrix

    glBegin(GL_QUADS)
    if colorSwap == 1:
        glColor3f(1.0, 0.0, 0.0)
        colorSwap = 0
    else:
        glColor3f(0.0, 1.0, 0.0)
        colorSwap = 1

    glVertex2f(window.width, 0)
    glVertex2f(window.width, window.height)
    glVertex2f(0.0, window.height)
    glVertex2f(0.0, 0.0)
    glEnd()
    fps_display.draw()

@window.event
def on_key_press(symbol, modifiers):
    global fullscreen
    if symbol == pyglet.window.key.F:
        if fullscreen == 1:
            window.set_fullscreen(False)
            fullscreen = 0
        else:
            window.set_fullscreen(True)
            fullscreen = 1
    elif symbol == pyglet.window.key.ESCAPE:
        print ''        


dt = pyglet.clock.tick()
pyglet.clock.schedule_interval(on_draw, 0.0001)
pyglet.app.run()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top