Newbie Sprite Question

I’m working on a 2D “sort-of-game, more-like-a-test” and wanted some ideas on this one thing.

The way I figure, I could very easily and likely have more than one of the same “sprite” onscreen at once. I was thinking of storing all of a sprite’s textures (after loading them) inside the sprite’s class, then call for that address when I need it. If I did this, then I think it would be more efficient to just reuse the same textures over, rather than reload the exact same image. The problem is, I’m not sure how to do this.

I was thinking of having one “queen” type object that would hold the actual textures and stuff, and then it could spawn off the real sprite objects as needed. Can someone help me figure out an implementation for this?

It seems like a lot to actually make a special queen class for every enemy or what-not in the game, but would that be the best way?

You could create a SpriteManager class that holds all the sprites statically. Then you just make a call getSprite(name) and it will pull the sprite you need.

There’s quite a few bits and pieces go together to make a decent sprite engine.

A Texture class might be used to hold a big ol’ image with loads of different sprite frames in it.

A SpriteImage class might be used to describe a single sprite frame inside a texture, ie. its x, y, width, height, and hotspot coordinates.

An Animation class might point to a series of SpriteImages and associate each one with a duration; and maybe have a loop flag.

A Sprite would have a current position, a current SpriteImage, optionally an Animation and current sequence number and ticker, and perhaps a few other doodads like layer, alpha and rotation. Note that you don’t actually modify the Animation instance here - all the dynamic stuff needs to be stored in the Sprite.

A SpriteEngine would own all the Sprites and would typically be told to render() all of them in one go. Because an efficient SpriteEngine needs to allocate OpenGL buffers big enough to draw all of its sprites, it tends to be easiest if it is constructed with a fixed maximum number of Sprites but if you’re adventurous you can get it to grow dynamically. The SpriteEngine will also have some kind of tick() method that iterates through all its Sprites and updates the animations of each one.

Cas :slight_smile:

Take a look at Kyra:

http://www.grinninglizard.com/kyra/

It’s a sprite engine for SDL/C++, but it’s worth reading their docs for one example of how such a system may be implemented.

Thanks for the ideas and thanks for the link, cfmdobbie. I’ll try out some of this stuff and take a look at that engine.

Right now, my family is visiting, so all projects are on hold for the week. Sigh I miss my Java.

Anyway, I will most definately be working on this stuff when I’m able to get at my projects again.