Reputation: 39
I'm writing an application with pyglet where all rendered objects are children of the window class. I'm trying to draw a simple rectangle, but using on_resize seems to break everything. There's no error message, it just doesn't draw the rectangle.
This is my file structure:
main.py
lib
|-- window.py
|-- quad.py
This code doesn't work, but if I remove the on_resize methods it does:
-------------
FILE: main.py
-------------
import pyglet
import lib
window = lib.window.Window(1280, 720)
window.add_object(lib.quad.Quad())
pyglet.app.run()
-------------------
FILE: lib/window.py
-------------------
import pyglet
from . import quad
class Window(pyglet.window.Window):
def __init__(self, w, h):
super().__init__(width=w, height=h)
self.objects = []
def on_draw(self):
for obj in self.objects:
obj.on_draw()
def on_resize(self, width, height):
for obj in self.objects:
obj.on_resize(width, height)
def add_object(self, obj):
self.objects.extend([obj])
-------------
FILE: lib/quad.py
-------------
import pyglet
import pyglet.gl
class Quad():
def __init__(self):
self.quad = pyglet.graphics.vertex_list(4, ('v2i', (10, 10, 100, 10, 100, 100, 10, 100)), ('c3B', (0, 0, 255, 0, 0, 255, 0, 255, 0, 0, 255, 0)))
def on_draw(self):
self.quad.draw(pyglet.gl.GL_QUADS)
def on_resize(self, width, height):
pass
I would like to be able to keep rendered objects as children of the window class, since that makes running event handlers much easier. Is there any way that I can make this work with the on_resize handler? Any help is appreciated.
EDIT: I tried removing on_resize from the Quad class, and making on_resize do nothing in the Window class. It seems like the existence of the on_resize function is the issue--if on_resize exists, pyglet wont draw the rectangle.
Upvotes: 1
Views: 717
Reputation: 31
The problem is by subclassing Window, (MyWindow in this case), when you define an on_resize() event handler, you are replacing the inherited handler with your own. What you want to do instead is to push your on_resize() handler onto the event handling stack so that both handlers get called. You want to do the same some for of the others like on_close(). Example:
class MyWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_minimum_size(400, 300)
#
# Push your handler onto the on_resize handler stack
#
self.push_handlers(on_resize=self.local_on_resize)
def local_on_resize(self, x, y):
# Do stuff
def on_draw(self):
self.clear()
self.triangle.draw()
Note that you can reproduce the failure by having your local_on_resize() handler return a value of pyglet.event.EVENT_HANDLED, (which will stop processing of the remaining handlers), and the graphics won't render.
Upvotes: 3
Reputation: 469
In the on_resize method add the glViewport function call. And also don't forget to set up an orthogonal projection. Take a look at my code, it draws a triangle at the center of the screen.
from pyglet.gl import *
class Triangle:
def __init__(self):
self.vertices = pyglet.graphics.vertex_list(3, ('v3f', [0,0,0, 300,0,0, 150,300,0]),
('c3B', [255,0,0, 0,255,0, 0,0,255]))
def draw(self):
self.vertices.draw(GL_TRIANGLES)
class MyWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_minimum_size(400, 300)
glClearColor(0.2, 0.25, 0.2, 1.0)
glOrtho(0, 1280, 0, 720, -10, 10) # setup orthogonal projection
self.triangle = Triangle()
def on_draw(self):
self.clear()
glPushMatrix()
glTranslatef(640-150 ,360-150, 0) # translate the Triangle to the center
self.triangle.draw()
glPopMatrix()
def on_resize(self, width, height):
glViewport(0, 0, width, height) # resize the viewport
if __name__ == "__main__":
MyWindow(1280, 720, 'My window', resizable=True)
pyglet.app.run()
Upvotes: 1