Ok I've come closer to the problem I think. When I read from animations.info I don't jump down one row after I've read in the number of animations...
Fixed part of the source:
// Read by the "Animations:" string in the beginning of the file then read in how many animations there are to tempi fin>>temps>>tempi; m_animations.resize(tempi); // Resize the vector to the number of animations there are getline(fin,temps); for(int i=0; i<m_animations.size(); i++) { getline(fin,temps); // Read the folder name for the animation m_animations.at(i).name=temps; // Assign the folder name to the animations name std::cout<<"Animation "<<i<<". "<<m_animations.at(i).name<<std::endl; } std::cout<<std::endl;
I've also added some debug text that goes to stdout.txt... I'll post the new load and draw functions...
One thing...now I only get the error 2/10 times, I don't know why. But even when I don't get the error nothing gets draw on the screen...
I also tested comment out the animation part of the drawing function an tried to draw the first image of the first animation, but it didn't work.
Here's the new functions:
// This function loads a sprite from a directory, first it checks how many different animations // the sprite has, then loads the frames of every animation. The width and height of the sprite // is decided by the first of the first animation bool sprite::load(std::string dir, SDL_Surface *dest) { // Temporary variables to receive the file input std::string temps; int tempi; // Make a string by adding the dir and a specified file std::string file=dir+"/animations.info"; std::ifstream fin; // Our fstream object, read only fin.open(file.c_str()); // Open the file if(fin==NULL) { return false; } // Return if the file don't exist or some other error else { // Read by the "Animations:" string in the beginning of the file then read in how many animations there are to tempi fin>>temps>>tempi; m_animations.resize(tempi); // Resize the vector to the number of animations there are getline(fin,temps); for(int i=0; i<m_animations.size(); i++) { getline(fin,temps); // Read the folder name for the animation m_animations.at(i).name=temps; // Assign the folder name to the animations name std::cout<<"Animation "<<i<<". "<<m_animations.at(i).name<<std::endl; } std::cout<<std::endl; } fin.close(); // We are done with this file fin.clear(); // We clear the handle to be able to open a new file with the same handle // In this loop we'll load the frames of every animation for(int i=0; i<m_animations.size(); i++) { // Make a filename of the dir and the name of the animation and the file containing the frames data // Example: dir="gfx/sprites/hero" animation name="walkleft" then the whole path becomes: // "gfx/sprites/hero/walkleft/frames.info" file=dir+"/"+m_animations.at(i).name+"/frames.info"; fin.open(file.c_str()); // Open the file if(fin==NULL) { return false; } // Exit if something unexpected happens else { fin>>temps>>tempi; // Read past the "Frames:" string in the file and read the number of frames this animation has m_animations.at(i).frames.resize(tempi); // Resize the vector to have place for all the frames // This loop goes trough all the filenames and loads the frame image to it's surface for(int k=0; k<m_animations.at(i).frames.size(); k++) { fin>>temps>>tempi; // Read the filename and the delay for the frame std::string img; // Another temporary string to load the image img=dir+"/"+m_animations.at(i).name+"/"+temps; // We do the same thing as above, make a whole path for the file m_animations.at(i).frames.at(k).img=load_IMG(img); // Load the image and convert it to the format SDL uses std::cout<<"Frame "<<k<<" of animation "<<i<<" has the path: "<<img<<" and the delay "<<tempi<<std::endl; if(m_animations.at(i).frames.at(k).img==NULL) { std::cerr<<"The frame: "<<img<<" couldn't be loaded!\n"; } m_animations.at(i).frames.at(k).delay=tempi; // Set the delay for the frame } fin.close(); // We are done with the file fin.clear(); // We clear the handle to be able to open a new file with the same handle } std::cout<<std::endl; } m_rect.x=0; // Set the x value to 0 m_rect.y=0; // Set the y value to 0 // Set the width and height of the sprite to the width and height of the first frame of the first animation m_rect.w=m_animations.at(0).frames.at(0).img->w; m_rect.h=m_animations.at(0).frames.at(0).img->h; m_animation=0; // Use the first animation std::cout<<"\nThe width of the first frame of the first animation: "<<m_rect.w<<std::endl; return true; } // This function draws the sprite, it animates automatically, if it isn't paused void sprite::draw() { if(m_paused==false) // Check if the sprite animation is paused { // Check if the time we showed the last frame + the delay of this frame is greater or equal to SDL_GetTicks() if(m_lastframe+m_animations.at(m_animation).frames.at(m_animations.at(m_animation).framenr).delay>=SDL_GetTicks()) { // If ot is change to the next frame m_animations.at(m_animation).framenr++; // If it is the last frame of the animation we'll loop it, setting the framenr to the the first frame if(m_animations.at(m_animation).framenr==m_animations.at(m_animation).frames.size()) { m_animations.at(m_animation).framenr=0; } m_lastframe=SDL_GetTicks(); // We showed this frame now, save the time } } // Blit the frame to the surface we assigned the sprite to SDL_BlitSurface(m_animations.at(m_animation).frames.at(m_animations.at(m_animation).framenr).img, NULL, m_dest, &m_rect); }
By the way, JWindebank I could mail you my sprite file if you'd like.
//walle