PyGlet and PyGTK

95 views
Skip to first unread message

selsine

unread,
Mar 8, 2008, 5:00:47 PM3/8/08
to pyglet-users
Hi Eveyone,

First off I have to say that I'm not a PyGlet or Open GL expert, but I
am looking into using PyGlet for my future "game" code.

One of the things that I have been looking at is the possibilities of
using PyGlet in a PyGTK application to serve as a level editor. I've
been looking through the archives and from what I understand this is
possible. I know that I won't have access to the event loop, but since
this is for an editor it wouldn't matter.

The reason I want to use PyGlet, instead of anther OpenGL module, is
because the level editor would be for an eventual PyGlet game so I
would like to reuse and share as much code as possible (i.e. use the
PyGlet 1.1 sprites in both_

I can create a GL Context in a PyGTK window, and according to one post
that I read what I need to do is "trick" PyGlet into using this GL
context.

So my questions are:

1) How would I trick PyGlet into using the GL context that I have
already created

2) How feasible is the idea of a PyGlet level editor in a PyGTK
application?

If I've missed something obvious in the archives, sorry about that.
I've done a few searches but didn't come up with anything all the
explicit.

Hopefully all if this makes sense. Thanks in advance.

mark.

Alex Holkner

unread,
Mar 8, 2008, 5:47:06 PM3/8/08
to pyglet...@googlegroups.com
On 3/9/08, selsine <sel...@gmail.com> wrote:
>
> Hi Eveyone,
>
> First off I have to say that I'm not a PyGlet or Open GL expert, but I
> am looking into using PyGlet for my future "game" code.
>
> One of the things that I have been looking at is the possibilities of
> using PyGlet in a PyGTK application to serve as a level editor. I've
> been looking through the archives and from what I understand this is
> possible. I know that I won't have access to the event loop, but since
> this is for an editor it wouldn't matter.
>
> The reason I want to use PyGlet, instead of anther OpenGL module, is
> because the level editor would be for an eventual PyGlet game so I
> would like to reuse and share as much code as possible (i.e. use the
> PyGlet 1.1 sprites in both_
>
> I can create a GL Context in a PyGTK window, and according to one post
> that I read what I need to do is "trick" PyGlet into using this GL
> context.
>
> So my questions are:
>
> 1) How would I trick PyGlet into using the GL context that I have
> already created

There's actually no trick... OpenGL has only one active context at a
time. In pyglet you can switch contexts with Window.switch_to().
When you create a new window the context will switch to that window
automatically. If you never call switch_to() or create a new window,
the context never changes; so the PyGTK one remains active.

The situation could get a lot more complicated if you're using more
than one PyGTK window (context), or trying to mix pyglet windows with
PyGTK windows.

In pyglet 1.1 you should also set pyglet.options['shadow_window'] to False.

>
> 2) How feasible is the idea of a PyGlet level editor in a PyGTK
> application?

I can't foresee any problems if you stick to just one PyGTK window.

Alex.

selsine

unread,
Mar 9, 2008, 2:41:21 PM3/9/08
to pyglet-users
Hi Alex,

Thanks for the reply.

<snip>
> > I can create a GL Context in a PyGTK window, and according to one post
> > that I read what I need to do is "trick" PyGlet into using this GL
> > context.

Just to be clear I'm using GtkGLExt: http://www.k-3d.org/gtkglext/Main_Page
to create my GL context.

> > So my questions are:
>
> > 1) How would I trick PyGlet into using the GL context that I have
> > already created
>
> There's actually no trick... OpenGL has only one active context at a
> time. In pyglet you can switch contexts with Window.switch_to().
> When you create a new window the context will switch to that window
> automatically. If you never call switch_to() or create a new window,
> the context never changes; so the PyGTK one remains active.
>
> The situation could get a lot more complicated if you're using more
> than one PyGTK window (context), or trying to mix pyglet windows with
> PyGTK windows.
>
> In pyglet 1.1 you should also set pyglet.options['shadow_window'] to False.

Thanks for the information Alex, I've been able to try this out and it
feels as though I almost have it going. The only problem now is that
my sprites are not drawing properly. Instead of seeing the actual
image what I'm getting are white squares (the size of the image) where
the sprites should be. This happens if I use a batch draw or just an
Image.blit().

Just to make sure that I understand what you are saying properly. I'm
creating my GL Context and doing all the other OpenGL stuff using
GtkGLExt, and then in my draw() function I'm trying to use PyGlet as I
normally would...which at the moment is giving my white squares.

I think I'm close, it feels as though I'm missing something simple.

I'm setting up the GL context with the following options:

display_mode = (gtk.gdkgl.MODE_RGBA
| gtk.gdkgl.MODE_DOUBLE
| gtk.gdkgl.MODE_DEPTH
| gtk.gdkgl.MODE_ALPHA)

And then configuring it like so:

gldrawable.gl_begin(glcontext)
glViewport(0, 0, 1000, 800)
gldrawable.gl_end()

And then I set things up in my realize event like so:

if not gldrawable.gl_begin(glcontext):
return

glClearColor(1.0, 1.0, 1.0, 1.0)
glMatrixMode(GL_PROJECTION)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glLoadIdentity()
glOrtho( 0, 1000, 0, 800, -1.0, 1.0)
glMatrixMode(GL_MODELVIEW)
gldrawable.gl_end()

And then finally I draw like so:

