Hmm... No Im not calling SDL_FreeSurface(). Why and when should i do that? I dont remember Ive read it in any tutorial...
However the function is supposed to load a bitmap in which several small images are. Four small images, each on 16x16 pixels so the total bmp size is 64x16 pixels. The functions purpose is then to "Clip Blit" the images into a vector of frames. The vector is a part of a std::map which holds all the animations. Hehe hard to explain this. :P
This holds all the animations:
// call it with the animation name and you get a vector with all its frames map <string, vector<Frame> > Animation;
The Frame structure looks like this:
struct Frame { SDL_Surface* image; // the actual image in the frame int displayTime; // for how long should the image be displayed before we go on to the next?};
The functions then looks like this:
bool Sprite::LoadAnimations(string filename) { // *** LOCAL VARIABLES ***************************************** stringstream str; // for the log to write in SDL_Surface* SourceFile; // the sourcefile which in all the frames are stringstream DataFile; // the txt file with all the numbers int SpriteSize = 0; // the size of the sprite; int Frames = 0; // how many frames in the animation string sAnimation = "UNDEFINED"; // the name of the sprite SDL_Rect source; // we have this so we now which area in the image sourcefile to take the image from // ************************************************************** str << "Sprite:\tLoading sprite " << filename << " ... "; DataFile << "Sprites\\" << filename << ".txt"; ifstream file(DataFile.str().c_str()); // the txt with all the data file >> SpriteSize; // we need to know how big each frame is str << "Size: " << SpriteSize << " px"; while(1) { if(file.eof()) // have we read all the data yet? break; // read in the data file >> sAnimation; file >> Frames; // load the bmp in which the animation is held SourceFile = SDL_LoadBMP( ("Sprites\\" + filename + "_" + sAnimation + ".bmp").c_str() ); // attempt to load the texture if(SourceFile == NULL) { // NULL, no image is there str << "\n\tFAILED: unable to open bmp file"; Log::Write(str.str()); return false; } // the image was loaded successfully, go on // setting transparency SDL_SetColorKey(SourceFile, SDL_SRCCOLORKEY, SDL_MapRGB(SourceFile->format, 255, 0, 255)); str << "\n\tAnimation: " << sAnimation; Animation[sAnimation].resize(Frames); // we want this many frames for(int frame = 0; frame < Frames; frame++) { // copy all the images source.x = SpriteSize * frame; source.y = SpriteSize; source.h = SpriteSize; source.w = SpriteSize; // the source area is now sorted out SDL_BlitSurface(SourceFile, &source, Animation[sAnimation][frame].image, NULL); // copy it file >> Animation[sAnimation][frame].displayTime; // store the wait time str << "\n\t" << frame << "\t" << Animation[sAnimation][frame].displayTime; // and go on to the next fram; // DEBUG SDL_Surface* img = SDL_LoadBMP( ("Sprites\\" + filename + "_" + sAnimation + ".bmp").c_str() ); // load animation sourceimage Sdl->DrawImg( img, 0, 0); // then draw it (just for now as debug) } } str << "\tSuccess"; Log::Write(str.str()); return true;}
The function reads the data from a .txt file, which holds the data like how many pixels the sprite is in size, how many frames it has, its name, and how many milliseconds it should display each frame. The file for my ninja sprite looks like this:
16running4 500 500 500 500
Any ideas?