On Jul 15, 5:21 pm, Zombie Mariachis <[email protected]>
wrote:
> Thanks a ton for all the info!  To better explain what I've gotten the
> code to do so far, and what I'm trying to do, I've uploaded some game
> play from the project to youtube.http://www.youtube.com/watch?v=zbX9Nj0P1ks
>
>   As seen in the video, the background layers shift via the
> glTranslatef draw instructions.  Right now each layer is just a sprite
> with it's own draw being called in the on_draw window event.  I'd like
> to batch all the draws for the background but when I do the
> glTranslatef seems to be ignored.  One thought was that each sprite's
> 'def update' was being ignored, but I don't know, I'm new to all
> this.


Wow, that looks really good, actually. Do you have an online page for
the project?

Now everything makes a bit more sense, especially after I had a few
hours of sleep. Okay, it should be able to be done, but not in the way
you expect it to be. I'll need to explain some things first, about how
Pyglet does its things. First off, a Sprite contains an image that it
uses for its texture. This is actually converted into a Texture
object, and stored as sprite_name.texture. When you ask a Sprite to
draw itself, it'll do this:

  self._vertex_list.draw(GL_QUADS)

This vertex list is actually a graphics.vertex_list. This means that
it contains a whole list of vertices, in various forms. For example, a
vertex list can contain simultaneously, v3f (position in 3 dimensions
stored as floats), t3f (texture coordinates in 3 dimensions as
floats), c4i (vertex color information in RGBA as integers). This
means that when you draw a vertex list, it already has ALL the info it
needs to draw the vertices, textured, colored, whatever.  Now THIS
vertex list, actually contains a vertex DOMAIN, which is another
object for management. This domain can contain a lot of different
vertex lists. If you only have a single vertex list, it'll have its
own domain it's actually calling for to draw itself.

This seems complicated, but it's just a way of putting everything into
its own thing. A vertex list contains all the properties needed to
draw one 'shape' (or multiple, I suppose), and a vertex domain
contains any number of vertex lists. In this case, the vertex list is
just one and it's calling its single domain to draw it in GL_QUADS
mode. Looking at the vertex domain draw code, we find out that it's
pretty 1:1. It'll draw the vertex list immediately, with the
properties it's told to. Like the position, texture coordinates and
color specified above.


So you want the background to scroll, this can be done in a number of
ways. The Pyglet way, to me, would be to modify this vertex list's
texture coordinates, instead of actually GL_Translate-ing the texture
matrix. This means that every time you want the texture to move a bit,
you go and reassign the 4 vertices' texture coordinates as being a bit
'higher'. This is actually pretty complicated to do properly, and I
believe is prone to bugs. Possibly not, but it just seems like
scratching your right ear with your left arm over your head.

Now, the way you're doing it, it might work. I'm not completely sure
and, again, I'm not in a position to test right now. My solution for
it would be blitting the texture yourself manually. This means looking
at Texture's blit code, and doing it yourself. This might work, but it
might not.

  def draw(self):
      #first we set everything up like the texture does, but with
modifying references
      t = self.texture.tex_coords
      x1 = x - self.texture.anchor_x
      y1 = y - self.texture.anchor_y
      x2 = x1 + (width is None and self.width or width)
      y2 = y1 + (height is None and self.height or height)
      array = (GLfloat * 32)(
           t[0],  t[1],  t[2],  1.,
           x1,    y1,    z,     1.,
           t[3],  t[4],  t[5],  1.,
           x2,    y1,    z,     1.,
           t[6],  t[7],  t[8],  1.,
           x2,    y2,    z,     1.,
           t[9],  t[10], t[11], 1.,
           x1,    y2,    z,     1.)
      #this is the initialising of data as in Texture.blit(), now we
actually draw everything
      glPushAttrib(GL_ENABLE_BIT)
      glEnable(self.texture.target)
      glBindTexture(self.texture.target, self.texture.id) #keep in
mind this is an EXPENSIVE operation in terms of time, each differing
use
      #now we have bound and enabled the texture. This means that we
can do the translation ourselves, and it should work!
      glMatrixMode(GL_TEXTURE)
      glPushMatrix()
      glTranslatef(self.gx,self.gy,-100)
      glMatrixMode(GL_MODELVIEW) #without this, pyglet might go nuts,
not sure how it handles it.
      #now we draw it like Texture.blit() says.

      glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT)
      glInterleavedArrays(GL_T4F_V4F, 0, array)
      glDrawArrays(GL_QUADS, 0, 4)
      glPopClientAttrib()
      glPopAttrib()

      #now we reset what we changed.
      glMatrixMode(GL_TEXTURE)
      glPopMatrix()
      glMatrixMode(GL_MODELVIEW)


In theory this should work. It's a hack, so to speak, but the (sane)
way involves subclassing Texture, as your own ScrollingTexture object,
and modifying the blit function so it looks like this. But that would
mean blitting would involve an extra variable, gx and gy, so I'm iffy
to request it as a feature. But it's easy to change, so I might
eventually write up a patch to submit (if it actually works, I'm not
so sure it does) and see how it goes.

I tried explaining almost anything interesting in how Pyglet works and
how I actually thought up the solution. If you're unfamiliar with
Pyglet, I suggest doing the following when you're next stuck:

  * If there's an error, see what it says, then look in the files it
tells you.
  * If you can't make sense of it, try reading up, and following the
line the program would follow, but backwards, up until it makes sense.
  * If you're on Linux, the source is in multiple places. 'locate
pyglet' should find it for you. My folder of choice is: /usr/share/
pyshared/pyglet. For Windows, downloading the source itself is a
better idea.
  * Try fixing it with what you've learned from the source code,
following the flow of the code until you reach the error. Even simple
trial-and-error things can help you figure out just what it wants from
you.
  * If you're still having trouble, read through the API on what's
faililng, there might be better documentation there, you might figure
out a link you didn't notice.
  * If it's still not clear, post on here. This doesn't need to be the
last step you try, really. Some problems are just obscure, and can't
be figured out from the source.

Good luck with the thing, and post back if it works the way I
mentioned it (or some other way you might figure out!) so that I can
possibly patch up a ScrollingTexture object eventually.

-- 
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en.

Reply via email to