I am attempting to build a small API to simplify some things when using pyglet. I want it to be as similar to Processing syntax, so that I can use my Python Processing code in a pure python environment instead of the processing one.
I have gotten some code to work to draw a rectangle, but it only works when I copy / paste. The model I am using at the moment does not seem to do anything.
Here is the important parts of the App class that I am using:
import pyglet
class App(pyglet.window.Window):
def __init__(self, w=1280, h=720, fullscreen = False, resizable = True):
super(App, self).__init__(w, h, fullscreen = fullscreen, resizable=resizable)
self.size = self.width, self.height = w, h
self.alive = 1
def on_draw(self):
self.render()
def setBinds(self, drawFunction):
self.drawFunction = drawFunction
def render(self):
self.clear()
self.drawFunction()
self.flip()
def run(self):
while self.alive == 1:
self.render()
event = self.dispatch_events()
def Rect(self, x, y, w, h):
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, w, y, w, h, x, h]))
Here is the code to run the application. It should open a window and then every frame draw a rectangle to the screen.
from processing import App
def draw(dt=0):
width, height = APP.width, APP.height
APP.Rect(100,100,100,100)
if __name__ == "__main__":
APP = App()
APP.setBinds(drawFunction=draw)
APP.run()
However it does not draw anything to the screen. No errors are displayed or anything, just a blank black screen. I have gotten the pyglet.grapics.draw function to draw things on the screen when using simplified code such as:
import pyglet
from pyglet.window import mouse
window = pyglet.window.Window()
@window.event
def on_draw():
window.clear()
x, y, dx, dy = 100, 100, 20, 30
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, dx, y, dx, dy, x, dy]))
pyglet.app.run()
I am not sure what the difference is between these two sets of code that is making it not work
When you call the method Rect
with the following parameters
APP.Rect(100,100,100,100)
then the instruction
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, w, y, w, h, x, h]))
doesn't draw a rectangle, because all the vertex coordinates are identical.
To draw a rectangle you've to calculate the right and top vertex coordinates by x+w
respectively y+h
:
x1, y1, x2, y2 = x, y, x+w, y+h
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x1, y1, x2, y1, x2, y2, x1, y2]))