Sign in to follow this  
madmaurice

SDL: Animation class Problems

Recommended Posts

ok for a jump'n'run game i made a Animation class which takes the tiles surface and an array of tile indexes. with this index and the tile size, the position of the tile on the tileset is calculated. however i made a int[] and i put {9,10,11} in it but {2293416,68221988,2088809675} came out. here's my class:
class SDLAnimation {
private:
        SDL_Surface* tileset;
        int *animtiles;
        int tcount;
        SDL_Rect tsize;
        int ctile;
        int colorkey;
public:
        SDLAnimation(SDL_Surface* tset,int animation[],int tilescount,int tw, int th) {
              tileset = tset;
              animtiles = animation;
              tcount = tilescount-1;
              ctile = 0;
              tsize.w = tw;
              tsize.h = th;
              colorkey = -1;
        }
        void setTransparency(int r, int g, int b) {
             colorkey = (int)SDL_MapRGB(tileset->format,r,g,b);
        }
        void Draw(SDL_Surface *screen, int x, int y,bool next_tile = false) {
             if(next_tile) {
                  ctile = (ctile+1)%(tcount+1);
             }
             SDL_Rect tile;
             tile.x = (animtiles[ctile]*tsize.w)%tileset->w;
             tile.y = ((animtiles[ctile]*tsize.w)-tile.x)/tsize.h;
             printf("tile.x: %d tile.y: %d ctile: %d animtiles[ctile]: %d\n",tile.x,tile.y,ctile,animtiles[ctile]); // Debug...
             tile.h = tsize.h;
             tile.w = tsize.w;
             SDL_Rect offset;
             offset.x = x;
             offset.y = y;
             if(colorkey > -1) {
                   SDL_SetColorKey(tileset,SDL_SRCCOLORKEY,(Uint32)colorkey);
             }
             SDL_BlitSurface(tileset,&tile,screen,&offset);
        }
};
sry but its not commented. the debug line prints:
tile.x: -216 tile.y: -30817 ctile: 0 animtiles[ctile]: 2088809675
tile.x: 192 tile.y: -17131 ctile: 1 animtiles[ctile]: 68221988
tile.x: 0 tile.y: 16126 ctile: 2 animtiles[ctile]: 2293416
is it because i'm using a pointer?

Share this post


Link to post
Share on other sites
Quote:

is it because i'm using a pointer?


Yes, if you are also using a stack allocated array passed to the constructor. The best method is to use a container that has reasonable copy semantics. Such a container is std::vector<>

[hint: use source /source tags to get a pretty box with syntax highlighting...]

class SDLAnimation {

private:

SDL_Surface* tileset;

std::vector<int> tiles;

int tileWidth, tileHeight

int currentTile;


public:

SDLAnimation(SDL_Surface* tileset,const std::vector<int> &tiles,int tileWidth, int tileHeight)
:
tileset(tileset),
tiles(tiles),
tileWidth(tileWidth),
tileHeight(tileHeight),
currentTile(0)
{
assert(!tiles.empty());
}

void setTransparency(int r, int g, int b)
{
SDL_SetColorKey(tileset,SDL_SRCCOLORKEY,SDL_MapRGB(tileset->format,r,g,b));
}

void Draw(SDL_Surface *screen, int x, int y,bool next_tile = false) {

if(next_tile) {

currentTile = (currentTile + 1) % tiles.size();

}

SDL_Rect tile;

tile.x = (tiles[currentTile]*tileWidth)%tileset->w;

tile.y = ((tiles[currentTile]*tileWidth)-tile.x)/tileHeight;

tile.h = tileHeight;

tile.w = tileWidth;

SDL_Rect offset = { x, y };


SDL_BlitSurface(tileset,&tile,screen,&offset);
}

};






I rewrote some of it to be simpler. Apologies for any error introduced, I did not test it. Check especially the tile math in Draw().

To create a tileset:

void foo(SDL_Surface *surface, int tileWidth, int tileHeight)
{
std::vector tiles;
tiles.push_back(9);
tiles.push_back(10);
tiles.push_back(11);

SDLAnimation animation(surface,tiles,tileWidth,tileHeight);

// use animation
}




Incidentally, why call your class "SDL" animation... why not just Animation?

Another issue of pointer ownership: who owns the SDL_Surface pointer? Not your Animation class, seeing as it doesn't free it. It clearly isn't shared (or at least shouldn't be) because each instance of animation appears to modify the surface (by setting the colour key).

Share this post


Link to post
Share on other sites
Quote:

Incidentally, why call your class "SDL" animation... why not just Animation?


Because:
I want a row of classes to be marked as classes which make SDL easier to use. like SDLFade for a logo Fode in Fade out (alpha going to 255 then wait 2 seconds then return to 0 with a black background like commercial games do at the beginning) and SDLTimer (A Wrapper class for SDL_GetTicks()).

Quote:

Another issue of pointer ownership: who owns the SDL_Surface pointer? Not your Animation class, seeing as it doesn't free it. It clearly isn't shared (or at least shouldn't be) because each instance of animation appears to modify the surface (by setting the colour key).


The tileset pointer points to the same as another pointer in main.cpp where it is given as a parameter to SDL_FreeSurface in a self-defined clean_up() function. so the surface is freed just the pointer stays. Do i need to free this pointer? =)

[Edited by - madmaurice on January 4, 2008 6:56:43 AM]

Share this post


Link to post
Share on other sites
Hmm, there is a slight problem here - to somebody else reading your code, which functions are from the SDL library, and which are written by you? Ususally, the prefix is used to denote which library its from, and the suffix can be used to denote which library it uses. (n.b. "somebody else" also includes you in 7 months time).

Try changing it to AnimationSDL.

Just my $0.02

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this