10
\$\begingroup\$

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);
        SDL_FreeSurface(loadedImage);
    } 
    else
        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?

Thanks!

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

3 Answers 3

11
\$\begingroup\$

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

\$\endgroup\$
1
5
\$\begingroup\$

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);
    texture = IMG_LoadTexture(renderer, "flower.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;
}

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
./main

Outcome:

enter image description here

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

\$\endgroup\$
3
4
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\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
    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 .