SDL2显示图片

SDL2显示图片

环境:manjaro linux

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

project(test)

add_executable(test main.cpp)
target_link_libraries(test -pthread -lSDL2 -lSDL2_image)

main.cpp

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>

using namespace std;

int main(int argc, char**argv){
    if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0){
        SDL_Log("initialize sdl failed: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Window* window = SDL_CreateWindow("my first SDL",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480,
        SDL_WINDOW_SHOWN);
    if(window == nullptr){
        cout << "create window error\n";
        return 1;
    }

    SDL_Renderer* render = SDL_CreateRenderer(window, -1, 0);
    SDL_Surface* surface = IMG_Load("th.jpg");
    SDL_Texture* tex = SDL_CreateTextureFromSurface(render, surface);
    SDL_RenderClear(render);
    
    SDL_RenderCopy(render, tex, nullptr, nullptr);
    
    SDL_Rect r;
    r.x = 20;
    r.y = 20;
    r.w = 100;
    r.h = 100;
    SDL_RenderSetClipRect(render, &r);
    
    SDL_SetRenderDrawColor(render, 0, 0, 255, 255);
    r.x = 50;
    r.y = 50;
    r.w = 100;
    r.h = 100;
    SDL_RenderDrawRect(render, &r);
    
    SDL_RenderPresent(render);
    

    SDL_Delay(3 * 1000);

    SDL_Quit();
    return 0;
}
上一篇:SDL2笔记01_搭建开发环境和编写helloworld


下一篇:Ubuntu移植FFmpeg和SDL2