I'm learning SDL 2.0 at the moment and I've seen this method for loading BMPs:

SDL_Texture* LoadImage(std::string file)
{
SDL_Texture *texture = nullptr;

{
}
else
std::cout << SDL_GetError() << std::endl;
return texture;
}


How could I switch it up to load PNG files?

SDL_Texture* grass_image = nullptr;
SDL_Rect grass_rect;
grass_rect.x = 0;
grass_rect.y = 0;
grass_rect.w = SCREEN_WIDTH;
grass_rect.h = SCREEN_HEIGHT;

if (grass_image == NULL)
std::cout << "Couldn't load grass_image" << std::endl;

while (!quit && mainEvent -> type != SDL_Quit)
{
SDL_RenderCopy(renderer, grass_image, NULL, &grass_rect);
}


I just don't want to have to do that for EVERY image. So how could I put that into a function?

Thanks!

• If the library doesnt come with its own PNG loader then you would either have to write your own, or use a 3rd party library for loading it. My reading indicates to me that SDL already supports png, libsdl.org/projects/SDL_image
– Evan
Jul 13, 2013 at 16:22
• Oh, sorry I forgot to add the method that I already use to load PNGs, but it's not in a function. I'm just looking for a short cut so I don't have to do this (look in the main post) for every single image. Jul 13, 2013 at 16:24
• Are you seriously asking how to write a function in C++? Jul 14, 2013 at 2:36

You should use SDL_Image API, it has one simple function that loads everything:

SDL_Surface *IMG_Load(const char *file);


More precisely, it loads ICO(Icon)/CUR(Cursor)/BMP, PNM (PPM/PGM/PBM), XPM, LBM(IFF ILBM), PCX, GIF, JPEG, PNG, TGA, TIFF, and XV thumbnail formats.

It also has individual format functions such as IMG_isPNG and IMG_LoadPNG_RW.

This lib is one of the most commonly used for image file loading with SDL, I think.

• An alternative is to use stb_image.h Jan 17, 2017 at 1:06

SDL 2 SDL_image minimal runnable example

main.c

#include <stdlib.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

int main(void) {
SDL_Event event;
SDL_Renderer *renderer = NULL;
SDL_Texture *texture = NULL;
SDL_Window *window = NULL;

SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(
500, 500,
0, &window, &renderer
);
IMG_Init(IMG_INIT_PNG);
while (1) {
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
}
SDL_DestroyTexture(texture);
IMG_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}


Compile and run:

sudo apt-get install libsdl2-dev libsdl2-image-dev
gcc -std=c99 -o main -Wall -Wextra -pedantic main.c -lSDL2 -lSDL2_image
./main


Outcome:

Tested on Ubuntu 16.04, GCC 6.4.0, SDL 2.0.4, SDL Image 2.0.1.

• Btw, it's easier to use IMG_LoadTexture(). Jan 17, 2017 at 1:25
• @HolyBlackCat thanks! I was grepping the online docs for texture, but it seems to be for the 1.x version for some reason. Updated. Jan 17, 2017 at 20:57
• Downvoters please explain so I can learn and improve info, thanks. Mar 7, 2021 at 15:10

In order to load PNG files with SDL you need to have the SDL_Image library extension.

http://www.libsdl.org/projects/SDL_image

A description of this library extension:

"SDL_image is an image file loading library. It loads images as SDL surfaces, and supports the following formats: BMP, GIF, JPEG, LBM, PCX, PNG, PNM, TGA, TIFF, WEBP, XCF, XPM, XV."

• YEah, sorry, just updated the main post, that should help things. I know how to load a PNG I'm just looking for a simpler way to do it. Jul 13, 2013 at 16:28