vs cmake 配置 SDL2

2020-10-30  本文已影响0人  FM_1ad7

一、下载SDL window库

二、配置cmakeList

cmake_minimum_required (VERSION 3.8)

set(SDL2_LIBRARY M:/PcPlayer/SDL2-2.0.12/lib/x64)

include_directories("M:/PcPlayer/ffmpeg/include")

include_directories("M:/PcPlayer/SDL2-2.0.12/include")

# 将源代码添加到此项目的可执行文件。

add_executable (PcPlayer "PcPlayer.cpp" "PcPlayer.h")

target_link_libraries( PcPlayer  ${SDL2_LIBRARY}/SDL2.lib)

三、拷贝SDL2.dll 到 C:\Windows\System32 目录下,不然会导致找不到

四、实现代码

#include "PcPlayer.h"

using namespace std;

extern "C" {

    #include <SDL.h>

}

#undef main

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

{

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window *pWindow = NULL;

    SDL_Renderer  *pRender = NULL;

    pWindow  = SDL_CreateWindow("SDL2 Window", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS);

if (!pWindow)

{

       cout << "window create failed" << endl;

       SDL_Quit();

       return -1;

}

cout << "Window Create Success" << endl;

//3 创建Render

pRender = SDL_CreateRenderer(pWindow, -1, 0);

if (!pRender)

{

SDL_Log("Create Render Failed");

SDL_DestroyWindow(pWindow);

return -1;

}

SDL_SetRenderDrawColor(pRender, 255, 0, 0, 255);  //设置窗口颜色,默认是黑色

SDL_RenderClear(pRender);    //清空之前的数

//4 显示render

SDL_RenderPresent(pRender);

SDL_Delay(30000);  //延时显示30秒,不然窗口一闪而过

// 销毁窗口

SDL_DestroyWindow(pWindow);

// SDL退出

SDL_Quit();

return 0;

}

上一篇下一篇

猜你喜欢

热点阅读