Sign in to follow this  

listSDL_Surface* problems

This topic is 3553 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

I'm currently using Dev-C++ and SDL. I'm creating an STL list of objects of the class 'Animation' I created. In this Animation class, I've created a list<SDL_Surface*> property called "frames" so in order to draw one of the frames to the screen I could pass an SDL_Surface* from my list as a parameter to the SDL_BlitSurface function. The problem, however, is I can't seem to render even just one of the frames without the program crashing. Here is the Animation class (actually it's struct for now) definition -- the AnimationInstance enum is also listed because it's used for an Animation instance's id property but is not incredibly important to the struct's functionality:
// The different types of animations that current exist
enum AnimationInstance{
    ANIM_GAME_SAVED,
    ANIM_EXPLOSION,  
};

struct Animation{
    int id;
    list<SDL_Surface*> frames;
    list<SDL_Surface*>::iterator frameIter;
    int frameNumber;
    int totalFrames;
    int loopNumber;
    int totalLoops;
    Coor position;
    
    Animation(int identifier, int frames, int loops){
        id = identifier;
        totalFrames = frames;
        totalLoops = loops;
        frameNumber = 1;
        loopNumber = 1;
        if(totalLoops < 0) totalLoops = 1; // Less than zero loops not allowed 
        if(totalFrames < 1) totalFrames = 1; // Less than 1 frame total not allowed
        position = Coor(0, 0); // Creates animation at location (0,0) by default
    }
    
    Animation(Animation a, Coor pos){ // Makes a copy of an already existing animation to a new Coor
        id = a.id;
        totalFrames = a.totalFrames;
        totalLoops = a.totalLoops;
        frameNumber = 1;
        loopNumber = 1;
        if(totalLoops < 0) totalLoops = 1; // Less than zero loops not allowed 
        if(totalFrames < 1) totalFrames = 1; // Less than 1 frame total not allowed
        position = a.position;
    }
    
    int nextFrame(){
        if(frameNumber != totalFrames){ // If the previous frame was not the last frame in the animation
            frameNumber++; // Move to the next frame in the animation
            if(!frames.empty()) frameIter++; // Move to the next frame in the animation in the SDL_Surface* list (only if this animation uses this list)
        }
        else if(totalLoops == 0){
            frameNumber = 1; // Start at the beginning of the animation again
            
            // Other frame list operations...
            if(!frames.empty()) frameIter = frames.begin(); // If this animation uses the SDL_Surface* list, point to the beginning of that list again
        }
        else if(loopNumber != totalLoops){ // If the previous loop was not the last loop for the animation
            frameNumber = 1; // Start at the beginning of the animation again
            loopNumber++; // Increment which loop number this is
            
            // Other frame list operations...
            if(!frames.empty()) frameIter = frames.begin(); // If this animation uses the SDL_Surface* list, point to the beginning of that list again
        }
        else frameNumber = 0; // Indicates that the animation has reached its end
        
        return frameNumber;
    }
    
    void draw(SDL_Surface*& screen){
        SDL_Surface *frameSurface = NULL;
        frameSurface = (*frameIter); // (*frameIter) == SDL_Surface*
        SDL_Rect rect;
        rect.y = position.y;
        rect.x = position.x;
        rect.w = frameSurface->w; // Width of the current frame's SDL_Surface
        rect.h = frameSurface->h; // Height of the current frame's SDL_Surface
        SDL_Surface *g = NULL;
        g = frameSurface; // frameSurface == SDL_Surface*
    	SDL_BlitSurface(g, NULL, screen, &rect);   
    }
};

I also declared global list of all animations and an animation for an explosion:
list<Animation> animations; // List of all current animations on screen

Animation explosion(ANIM_EXPLOSION, 17, 1); // The explosion animation is 17 frames long and loops once
This is where I'm loading all my images for the explosion animation in my game's Initialize() function:
for(int i = 1; i <= explosion.totalFrames; i++){ // Load all frame images into the explosion animation
        SDL_Surface *frameImage = NULL; // SDL_Surface*
        string frameImagePath = "images/explosion/explosion_" + IntToString(i) + ".bmp";
        frameImage = SDL_LoadBMP(frameImagePath.c_str());
        if(frameImage == NULL) // Check that the image for the sprite was loaded properly
        {
            string errorMessage("The image file at \"" + frameImagePath + "\" could not be properly loaded.");
            cerr << "ERROR: " << errorMessage.c_str() << endl;
            exit(1);
        }
        explosion.frames.push_back(frameImage);
    }

And here's the bit of code that makes my program terminate:
Animation e2(explosion, Coor(20,20)); // Create a copy of the explosion animation and position it at position (20,20)
list<SDL_Surface*>::iterator fIter = e2.frames.begin(); // Just start your own iterator since everything is public at the moment
SDL_Rect dest;
dest.x = 20;
dest.y = 20;
SDL_BlitSurface((*fIter), NULL, g_Screen, &dest); // Blit the first frame to the screen (g_Screen)
Can someone tell me what I'm doing wrong? Any help is much appreciated!

Share this post


Link to post
Share on other sites
Ah! So sorry, this isn't a pointer problem at all -- I failed to realize I was not making copy of the list of SDL_Surface* objects when I called a copy constructor.

Share this post


Link to post
Share on other sites

This topic is 3553 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.

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