8
\$\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\$
  • \$\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 '13 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 '13 at 16:24
  • \$\begingroup\$ Are you seriously asking how to write a function in C++? \$\endgroup\$ – Trevor Powell Jul 14 '13 at 2:36
10
\$\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\$
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\$
  • \$\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 '13 at 16:28
1
\$\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\$
  • 1
    \$\begingroup\$ Generally we like comments and/or description of what the code does, not just a bloc of code! \$\endgroup\$ – Alexandre Vaillancourt Jan 17 '17 at 1:11
  • \$\begingroup\$ Btw, it's easier to use IMG_LoadTexture(). \$\endgroup\$ – HolyBlackCat Jan 17 '17 at 1:25
  • 1
    \$\begingroup\$ @AlexandreVaillancourt Thanks for feedback. Philosophy here: 1) This code was self-explanatory 2) It is also good to have something that you can copy paste and it just works to try it out. | But more explanation could be a third thing of value to be added as well. \$\endgroup\$ – Ciro Santilli 新疆改造中心996ICU六四事件 Jan 17 '17 at 20:57
  • \$\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\$ – Ciro Santilli 新疆改造中心996ICU六四事件 Jan 17 '17 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.