SDL(2) - load pictures

load BMP

1. Use SDL_init to initialize the SDL library

2. Use SDL_CreateWindow to create a window

3. Use SDL_GetWindowSurface to get the surface of the created window

4. Use SDL_LoadBMP to load a BMP image

5. Use SDL_BlitSurface to copy the loaded bmp surface to the surface of the window

6. Use SDL_UpdateWindowSurface to update to the window

7. Use SDL_FreeSurface to release the requested space

8. Use SDL_DestroyWindow to destroy the window

9. Use SDL_Quit to release the SDL library

The complete code is as follows

#include <iostream>
#include <sdl.h>

const int SCREEN_WIDTH = 640;
const int  SCREEN_HEIGHT = 480;

SDL_Window* gWindow = nullptr;
SDL_Surface* gScreenSurface = nullptr;
SDL_Surface* gHelloWorld = nullptr;

bool init()
{
	bool success = true;

	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		printf("SDL could not initialize!SDL Error:%s\n", SDL_GetError());
		success = false;
	}
	else
	{
		//create window
		gWindow = SDL_CreateWindow("SDL tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
		if (gWindow == nullptr)
		{
			printf("window could not created!SDL_Error:%s\n", SDL_GetError());
			success = false;
		}
		else
		{
			//get window surface
			gScreenSurface = SDL_GetWindowSurface(gWindow);

		}

	}

	return success;
}


bool loadMedia()
{
	//loading success flag
	bool success = true;

	//load splash image
	gHelloWorld = SDL_LoadBMP("../BMP2.bmp");
	if (gWindow == nullptr)
	{
		printf("unable to load image!SDL_ERROR%s\n",SDL_GetError());
		success = false;
	}

	return success;
}


void close()
{
	SDL_FreeSurface(gHelloWorld);
	gHelloWorld = NULL;

	SDL_DestroyWindow(gWindow);
	gWindow = NULL;

	SDL_Quit();

}


int main(int argc, char* args[])
{

	if (!init())
	{
		printf("failed to initialize!\n");
	}
	else
	{
		//load media
		if (!loadMedia())
		{
			printf("failed to load media!\n");
		}
		else
		{
			SDL_BlitSurface(gHelloWorld, nullptr, gScreenSurface, nullptr);

			//update the surface
			SDL_UpdateWindowSurface(gWindow);

			SDL_Event e;
			bool quit = false;

			while (quit == false)
			{
				while (SDL_PollEvent(&e))
				{
					if (e.type == SDL_QUIT)
						quit = true;
				}
			}
		}


	}

	close();
	return 0;
}

load PNG

Since the default SDL library can only load pictures in BMP format, if you need to load pictures in other formats such as png, you need to download an extension library SDL_image

Here we download SDL2_image-devel-2.6.3-VC.zip SDL_image

 The loading steps are similar to those of loading BMP, here are the following points to note

1. You need to use IMG_Init to initialize the iamge library

2. Use IMG_Load to load png images (before using SDL_LoadBMP to load BMP)

3. Use IMG_Quit to release the image library

The following is the complete code

#include <iostream>
#include <sdl.h>
#include <sdl_image.h>
#include <string>

using namespace std;

const int SCREEN_WIDTH = 640;
const int  SCREEN_HEIGHT = 480;


SDL_Surface* loadSurface(std::string path);

SDL_Window* gWindow = nullptr;

SDL_Surface* gScreenSurface = nullptr;

SDL_Surface* gPNGSurface = nullptr;


bool init()
{
    bool success = true;
    //initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL_ERROR:%s\n", SDL_GetError());
        success = false;
    }
    else
    {
        //create window
        gWindow = SDL_CreateWindow("SDL load PNG", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if (gWindow == nullptr)
        {
            printf("SDL_ERROR:%s\n", SDL_GetError());
            success = false;

        }
        else
        {
            //initialize PNG loading
            int imgFlag = IMG_INIT_PNG;

            if (!(IMG_Init(imgFlag) & imgFlag))
            {
                printf("SDL_IMAGE could not initialize!SDL_IMAGE ERROR: %s\n", IMG_GetError());
                success = false;
            }
            else
            {
                //get window surface
                gScreenSurface = SDL_GetWindowSurface(gWindow);
            }
        }
        
    }

    return success;
}


SDL_Surface* loadSurface(string path)
{
    SDL_Surface* optimizedSurface = nullptr;

    //load image at specified path
    SDL_Surface* loadSurface = IMG_Load(path.c_str());
    if (loadSurface == nullptr)
    {
        printf("load image error: %s\n", IMG_GetError());
    }
    else
    {
        //convert surface to screen format
        optimizedSurface = SDL_ConvertSurface(loadSurface, gScreenSurface->format, 0);
        if (optimizedSurface == nullptr)
        {
            printf("unable to optimize image: %s\n", SDL_GetError());
        }

        //get rid of old loaded surface
        SDL_FreeSurface(loadSurface);
    }

    return optimizedSurface;
}

bool loadMedia()
{
    //Loading success flag
    bool success = true;

    //Load PNG surface
    gPNGSurface = loadSurface("../picture/loaded.png");
    if (gPNGSurface == NULL)
    {
        printf("Failed to load PNG image!\n");
        success = false;
    }

    return success;
}

void close()
{
    SDL_FreeSurface(gPNGSurface);
    gPNGSurface = nullptr;

    SDL_DestroyWindow(gWindow);
    gWindow = nullptr;

    IMG_Quit();
    SDL_Quit();
}


int main(int argc, char* argv[])
{

    if (!init())
    {
        printf("Failed to initialize!\n");
    }
    else
    {
        //Load media
        if (!loadMedia())
        {
            printf("Failed to load media!\n");
        }
        else
        {
            //Main loop flag
            bool quit = false;

            //Event handler
            SDL_Event e;

            //While application is running
            while (!quit)
            {
                //Handle events on queue
                while (SDL_PollEvent(&e) != 0)
                {
                    //User requests quit
                    if (e.type == SDL_QUIT)
                    {
                        quit = true;
                    }
                }

                //Apply the PNG image
                SDL_BlitSurface(gPNGSurface, NULL, gScreenSurface, NULL);

                //Update the surface
                SDL_UpdateWindowSurface(gWindow);
            }
        }
    }

    //Free resources and close SDL
    close();

    return 0;
}

Guess you like

Origin blog.csdn.net/yunxiaobaobei/article/details/130186814