Dentho
Dentho

Reputation: 55

C++ image loading with SDL2 and SDL_image

I have a question about image loading in SDL2 with the SDL_image libary.

Unfortunatly i dont find any information about IMG_LoadTexture in the SDL_image documentation. So what is the difference between using IMG_LoadTexture to directly load an PNG image as texture and using IMG_Load to load a surface an then convert it to an texture with SDL_CreateTextureFromSurface? Is there any benefit in using one over the other?

Upvotes: 2

Views: 13386

Answers (2)

Owl
Owl

Reputation: 1562

I've just finished writing a GUI in SDL. So IMG_Load is the old way to load in SDL images, from SDL 1 i believe. The way it used to work is that you'd have SDL surfaces, and then you'd merge them together and then blit them to the screen, or blit sections of the surfaces to other surfaces, using masks etc. The problem is some stuff - for example drawing lines, now requires a renderer.

Renderers pull in the new features of SDL2. It also means that you can't just blit your surfaces necessarily to a texture without converting it first.

So, in summary, if you can get away with using IMG_load and using the old SDL features, do so because it's more intuitive. If you are planning to draw any lines at all, or anything that use the SDL renderer, then you'll need to learn how to convert between surfaces and textures!

Regarding your original question, because i realise i'm not answering it very well, normally it's best to use the right function calls, such as IMG_LoadTexture directly, rather than IMG_Load and then convert it to a texture. SDL talks to the hardware directly and has a surprising amount of optimisation. Converting the surface to a texture, presumably involves blitting, which means copying a substantial amount of memory.

However, it seems that in this case at the time of writing this, there is absolutely no difference at all. The function IMG_LoadTexture does exactly the same thing.

But once again, check, you might not need textures at all, if not, you could save yourself some work ;)

Upvotes: 2

keltar
keltar

Reputation: 18409

When in doubt, check source (easily browsable at https://hg.libsdl.org/) - https://hg.libsdl.org/SDL_image/file/ca95d0e31aec/IMG.c#l209 :

SDL_Texture *IMG_LoadTexture(SDL_Renderer *renderer, const char *file)
{
    SDL_Texture *texture = NULL;
    SDL_Surface *surface = IMG_Load(file);
    if (surface) {
        texture = SDL_CreateTextureFromSurface(renderer, surface);
        SDL_FreeSurface(surface);
    }
    return texture;
}

Upvotes: 9

Related Questions