SDL2小作业之纹理加载和渲染

462

由于最近要在linux平台上学习,索性换成Linux下的Clion平台用Cmake继续学习了。

Linux下的SDL2环境配置(Ubuntu)

sudo apt install libsdl2-dev libsdl2-image-dev

CMakeLists.txt需要更新的配置:

TARGET_LINK_LIBRARIES(这里写项目名称 SDL2 SDL2_image)

环境配置比Windows下简单很多,虽然Cmake还是很反人类。。

本节任务

用SDL2自带的Texture和Renderer来绘制窗口

运行结果

Lesson7-min

代码

#include <iostream>
#include <string>
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
// CONSTS
const int SCREEN_HEIGHT = 640;
const int SCREEN_WIDTH = 480;
SDL_Window* gWindow = nullptr;//in window
SDL_Texture* gTexture = nullptr; // global texture
SDL_Renderer* gRenderer = nullptr;//global renderer
//FUNCTIONS
bool loadMedia();
void freeSDL();
bool initWindow();
SDL_Texture* loadTexture(std::string path);

bool initWindow()
{
    bool flag = true;
    if (SDL_Init(SDL_INIT_VIDEO)<0)
    {
        printf("Unable to init Video!ERROR: %s\n",SDL_GetError());
        flag = false;
    }
    else
    {
        gWindow= SDL_CreateWindow("SDLSample", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (gWindow == nullptr)
        {
            printf("Unable to creatwindow!\nERROR:%s",SDL_GetError());
            flag = false;
        }
        else
        {
            gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED);
            if (gRenderer== nullptr)
            {
                printf("Renderer could not be created! ERROR:%s", SDL_GetError());
                flag = false;
            } else
            {
                SDL_SetRenderDrawColor(gRenderer,0XFF,0XFF,0XFF,0XFF);
                int IMG_FLAG =IMG_INIT_PNG;
                if (!(IMG_Init(IMG_FLAG) & IMG_FLAG))
                {
                    printf("Image could not be loaded! ERROR:%s\n", IMG_GetError());
                    flag = false;
                }
            }
        }
    }
    return flag;
}

SDL_Texture* loadTexture(std::string path)
{
    SDL_Texture * tmpTexture = nullptr;
    SDL_Surface * loadedSurface = IMG_Load(path.c_str());
    if (loadedSurface == nullptr)
    {
        printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
    } else
    {
        tmpTexture = SDL_CreateTextureFromSurface(gRenderer,loadedSurface);
        if (tmpTexture == nullptr)
        {
            printf("Unable to CreateTextureFromSurface! SDL_image Error: %s\n", SDL_GetError());
        }
        SDL_FreeSurface(loadedSurface);
    }
    return tmpTexture;
}
bool loadMedia()
{
    bool flag = true;
    gTexture = loadTexture("/home/wenqi/CLionProjects/SDLGame/screenshot.png");
    if (gTexture== nullptr)
    {
        printf("Unable to load Media!");
        flag = false;
    }
    return flag;
}


void freeSDL()
{
    SDL_DestroyTexture(gTexture);
    gTexture = nullptr;

    SDL_DestroyRenderer(gRenderer);
    gRenderer = nullptr;

    SDL_DestroyWindow(gWindow);
    gWindow = nullptr;

    SDL_Quit();
    IMG_Quit();

    return;
}

int main() {
    if (!initWindow())
    {
        printf("unable to init window!\n");
    } else
    {
        if (!loadMedia())
        {
            printf("Failed to load media!\n");
        } else
        {
            bool quit = false;
            SDL_Event event ;
            while (!quit)
            {
                while (SDL_PollEvent(&event)!=0)
                {
                    if (event.type==SDL_QUIT){
                        quit = true;
                    }
                }
                SDL_RenderClear(gRenderer);
                SDL_RenderCopy(gRenderer,gTexture, nullptr, nullptr);
                SDL_RenderPresent(gRenderer);
            }
        }
    }
    return 0;
}

新函数和变量学习

SDL_Renderer * SDL_CreateRenderer(SDL_Window * window,
				int index, 
				Uint32 flags);

Wiki连接

SDL_Texture * SDL_CreateTextureFromSurface(SDL_Renderer * renderer, 
					SDL_Surface * surface);

Wiki链接

int SDL_RenderClear(SDL_Renderer * renderer);

Wiki链接

int SDL_RenderCopy(SDL_Renderer * renderer,
                   SDL_Texture * texture,
                   const SDL_Rect * srcrect,
                   const SDL_Rect * dstrect);

Wikil链接