1.下载安装包
下载安装包 SDL2-devel-2.30.2-VC.zip,然后解压到指定目录
此时就安装完了。简简单单。
2.编码使用demo
编写CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
project(sdltest01)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_PREFIX_PATH "C:/Users/Dark/SDL2-2.30.2/")
find_package(SDL2 REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${SDL2_LIBRARIES})
编写main.cpp
#include <iostream>
#include <SDL.h>
#undef main
int main()
{
int run = 1;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *texture = NULL;
SDL_Rect rect; // 长方形,原点在左上角
rect.w = 50; //方块大小
rect.h = 50;
SDL_Init(SDL_INIT_VIDEO);//初始化函数,可以确定希望激活的子系统
window = SDL_CreateWindow("2 Window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);// 创建窗口
if (!window)
{
return -1;
}
renderer = SDL_CreateRenderer(window, -1, 0);//基于窗口创建渲染器
if (!renderer)
{
return -1;
}
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
640,
480); //创建纹理
if (!texture)
{
return -1;
}
int show_count = 0;
while (run)
{
rect.x = rand() % 600;
rect.y = rand() % 400;
SDL_SetRenderTarget(renderer, texture); // 设置渲染目标为纹理
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // 纹理背景为黑色
SDL_RenderClear(renderer); //清屏
SDL_RenderDrawRect(renderer, &rect); //绘制一个长方形
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255); //长方形为白色
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderTarget(renderer, NULL); //恢复默认,渲染目标为窗口
SDL_RenderCopy(renderer, texture, NULL, NULL); //拷贝纹理到CPU
SDL_RenderPresent(renderer); //输出到目标窗口上
SDL_Delay(300);
if(show_count++ > 30)
{
run = 0; // 不跑了
}
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); //销毁窗口
SDL_Quit();
return 0;
}
运行时自己把SDL安装路径下的lib下的对应的库,拷贝到exe目录下
运行结果会弹个框框出来,然后有个正方形在那里动来动去,然后就自动关闭框框。
标签:SDL2,win,texture,renderer,SDL,NULL,安装,rect From: https://www.cnblogs.com/DarkH/p/18153233