其他分享
首页 > 其他分享> > SDL2显示图片

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;
}

标签:显示,include,SDL2,int,test,SDL,main,图片
来源: https://blog.csdn.net/u010745620/article/details/98984578