Ken-Soft

Software and the World

SDL Sprite Class in C++

Posted under C/C++, Game Development, Programming, Software Development by Kenny on Friday 2 January 2009 at 9:58 pm
Bookmark and Share

This is a simple Sprite class that I use for game development in C++ using SDL (www.libsdl.org).
Features include sprite animation, rotation, stretching, transparencies, and other commonly used sprite functions.

I hope this is somewhat useful. The source is available and should be fairly simple to work with.
Feel free to modify it however you want. please comment about any bugs are suggestions that you have. Thanks.

Last Updated
2010 July 18 - implemented a builder design to all setter/modifying operations that return void. They now return the object it’s self, (*this). This results in being able to set up a Sprite with less code. i.e.
sprite->setTransparency(0xFF, 0, 0xFF)->flipHorizontal()->reverseAnimation()->zoom(50);
sprite->animate()->draw(screen, 100, 100);
2010 June 1 - fixed some minor bugs

SDL Sprite C++
Version 1d (With demo & source) SDL_Sprite_1d.zip

Individual source files

Sprite.h

Sprite.cpp

Main.h

main.cpp

To view a demo of some of the Sprites classes features, and have access to some sample Bitmap images used with the Sprite library, download the following zip file. It also contains the project settings that I used in Dev C++ and SDL 1.2.12 to compile.

All examples use these sprites

SDL Sprite C++ SDL Sprite C++

Demo using special effects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    // Initialize SDL, etc
   ...
   ...
   Sprite* s1 = new Sprite("sprites/samus_normal_run.bmp",10,60); // load a BMP that contains 10 frames
                                                // set the animation speed to 60 milliseconds
      // set RGB(255,0,255) as transparent, rotate 180 degrees, flip horizontal and reverse animation
  s1->setTransparency(255,0,255)->rotate180()->flipHorizontal()->reverseAnimation();
   // etc
    ...
    ...
 
    // Main loop
    // clear background to black, RGB(0,0,0)
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
    // animate and draw the sprite
    s1->animate()->draw(screen,0,0);
    SDL_Flip(screen);

3 Comments »

  1. Comment by Swampert — June 26, 2009 @ 1:46 pm

    This is a great library but where is the .lib/.a? I get undefined references.

  2. Comment by Kenny — June 26, 2009 @ 3:59 pm

    Do you have SDL installed on your computer? If not just google “install SDL” or “setup SDL”
    or, if you use linux it’s in the repositories.
    In the case that you are using DeVcpp IDE (freeware), you can also automatically download and install SDL.
    else you have to install it manually.

    Also, don’t forget to set your linker options for the project, this is very important.
    you will have to add -lSDL to the linker options.

    you may also have to play around with the #include by changin it to

    reply with any questions or problems, I’ll help you get everything set up.
    (just let me know the IDE you are using)
    Thanks

  3. Pingback by Ken-Soft » SDL - Simple Space Shooter Game Demo - Part I — September 20, 2009 @ 8:32 pm

    [...] Sprite Class in C++ using SDL [...]

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Copyright © 2009 www.Ken-Soft.com