Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm making a game in SDL and I am using an Array of Rects to store values of sprite sheet clip's dimensions. When I call the function and pass in one of my sprite sheet textures with it's corresponding Rect array, it sets the values of those Rects for that sprite sheet. Currently it is working for one of my sprite sheet's but I can't understand why it isn't working for the other.

In my TextureManager.h I declared these Rects

SDL_Rect playerSpriteClips[27];
SDL_Rect playerStandStillRightClips[22];

For the Clip setting function I pass in how many frames are in the sprite sheet, the rect array, the TextureManager object associated with that sprite sheet texture, the number of character frames per row, and how many rows there are. With the characters per row and number of rows, I can find the width and height of each character frame. I then run a for loop for as long as there are frames and each time it loops it sets the values for the entire array.

Inside AnimationClips.cpp

void AnimationClips::setClips(int numFrames, SDL_Rect clipRect[], TextureManager texture, int rowChars, int columnChars)
{
    int sheetWidth = texture.getWidth();
    int sheetHeight = texture.getHeight();

    int characterWidth;
    int characterHeight;

    int widthMultiplier = 0;
    int heightMultiplier = 0;

    characterWidth = sheetWidth / rowChars;
    characterHeight = sheetHeight / columnChars;

    for (int i = 0; i < numFrames; i++)
    { 
        //If finish one row, go to the bottom left of the next row
        if (characterWidth * widthMultiplier >= sheetWidth)
        {
            heightMultiplier++;
            widthMultiplier = 0;
        }
        //set the x values until it reaches the end of the row
        if (characterWidth * widthMultiplier <= sheetWidth)
        {
            clipRect[i].x = 0 + (characterWidth * widthMultiplier);
        }

        clipRect[i].y = 0 + (characterHeight * heightMultiplier);
        clipRect[i].w = characterWidth;
        clipRect[i].h = characterHeight;

        widthMultiplier++;
    }

}

I load the textures up in my loadMedia function in my Application class and then call this function, passing in the number of frames, the Rect array, the texture object, and the characters per row and number of rows

//Load the sprite sheet
(playerTextureRight.loadFromFile("playerright.png"))
//Set the sprite sheet clips
animationClips.setClips(27, playerSpriteSheet.playerSpriteClips, playerSpriteSheet, 7, 4);

This right here sets the Rect array for this sprite sheet perfectly and it animates. My problem is that when I call the function again to set the values for a different Rect array, it doesn't work and all the values of that rect remain 0.

This time I call the setClips function and use the playerStandStillRightClips rect array.

animationClips.setClips(22, playerSpriteSheet.playerStandStillRightClips, playerStandStillRight, 22, 1);

This function call does absolutely nothing for some reason and does not set the values for that Rect. I don't know what the problem is

share|improve this question
1  
You seem to be loading the file into playerTextureRight but call setClips with argument playerSpriteSheet. Did you check your variables? –  Sam Hocevar Oct 9 '14 at 4:57
    
Now, it's a good reason to invest some time into learning your debugger! –  Petr Abdulin Oct 9 '14 at 10:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.