Clion 编译SDL实例

  1. 先安装SDL以提供SDL支持
  2. 安装CLION以开发SDL程序
  3. 代码如下:

main.cpp

include
include
int main(){
    SDL_Surface *screen;
    Uint32 color;
    int x;
    if(SDL_Init(SDL_INIT_VIDEO)<0){
        fprintf(stderr,"SDL运行错误%s\n",SDL_GetError());
        exit(1);
    }
    screen=SDL_SetVideoMode(640,480,16,SDL_SWSURFACE);
    if(screen==NULL){
        fprintf(stderr,"程序运行的时候会出现一个640*480大小,16位色的屏幕%s",SDL_GetError());
        exit(1);
    }
    atexit(SDL_Quit);
    for(x=0;x<=255;x+=1)
    {
       color=SDL_MapRGB(screen->format,255,255,x);
       SDL_FillRect(screen,NULL,color);
       SDL_UpdateRect(screen,0,0,0,0);
    }
    SDL_Delay(6000);
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.9)
project(SDLDemo)

set(CMAKE_CXX_STANDARD 11)

add_executable(SDLDemo main.cpp)

target_link_libraries(SDLDemo SDL pthread)

  1. 效果图:
    Clion 编译SDL实例_第1张图片

你可能感兴趣的:(Linux,C/C++)