SDL2 - Is There A Better Way To Load Sprites?


Started on a new SDL2 project code named: “C64GKGM2”.
I’m building a newer SDL2 engine which is in progress.
(project will use straight up SDL2 with no support libs)

Sprite loading is a pain, is there a better way?

bool Visuals::LoadSpritesAndInitialize(void)
char filePath[256];
SDL_Surface* tempSurface;
Uint32 textureFormat;
int textureAccess;
int textureWidth;
int textureHeight;

    for (int index = 0; index < NumberOfSprites; index++)
        Sprites[index].Texture = NULL;

    for (int index = 0; index < NumberOfSprites; index++)
        strcpy(filePath, "~");

            case 1:
                strcpy(filePath, "data/visuals/Screen-Fade.bmp");

            case 10:
                strcpy(filePath, "data/visuals/FAS-Statue.bmp");

            case 20:
                strcpy(filePath, "data/visuals/SDL-Logo.bmp");


        if (filePath[0] != '~')
            tempSurface = SDL_LoadBMP(filePath);
            if (tempSurface == NULL)
                printf( "SDL2 load BMP failed: %s\n", SDL_GetError() );
                CoreFailure = true;

            Sprites[index].Texture = SDL_CreateTextureFromSurface(Renderer, tempSurface);

            Sprites[index].ScreenX = 320;
            Sprites[index].ScreenY = 240;
            Sprites[index].ScaleX = 1.0;
            Sprites[index].ScaleY = 1.0;
            Sprites[index].RotationDegree = 0;
            Sprites[index].RedHue = 255;
            Sprites[index].GreenHue = 255;
            Sprites[index].BlueHue = 255;
            Sprites[index].Transparency = 255;
            Sprites[index].Smooth = false;
            Sprites[index].FlipX = false;
            Sprites[index].FlipY = false;

            Sprites[index].AnimationTimer = 0;

            SDL_QueryTexture(Sprites[index].Texture, &textureFormat, &textureAccess, &textureWidth, &textureHeight);
            Sprites[index].TextureWidthOriginal = textureWidth;
            Sprites[index].TextureHeightOriginal = textureHeight;



Let me know, thanks!


First screenshot of dev:

First of all: have you thought about loading your image filenames from an external file? I gave you this advice in this thread:

Secondly: you have a few default values for your sprite objects and instead of setting all those values at creation, you can just set those values in the sprite object’s constructor. By doing that, the second for loop will look a lot cleaner. Also, in each sprite object’s constructor, set Texture to nullptr, instead of doing it as you’re doing it now. If you set Texture to nullptr in each sprite object’s constructor, you can just remove the first for loop altogether.

I don’t know the exact Implementation of the sprite class but as Naith mentioned you can use a constructor the default all these values for you.

If you have access and are using Cpp already then use it,

struct Sprite {
        : texture( nullptr )
        , screenX( 320 )
        , screenY( 240 ) {
    SDL_Texture* texture;
    int32_t screenX;
    int32_t screenY;

Also I don’t think texture vars you create at the start are needed at all.
You should be able to do something like.

Sprites[index].Texture = SDL_CreateTextureFromSurface(Renderer, tempSurface);
SDL_QueryTexture(Sprites[index].Texture, NULL, NULL, &Sprites[index].TextureWidthOriginal, &Sprites[index].TextureHeightOriginal); 

The I would look into cleaning up how you load from file.
You could again as Naith mentions read the paths from a file