3
\$\begingroup\$

Well passing just external PNG to SDL is very easy with the SDL_image library. I have a class that does just that. The code looks like the following:

SDL_Surface* Texture::LoadImg(const char *filename) {
    SDL_Surface* Image = IMG_Load(filename);
    if (Image == NULL ) {
        printf("Unable to load picture%s\n",SDL_GetError());
        return NULL;
    }
    return Image;
}

Later on I can just call it with

//some code
SDL_Texture* Texture = SDL_CreateTextureFromSurface(Renderer,LoadImg(filename));
//some code

But I don't have just a PNG file. I have a binary file that consist of many PNG files inside. Basically I combine binary(PNG) files into one larger file. Later on I just open that binary file and load pictures into memory from one single file. So now I have an array of vectors or pointers or any other types (depending how you read the binary file). Now I need to pass this from memory into SDL or SDL_image to load it as texture. How would I pass already loaded PNG (binary) data into SDL to use as an SDL_Texture?

\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

SDL uses a type called SDL_RWops for this purpose. This is essentially a wrapper around a stream. When you call a function like IMG_Load(filename) it is just a small wrapper around IMG_Load_RW(ops) which constructs the ops using SDL_RWFromFile. You can create an SDL_RWops yourself (it's just a struct with some function pointers) if you have a custom stream type.

There's also a helper function SDL_RWFromMem which creates an SDL_RWops for manipulating bytes in memory like they are a file, which is probably what you're looking for in conjunction with IMG_Load_RW.

\$\endgroup\$
0
\$\begingroup\$

MNG might be worth looking into which basically specifies a single file containing multiple images with PNG like compression. And there are a couple of libraries for assigning the MNG components to SDL textures.

\$\endgroup\$
1
  • \$\begingroup\$ Too bad that the MNG format never really caught on. Standard practice in the gamedev industry is to bundle multiple PNG textures in a ZIP archive or some other standardized or inhouse-developed container format. \$\endgroup\$
    – Philipp
    Aug 17, 2021 at 12:55

You must log in to answer this question.

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