Reputation: 246
I'm trying to batch draw primitives with pyglet, but I can't even get a simple example to work.
I was able to draw things individually, but my understanding is that best practice is to put elements into batches.
Here is some very basic code, yet it is not working. I am getting the error: AttributeError: 'tuple' object has no attribute 'parent'
import numpy as np
import pyglet
WIDTH = 640
HEIGHT = 480
game_window = pyglet.window.Window(width=WIDTH, height=HEIGHT)
batch = pyglet.graphics.Batch()
def update(dt):
global t
batch = pyglet.graphics.Batch()
batch.add(2, pyglet.gl.GL_LINES, ('v2f', (100,100,200,200)),
('c3B', (255,0,0) * 2))
batch.add(2, pyglet.gl.GL_LINES, ('v2f', (400,400,50,50)),
('c3B', (255,0,0) * 2))
@game_window.event
def on_draw():
game_window.clear()
batch.draw()
if __name__ == '__main__':
pyglet.clock.schedule_interval(update, 1/120)
pyglet.app.run()
I feel like I must be missing something obvious.
Upvotes: 1
Views: 1568
Reputation: 211278
The 3rd parameter of pyglet.graphics.Batch.add
is the pyglet.graphics.Group
object. If you don't associate the batch to a group, this parameter can be None
. See Batched rendering:
def update(dt):
#batch = pyglet.graphics.Batch() <---- delete
batch.add(2, pyglet.gl.GL_LINES, None, # <---- add None
('v2f', (100,100,200,200)),
('c3B', (255,0,0) * 2)
)
batch.add(2, pyglet.gl.GL_LINES, None, # <---- add None
('v2f', (400,400,50,50)),
('c3B', (255,0,0) * 2)
)
Upvotes: 2