Hi
Pyglet lacks a surface like pygame has so it doesn't support 2D primitives
out of the box (unless you draw the polygons directly in openGL). One
alternative I quite like is to use a pycairo surface to draw primitives and
then use that as a texture in pyglet.
See below for an example.
Regards
Martin
import pyglet
import ctypes
import cairo
WIDTH = 800
HEIGHT = 600
window = pyglet.window.Window(width=WIDTH, height=HEIGHT)
#cairo
data = (ctypes.c_ubyte * WIDTH * HEIGHT * 4)()
stride = WIDTH * 4
surface = cairo.ImageSurface.create_for_data (data, cairo.FORMAT_RGB24,
WIDTH, HEIGHT, stride);
ctx = cairo.Context(surface)
ctx.translate(200, 200)
#pyglet
texture = pyglet.image.Texture.create_for_size(pyglet.gl.GL_TEXTURE_2D,
WIDTH, HEIGHT, pyglet.gl.GL_RGB)
@window.event
def on_draw():
ctx.set_source_rgb(0, 1, 0)
ctx.rectangle(5, 5, 50, 50)
ctx.fill()
window.clear()
pyglet.gl.glEnable(pyglet.gl.GL_TEXTURE_2D)
pyglet.gl.glBindTexture(pyglet.gl.GL_TEXTURE_2D, texture.id)
pyglet.gl.glTexImage2D(pyglet.gl.GL_TEXTURE_2D, 0, pyglet.gl.GL_RGBA,
WIDTH, HEIGHT, 1, pyglet.gl.GL_BGRA, pyglet.gl.GL_UNSIGNED_BYTE, data)
pyglet.gl.glBegin(pyglet.gl.GL_QUADS)
pyglet.gl.glTexCoord2f(0.0, 1.0)
pyglet.gl.glVertex2i(0, 0)
pyglet.gl.glTexCoord2f(1.0, 1.0)
pyglet.gl.glVertex2i(WIDTH, 0)
pyglet.gl.glTexCoord2f(1.0, 0.0)
pyglet.gl.glVertex2i(WIDTH, HEIGHT)
pyglet.gl.glTexCoord2f(0.0, 0.0)
pyglet.gl.glVertex2i(0, HEIGHT)
pyglet.gl.glEnd()
ctx.set_source_rgb(0, 0, 0)
ctx.paint()
pyglet.app.run()
On Tue, Mar 5, 2013 at 1:44 AM, Joseph Clark <[email protected]> wrote:
> I know this is a newbie question, but can anybody shed light on how to use
> Pyglet draw a simple primitive like a filled rectangle? I can't believe
> that a web search didn't turn up any examples, but it didn't. And the
> OpenGL documentation is impenetrable to me.
>
> One article I found (http://www.akeric.com/blog/?p=1510) points to a '2d
> drawing primitives module' apparently uploaded to this Google group some
> years ago, but the link is dead now. That's as close as I've got.
>
> --
> You received this message because you are subscribed to the Google Groups
> "pyglet-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/pyglet-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
to do is to be. dobedobedo
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pyglet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.