any good way to write sprite/animation classes for a game? [Archive] - C Board

PDA

View Full Version : any good way to write sprite/animation classes for a game?


compjinx
03-26-2002, 02:04 AM
I need a better way of controlling my sprites in my game.
My typical character will have movment animations along still sprites for direction.

I wrote my sprite/animation classes like this:
SpriteControl, you only need one of these per bitmap file
- this is meant for a whole bunch of sprites to be in one bitmap file
-feed it a filename for the bitmap.
- you must feed it a Sprite array, an array of X,Y,Width,Height postions for all the sprites in the file
- you tell it to draw sprite(x) where X is one of the positions on the array.

AnimationControl, feed it a pointer of the above
-feed it a pointer to an initialized SpriteControl class, along with an array of numbers, numbers that point to positions on the Sprite array (see above)
- tell it to draw the animation (with loop, backwards, and pause capabilities)


and thats all I have, is there a better way to do this?
I am using allegro.

SilentStrike
03-26-2002, 04:51 PM
How about making the sprites have a common base?


class DrawingControl {
public:
virtual void draw( /* whatever allegro specific args here, like surface and postion */)=0;
virtual ~DrawingControl;
};

class SpriteControl : public DrawingControl {
public:
SpriteControl(std::string filename);
void draw(/* args */);
~SpriteControl();
private:
// whatever ya need
};

class AnimationControl : public DrawingControl {
public:
AnimationControl(std::string filename);
void draw(/* args */);
~AnimationConrol();
private:
// whatever
};


The filenames in each constructor should be the names of files that contain the appriopriate data. For example, SpriteControl perhaps has some stuff like transparency, clipping information, and a single bitmap file name. It only is capable of drawing one bitmap however. The animation file could contain things like rate of animation, number of frames, followed by number of SpriteControl filenames, and could internally have an array( or preferablly, a std::vector) of SpriteControls.

Each type of drawing file should then have a specific extension, say .sc for SpriteControl, and .ac for Animation control, then you could write a function


// leaves caller responsible for deletion
DrawingControl* allocateControl(std::string filename) {
string extension = filename.substr(filename.rfind('.'));
if (extension == ".ac") {
return new AnimationControl(filename);
} else if (extension == ".sc") {
return new SpriteControl(filename);
}
return NULL;
}


Then, you could have say, some player class

class Player {
public:
Player();
~Player();
void draw() {
switch (movementDirection) {
case MOVE_LEFT: leftImage->draw();break;
case MOVE_RIGHT: rightImage->draw(); break;
// handle rest
}
}

private:
Direction movementDirection;
SomeLocationClass a;
DrawingControl* leftImage;
DrawingControl* rightImage;
DrawingControl* downImage;
DrawingControl* upImage;
};

The player could be loaded from a file that contained 4 DrawingControls, each would be allocated with allocateControl, and you could easily change how the player behaves by just changing the data file... adding another DrawingControl subclass wouldn't break your code either, as you would just have to add the DrawingControl subclass, and then add another extension to allocateDrawingControl.

With this method, you do lose some things, however. You can't tell a DrawingControl to pause. But you could simply use two DrawingControls to do that. For example, say you have an image for the player moving to the right, and another for the player sitting idle. The idle picture could be an animationless SpriteControl, while the movement can be animated with a AnimationControl. When you draw the player moving, you call the moveRight->draw(), but when he is not moving, you call the idle->draw().

Least, that's how I am doing my drawing system in BomberLAN :).

Good luck.

compjinx
03-28-2002, 01:22 AM
WOW! Thank you very much!