gldrawable.gl_begin(glcontext)
glClearColor(0,0,0,0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

self.display()

gldrawable.swap_buffers()
gldrawable.gl_end()

Where self.display() calls the PyGlet draw code.

Does anything in that appear wrong or way off base? Most of the code
is from a GtkGLExt example that I have configured to work with PyGlet.

>
>
> > 2) How feasible is the idea of a PyGlet level editor in a PyGTK
> > application?
>
> I can't foresee any problems if you stick to just one PyGTK window.

Thanks I'm planning on sticking with one window.

mark.

Alex Holkner

unread,
Mar 9, 2008, 5:35:17 PM3/9/08
to pyglet...@googlegroups.com

Sounds like the texture's missing, which suggests to me that it was
loaded into the wrong context. Make sure you only load images (and
get .texture / call get_texture) after setting up the GTK context.

Alex.

selsine

unread,
Mar 9, 2008, 8:51:51 PM3/9/08
to pyglet-users
On Mar 9, 5:35 pm, "Alex Holkner" <alex.holk...@gmail.com> wrote:
>
> Sounds like the texture's missing, which suggests to me that it was
> loaded into the wrong context. Make sure you only load images (and
> get .texture / call get_texture) after setting up the GTK context.

That was my problem, thanks a lot Alex. I was loading the images using
PyGlet before the GL Context was really setup. Once I moved it after
the context was ready I was able to display my image.

I did notice that I was unable to get sprites to draw using the
pyglet.sprite.Sprite.draw() method. I was also unable to get sprites
to draw using the batch method. In oder to get images to display I had
to use the image blit() function.

Thinking that this maybe be an issue with the PyGlet 1.1 code I tried
using sprites and the batch draw examples found here:
http://www.pyglet.org/doc/1.1/api/pyglet.sprite-module.html just to
test and I found that both examples currently do not work on my
computer with the latest version of PyGlet.

I'm not sure if this is a known issue with the current version or not,
so I thought I would mention it here. I did notice that if I tried to
use the sprite or batch draw, and then use the blit() method that the
image gets drawn by the blit but it appears to be washed out.

mark.

Alex Holkner

unread,
Mar 9, 2008, 8:57:15 PM3/9/08
to pyglet...@googlegroups.com

That's an unreported bug, and so unique (so far) to your setup. I
would assume that none of the text or sprite examples in SVN head work
(i.e., most of them). Please open an issue, including the output of
`python -m pyglet.info` (requires Python2.5 and SVN head).

Alex.

selsine

unread,
Mar 9, 2008, 9:32:41 PM3/9/08
to pyglet-users


On Mar 9, 8:57 pm, "Alex Holkner" <alex.holk...@gmail.com> wrote:
> That's an unreported bug, and so unique (so far) to your setup. I
> would assume that none of the text or sprite examples in SVN head work
> (i.e., most of them). Please open an issue, including the output of
> `python -m pyglet.info` (requires Python2.5 and SVN head).

All right I'll report the issue, but just to be sure that I have
everything correctly (I've only used SVN a few times) I got PyGlet
from the SVN repository using:

svn checkout http://pyglet.googlecode.com/svn/trunk/ pyglet

The 'python -m pyglet.info' command works, but I just wanted to make
sure that I was working with the most recent code.

Also I was unable to find any other spirte examples to test in the
SVN.

mark.

Alex Holkner

unread,
Mar 9, 2008, 10:32:50 PM3/9/08
to pyglet...@googlegroups.com
On Mon, Mar 10, 2008 at 12:32 PM, selsine <sel...@gmail.com> wrote:
>
>
>
> On Mar 9, 8:57 pm, "Alex Holkner" <alex.holk...@gmail.com> wrote:
> > That's an unreported bug, and so unique (so far) to your setup. I
> > would assume that none of the text or sprite examples in SVN head work
> > (i.e., most of them). Please open an issue, including the output of
> > `python -m pyglet.info` (requires Python2.5 and SVN head).
>
> All right I'll report the issue, but just to be sure that I have
> everything correctly (I've only used SVN a few times) I got PyGlet
> from the SVN repository using:
>
> svn checkout http://pyglet.googlecode.com/svn/trunk/ pyglet
>
> The 'python -m pyglet.info' command works, but I just wanted to make
> sure that I was working with the most recent code.

That's fine.

> Also I was unable to find any other spirte examples to test in the
> SVN.

"noisy" and "astraea" both use sprites.

Alex.

selsine

unread,
Mar 10, 2008, 12:02:58 AM3/10/08
to pyglet-users
Both examples don't seem to work on my system. The noisy example does
nothing, while the astrea example does display some graphics, but it
doesn't seem to work at all, I'm assuming this is because many of the
sprites are not being displayed.

I've submitted the issue.

Thanks again for all of your help getting me going with my PyGTK/
PyGlet application.

mark.

On Mar 9, 10:32 pm, "Alex Holkner" <alex.holk...@gmail.com> wrote:
Both examples don't seem to work on my system. The noisy example does
nothing, while the astrea example does display some graphics, but it
doesn't seem to work at all. I'm assuming this is because many of the
sprites are not being displayed.

I've submitted the issue.

Thanks again for all of your help getting me going with my PyGTK/
PyGlet application.

mark.

Richard Jones

unread,
Mar 10, 2008, 5:10:06 AM3/10/08
to pyglet...@googlegroups.com
On Mon, 10 Mar 2008, selsine wrote:
> PyGlet

Please, it's just "pyglet" :)


Richard


selsine

unread,
Mar 10, 2008, 11:21:02 AM3/10/08
to pyglet-users
Mea culpa. My CamelCaps knows no bounds...

mark.
Reply all
Reply to author
Forward
0 new messages