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_Surface *loadedImage = nullptr;
    SDL_Texture *texture = nullptr;
    loadedImage = SDL_LoadBMP(file.c_str());

    if (loadedImage != nullptr)
        texture = SDL_CreateTextureFromSurface(renderer, loadedImage);
        std::cout << SDL_GetError() << std::endl;
        return texture;

How could I switch it up to load PNG files?

EDIT: Woops, forgot to add my method of loading PNGs.

SDL_Texture* grass_image = nullptr;
grass_image = IMG_LoadTexture(renderer, "res/grass.bmp");
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?


  • \$\begingroup\$ 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 \$\endgroup\$
    – Evan
    Commented Jul 13, 2013 at 16:22
  • \$\begingroup\$ 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. \$\endgroup\$
    – Connor W
    Commented Jul 13, 2013 at 16:24
  • 1
    \$\begingroup\$ Are you seriously asking how to write a function in C++? \$\endgroup\$ Commented Jul 14, 2013 at 2:36

3 Answers 3


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.

Website and Documentation


SDL 2 SDL_image minimal runnable example


#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;

        500, 500,
        0, &window, &renderer
    texture = IMG_LoadTexture(renderer, "flower.png");
    while (1) {
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
    return EXIT_SUCCESS;

GitHub upstream.

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


enter image description here

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

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

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


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."

  • \$\begingroup\$ 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. \$\endgroup\$
    – Connor W
    Commented Jul 13, 2013 at 16:28

You must log in to answer this question.

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