Sprite Engine

Started by
7 comments, last by QzarBaron 19 years, 10 months ago
Im working on a simple 2D engine written in C++ with SDL. I am currently working on the Sprite system for the engine. The thing is it has to be reusable for all my games. What I need is a Sprite class that hold data for my Sprite(easy), I also need it to hold animations for the sprite(such as moving north, south, east, west, dying, drawing sword, etc.) These should be held in a data structure and will be created and filled with frames dynamically. I need it so that I can use this Base sprite class to make all the different sprites for my game. I have tried many different approaches to this(I tried Vectors, Lists, Linked Lists, Arrays) But none have worked so far. I need someone to point me on the right track. Maybe im just thinking about it wrong. I tried the BOB system in Tricks of the Windows Game Programming Gurus and found it horrible to use. If anyone has my answer or maybe a better way to do it well I would really appreciate it.
"Go on get out last words are for fools who have not said enough already." -- Karl Marx
Advertisement
Question is a bit vague

Err
Having a Sprite interface that is implemented by everything and has very high level things (like drawAtLocation, initFrames, etc) is a good start
then a base class implementing that
then the inheriting classes may override bits in the base class, and further specilisations could even override that. Then a linked list full of pointers to the interface which are actually implemented child classes would do.

It''s a case of assigning reponsibility to your sprite class - what should a sprite actually know how to do by itself? What counts as the internal bits that never actually need to be seen by the outside world (black box)?

You are kind of also hinting at a Class Factory by the way you are talking... a class whose only reponsibility is to generate instances of a type of sprite. So you could ask the factory for 10 aliens and one hero for example, and perhaps it would create them all by using a copy constructor on an instance it keeps internally of each type of class.

Of course the above might be all complete bollocks. ;-)
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
No you see I dont need a Class Factory what I need is a template class for all sprites. I already have the template designed. I have all the important data about each sprite(xpos, ypos, speed, different states for each sprite, etc.). I already have all the drawing functions, collision detection, and other functions set for this. The problem is my Sprites only hold 1 frame.

What I need is a way for the user of the Sprite Template to be able to create different animations for each sprite. Ok and example would be lets say I have an Alien sprite, I first Construct the Alien from the Sprite Class and set all its basic variables. Then I create different animations for the Alien sprite such as MoveNorth, MoveSouth, MoveEast, MoveWest, Die, AttackPlayer, things like that. Then I load the frames into each animation(either with a script or hard coded). Then I just call a class function that will render a certain animation(ex. Draw(MoveWest)). What I need is a class that holds all these different functions so that the user does not have to deal with how they work.

My greatest consern is reusability. I have created many different sprite systems but they are all based on the game I am working on. What I need is a reusable sprite system along the lines of Andre LaMothe's BOB system(but object oriented).

Come on somone has to be able to point me in the right direction. If not give me some advice on how to do this differently.

[edited by - QzarBaron on June 8, 2004 2:08:41 PM]
"Go on get out last words are for fools who have not said enough already." -- Karl Marx
How bout assigning a texture to your sprite (or surface, whatever you''re using) and then having a function called SetAnimation that takes Animation_ID, # of frames, speed of animation, and a pointer to an array of rectangles or images or whatever you use to define each frame. Then you could play the animation with PlayAnimation. Like so

#define ANIMATION_JUMP 0x0002

RECT myFrames[10] = {{0,0,32,64},{32,0,64,64},...};

Sprite.SetAnimation(ANIMATION_JUMP, 10, 100, myFrames);

later...

Sprite.PlayAnimation(ANIMATION_JUMP);

Store the information for the animations in parallel internal arrays. Then have the sprite take care of the rest, and the user doesn''t have to worry about anything else.
Yes thats basically how the BOB system works in Tricks. I did consider such meathod. If I can''t find anything better I will end up using it.
"Go on get out last words are for fools who have not said enough already." -- Karl Marx
Do I hear an echo? This dud sounds just like id did!

Hey, use linked-list. They are very simple and you can make a template class that is a linked list. If not, make a linked list with each node being an abstract base class.

An abstract base class is very good if you want to make tons of little sprites derived off of one similar base functionality. You would have to figure out what the basic functions that all of your sprites need. Then make then pure virtual.

Example
class CBaseSprite{public:	CBaseSprite( ) {}	virtual ~CBaseSprite( ) {}	bool InitSprite( ) = 0;	void MoveSprite( ) = 0;	void KillSprite() = 0;private:	// TODO : Add sprite variables};


You will have to figure out the linked mechanism for the list for your self; which isn’t that hard. It all really boils down to what type of functionality all of your sprites need to have. Then you declare the functions in the base class as pure virtual then derive all the sprites as needed.

There is a lot more power achieved by using base abstract classes. I will not go into it, because you can look on Google for that. I hope that helped a little bit
Take back the internet with the most awsome browser around, FireFox
Thats pretty much what I have so far except far more complex. I use Vectors to manage my animations what I need now is to manage my animations.
"Go on get out last words are for fools who have not said enough already." -- Karl Marx
"I use Vectors to manage my animations what I need now is to manage my animations." Why is that? It sounds like you are already using Vectors to manage them.
Sorry my bad. I ment to say "I already use Vectors to Manage the Frames in the animations, now I need to manage the Animations"
"Go on get out last words are for fools who have not said enough already." -- Karl Marx

This topic is closed to new replies.

Advertisement