Using SDL2 in C++ I can load textures with out problem however, I have sprites that are from about 15 different png's that I was hoping I could load them all up and for loop through them. However, when I try this it looks like the image is still .

I am hoping that I wouldn’t have to photo shop them into one image and shift/pan through that considering I have about 3K of images to go through.

can some one provide an example of how this could be done cleanly?

I have been working on this for a while for a bit and yes, this is probably a coding mistake as I am learning C++ still and know that for me to learn is to take up a project.

int main ( int argc, char *argv[] )

    Window = SDL_CreateWindow("NEW WINDOW",100,100,300,300,SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
    renderer = SDL_CreateRenderer(Window,-1,SDL_RENDERER_ACCELERATED);

    if (Window == NULL)
        std::cout << "Window Couldn't be Created!\n";

    struct imagebatch
        const char *image;

    imagebatch foto;

    //  A for loop is needed here to loop through Healer0.png Healer1.png to Healer15.png
    foto.image = "DData/towners/healer/healer/Healer0.png";
    HealerTexture = IMG_LoadTexture(renderer,foto.image);

    //loads an image;
    //HealerTexture = IMG_LoadTexture(renderer,"DData/towners/healer/healer/Healer0.png");

    //defines the rect of this image;
    SDL_Rect HealerRect;
    HealerRect.x = 0;
    HealerRect.y = 0;
    HealerRect.w = 90;
    HealerRect.h = 90;

    while (!quit)
        while( SDL_PollEvent( &event ) != 0 )
            //User requests quit
            if( event.type == SDL_QUIT )
                quit = true;

        //Blit everything here.


    return 0;

Full code is here: http://pastebin.com/H5fD6cfX

  • \$\begingroup\$ Can you post the code you're using to loop through the images? That's the correct approach, so your problem is with the code rather than suggestions on how to do it. \$\endgroup\$
    – usm
    Commented Oct 28, 2014 at 17:26
  • \$\begingroup\$ Thanks, I've pasted the code into your post. You can load Healer0.png to Healer15.png yourself, you can do that with a loop and some string manipulation. You should load those into an array of SDL_Textures and then switch the array index of the surface you're drawing with the one you currently want to show. If that doesn't make sense, I can write a full answer. \$\endgroup\$
    – usm
    Commented Oct 28, 2014 at 21:45

2 Answers 2


For loading the images, you could do:

std::vector<imagebatch> fotos;
std::vector<SDL_Texture*> healerTexture;

// Parts of filename string that occur multiple times.
char* filenameBase = "DData/towners/healer/healer/Healer";
char* fileExt = ".png";

// This is 15 because of the number of images there are.
for (int i = 0; i < 15; ++i)
    char filename[64];
    strcpy(filename, filenameBase); // Copy the first part of the filename into the variable filename.

    char fileIndex[4]; // Create a string of length 4 for storing converted numbers.

    sprintf(fileIndex, "%d", i); // Convert i to a number (that's what %d specifies) and store in fileIndex
    strcat(filename, strcat(fileIndex, fileExt)); // Append the number to filename, and then append the file extension to that.

    // Create a new imagebatch to push onto the imagebatch vector.
    imagebatch foto;
    foto.image = filename;

    // Finally, load the filename that we generated.

You may want to double check the string manipulation code, although I highly recommend std::string where possible with boost::lexical_cast since you won't be using it in a very performance sensitive area.

I've also used std::vector which is better because you don't have to pick their max length before compiling the code (amongst other things).

Don't forget to include vector and string at the top of each file if you use them as follows:

#include <vector>
#include <string>

And finally, when you want to render a particular sprite, you can do the following:

int spriteToRender = 5; // Edit this to the sprite you need to render.
  • \$\begingroup\$ Currently going through to make sure I understand and that it works ok.It seems that my compiler doesnt like that Vector isn't a member of std::. Working for a resolution now. \$\endgroup\$
    – LUser
    Commented Oct 29, 2014 at 19:42
  • \$\begingroup\$ Have you put #include <vector> at the top of your file? And note that it's vector with a lower case 'v'. I should probably annotate the code so that it's easier to follow. \$\endgroup\$
    – usm
    Commented Oct 29, 2014 at 19:47
  • \$\begingroup\$ Yes . I was able to make it that far. It for some reason doesn't seem to like types <imagebatch> and <SDL_Texture> \$\endgroup\$
    – LUser
    Commented Oct 29, 2014 at 20:11
  • \$\begingroup\$ Are you placing this code after the definition of the struct imagebatch? SDL_Texture should be available after including the SDL headers. Although, I've realised I've made a mistake and it should be std::vector<SDL_Texture*> because IMG_LoadTexture() returns a pointer to SDL_Texture. \$\endgroup\$
    – usm
    Commented Oct 29, 2014 at 20:19
  • \$\begingroup\$ That works great! I don't suppose you know how to make it slower using GetTicks()of other means? \$\endgroup\$
    – LUser
    Commented Oct 29, 2014 at 20:28

I highly recommend you look into spritesheets, for memory efficiency/performance. There are a couple of tools out there to help you convert your collection of images into a spritesheet, such as SpriteSheetPacker (open source) or my Gimp plugin for it. A quick google found me this sdl2 tutorial on spritesheets.

  • \$\begingroup\$ Thanks This is really cool and works as a possible solution as well. \$\endgroup\$
    – LUser
    Commented Nov 4, 2014 at 3:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .