SDL2 Concise Tutorial (4): Importing pictures with the SDL_IMAGE library

Series Article Directory

SDL2 Concise Tutorial (1): Using Cmake and Conan to Build an SDL2 Programming Environment

SDL2 brief tutorial (2): Create an empty window

SDL2 brief tutorial (3): display pictures



Integrate SDL_IMAGE

In SDL2 Concise Tutorial (3): Displaying pictures
, we use the function that comes with SDL2 SDL_LoadBMP()to import bmp pictures. But this function can only import pictures in bmp format, and SDL2 does not provide other functions to import pictures in other formats. Fortunately, there is a library called sdl_image that provides such functionality. This article will show you how to use sdl_image to import images in other formats.

Integrating sdl_image in our project is very simple (thanks to the convenience brought by conan), you only need to add the dependency on sdl_image in conanfile.txt:

[requires]
sdl/2.0.20
sdl_image/2.0.5

[generators]
cmake

You can find more information about the sdl_image package at the sdl_image - conan center . Then, please build our code as in SDL2 Concise Tutorial (1): Using Cmake and Conan to build the SDL2 programming environment .


Import images using SDL_IMAGE

sdl_image is very simple to use, first import the header file

#include <SDL_image.h>

Next, call to IMG_Init()initialize sdl_image

IMG_Init(IMG_INIT_JPG);

Currently sdl_image supports four formats, namely:

  • IMG_INIT_JPG
  • IMG_INIT_PNG
  • IMG_INIT_TIF
  • IMG_INIT_WEBP

You can initialize multiple formats at the same time, e.g.

IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);

Remember, call before exiting IMG_Quit()to release resources

IMG_Quit();

Now, use IMG_Load()replace SDL_LoadBMP()to import other image formats

SDL_Surface * image = IMG_Load("PICT3159.JPG");

Finally paste all the code:

#if defined(__cplusplus)
extern "C" {
    
    
#endif
#include <SDL.h>
#include <SDL_image.h>
#if defined(__cplusplus)
};
#endif

#include <iostream>
using namespace std;

int main() {
    
    
    bool quit = false;

    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);
    IMG_Init(IMG_INIT_JPG);

    SDL_Window *window = SDL_CreateWindow("My SDL Empty window",
                                          SDL_WINDOWPOS_UNDEFINED,
                                          SDL_WINDOWPOS_UNDEFINED,
                                          640, 480, 0);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_Surface *image = IMG_Load("sdl_image.jpeg");
    if (image == nullptr) {
    
    
        cerr << "SDL_LoadBMP failed\n";
        return -1;
    }
    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, image);

    for (; !quit;) {
    
    
        SDL_WaitEvent(&event);

        switch (event.type) {
    
    
        case SDL_QUIT: {
    
    
            quit = true;
            break;
        }
        }
        SDL_RenderCopy(renderer, texture, nullptr, nullptr);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyTexture(texture);
    SDL_FreeSurface(image);
    SDL_DestroyRenderer(renderer);
    IMG_Quit();
    SDL_Quit();
    return 0;
}

Summarize

Great! sdl_image can support multiple formats of images, just need to IMG_Loadreplace with SDL_LoadBMP(). You need to initialize and clean up sdl_image, which is very simple.

All the code for this article can be found in sdl2_tutorial .

Guess you like

Origin blog.csdn.net/weiwei9363/article/details/125919266