Hi Fred,
Sorry for the delayed response, but I've only just found time :-)
There's a few things going on here, but the crux of sprites in pyglet
is that they're implemented as textured quads in OpenGL. That's
important because it means that sprites are basically:
1. a set of 4 vertices,
2. a set of 4 colour values to blend into the texture (typically only
used for alpha fading control),
3. a reference to a texture ID, and
4. a set of texture coordinates.
The optimal way of animating a pyglet sprite is to:
1. ensure the animation images are all in one texture, and
2. modify those texture coordinates at point 4 each frame the image
needs to change.
A third point, which can complicate things, is to only do the updates
for those sprites on screen. But that's an optimisation to add in
after you've done the rest.
To achieve point 1, you use a texture atlas. The pyglet image module
has these for you, but also note that if you use pyglet.resource.image
to load your images (which is a good idea since pyglet's resource
handling is quite neat and deals with platform stuff) then the images
will automatically be added to a texture atlas. The reason to use a
texture atlas is so that your sprites all share the same texture ID
and there's a single batch rendering call (which means vertex buffer
(VBO) render call at the OpenGL level) which is much faster. Texture
swapping (requiring multiple VBO render calls) can slow things down a
lot as it means a separate pyglet->OpenGL call for each texture.
The easiest way of managing a texture atlas for your purposes is to
use an Animation, as it manages the frames for you (and timing, if you
ask it to).
To achieve point 2 it's enough to identify the image object in the
atlas you wish to use and then just: "sprite.image = new_image_frame"
and pyglet is smart enough to do the rest.
Note that pyglet's Animation class does pretty much all of this for
you - it's what it's for. Could you please indicate where you saw
people recommending against using pyglet.app.run() because that
certainly complicates things a lot, and does explain why your
animation isn't playing (and other pyglet.clock scheduling also won't
work).
Except for some special effects work you almost never want to be
copying OpenGL texture image data around during program running.
I hope this helps,
Richard
On 5 August 2013 07:53, Fred <[email protected]> wrote:
> Hello,
>
> I keep on trying to develop my 2D RPG engine, and I face another slowdown
> problem.
>
> The map of the game is made of tiles (32 x 32 px) and some of them (say,
> around 30 among 500) are animated.
>
> I tried to represent each by a sprite, whose image would be an Animation.
> But that is too slow.
>
> Since most of these tiles are actually the same, I thought that, rather than
> updating the image attribute of each 30 sprites, it would be more clever to
> make those prite.image point to the same texture, and updating this texture
> (by blitting the proper "image" regularly). I assumed fact that copying
> something to a texture would be quite fast, especially if the "something" is
> too a texture.
>
> Unfortunately, I can't figure out how to blit a TextureRegion in a texture.
> blit_to_texture keeps on raising the same error :
> pyglet.image.ImageException: Cannot blit <TextureRegion 32x32> to a texture.
>
> So my two questions :
> 1) Is this idea (modify texture rather than sprite) sufficient to improve
> performances ?
> 2) how to blit a texture on another ?
>
> Thanks in advance,
>
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
--
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.
For more options, visit https://groups.google.com/groups/opt_out.