Jump to content
  • Advertisement
Sign in to follow this  

SDL Sprite Class C++ version 1d

This topic is 3017 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone I wrote a Sprite class in C++ using SDL (uses BMP images)

It contains all your basic Sprite functions including animation, rotations, zooming, plus some other functions

The entire source/documentation/examples can all be downloaded at the below link:

It is easy for me to say that it seems easy to use but I wanted the communities input and overall suggestions to improve usability and function.
I pulled some functionality out into a SpriteEffects class which is accessed statically.
Thanks again,

[Edited by - kennycason on July 22, 2010 2:20:16 PM]

Share this post

Link to post
Share on other sites
Oh and here is the header so that you can see some of the included functionality and whether or not it's of interested or relevancy to you.

class Sprite {
Sprite(std::string fileName,int maxFrames, int animationSpeed);
Sprite(SDL_Surface* surface, int maxFrames,int animationSpeed);
virtual ~Sprite();

Sprite* draw(SDL_Surface* buffer, int x, int y);
Sprite* setTransparency(Uint32 color);
Sprite* setTransparency(Uint8 r, Uint8 g, Uint8 b);
Sprite* setSpeed(int animationSpeed);
int getSpeed();
Sprite* start();
Sprite* restart();
Sprite* animate();
Sprite* setLoopToBegin(bool loop);
bool running();
Sprite* stop();
bool isSprite();
int getFrame();
int getFrameIterator();
int getMaxFrames();
int getWidth(); // frame width
int getHeight(); // frame height
Sprite* setWidth(int width);
Sprite* setHeight(int height);
int getSpriteWidth();
int getSpriteHeight();
bool equals(Sprite compare);
SDL_Surface* getSurface();
Sprite* setSurface(SDL_Surface* surface);
Sprite* destroy();

bool isTransparentPixel(int x, int y);
Sprite* setTransparentPixel(int x, int y);
Uint32 getPixel(int x, int y);
int setPixel(int x, int y, Uint32 color);
int setPixel(int x, int y, Uint8 red, Uint8 green, Uint8 blue);
Uint8 getPixel8(int x, int y);
int setPixel8(int x, int y, Uint8 color);
Uint16 getPixel16(int x, int y);
int setPixel16(int x, int y, Uint16 color);
Uint32 getPixel32(int x, int y);
int setPixel32(int x, int y, Uint32 color);

SDL_Surface* getRect(int x, int y, int width, int height);
bool rectCollide(int x1, int y1, Sprite &spriteB, int x2, int y2);
bool pixelCollide(int x1, int y1, Sprite &spriteB, int x2, int y2);
Sprite* rotate90();
Sprite* rotate180();
Sprite* rotate270();
Sprite* flipHorizontal();
Sprite* flipVertical();
Sprite* fade(float fade); // fade from 0 to 100%
Sprite* reverseAnimation();

Sprite* zoom(float z); // percentage to zoom in
Sprite* stretchX(float x); // percentage to stretchheight
Sprite* stretchY(float y); // percentage to stretchwidth
Sprite* stretch(float x,float y); // percentage to strech X and Y


bool loaded;
bool run;
Uint32 speed;
Uint32 width;
Uint32 height;
Uint32 index;
Uint32 indexIterator;
bool loopToBeginning; // if loop = true iterate through animations from 0 to N, then reset to 0
// if loop = false iterate through animations from 0 to N, then from N to 0, and repeat
Uint32 maxFrames;
Uint32 lastAnimated;

SDL_Surface* sprite;

Share this post

Link to post
Share on other sites
Added another character sheet to the demo to demonstrate how load sprite sheet that contains multiple animations, such as the one below.

The default constructor for sprite loads animations in the below format:

(The transparency key can be set anytime)

After loading them with the sprite class, and applying a few different operations they are rendered as such:

Share this post

Link to post
Share on other sites
I also created a TileSheet class to handle tiling and storing tiles, primarily for map editors and games that use tiled maps. It uses this Sprite class, I will upload it later in another thread

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!