SDL配置
下载¶
根据自己需求, 选择mingw或者vc版本下载, 我这里下载vc版本
下载地址: Simple DirectMedia Layer - SDL version 2.0.22 (stable) (libsdl.org)
把下载的压缩包解压, 结构如下
失败尝试¶
打开电脑的环境变量设置界面, 网上很多教程, 这里不赘述.
win7或以下的系统可以使用下面rapid environment editor这个软件编辑环境变量.
添加两个变量: INCLUDE和LIB.
分别将include和lib这两个文件夹的位置写入两个变量中.
用cl.exe 编译之后还是失败.
成功的方法¶
mingw + makefile方式
编写文件¶
在同一个目录下创建文件: 一个sdldemo.cpp, 一个makefile, 一个make.bat
sdldemp.cpp:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <iostream>
//#pragma comment( linker, "/subsystem:console /entry:SDL_main" )
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
std::cout << SDL_GetError() << std::endl;
return 1;
}
SDL_Window *win = nullptr;
win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
if (win == nullptr){
std::cout << SDL_GetError() << std::endl;
return 1;
}
SDL_Renderer *ren = nullptr;
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr){
std::cout << SDL_GetError() << std::endl;
return 1;
}
SDL_Surface *bmp = nullptr;
bmp = SDL_LoadBMP("test.bmp");
if (bmp == nullptr){
std::cout << SDL_GetError() << std::endl;
return 1;
}
SDL_Texture *tex = nullptr;
tex = SDL_CreateTextureFromSurface(ren, bmp);
SDL_FreeSurface(bmp);
SDL_RenderClear(ren);
SDL_RenderCopy(ren, tex, NULL, NULL);
SDL_RenderPresent(ren);
SDL_Delay(2000);
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
makefile:
SRC = sdldemo.cpp
SDLDIR = E:\programming\C\Lib\SDL2-2.0.22mingw
CFLAGES = -I $(SDLDIR)\x86_64-w64-mingw32\include -L $(SDLDIR)\lib\x64 -l SDL2main -l SDL2 -O2 #-mwindows #-l mingw32
CC = D:\Qt\Qt5.14.2\Tools\mingw810_64\bin\g++.exe
sdldemo.exe: $(SRC)
$(CC) $(SRC) $(CFLAGES) -o sdldemo.exe
make.bat:
D:\....\mingw810_64\bin\mingw32-make.exe ;你的mingw安装目录下的mingw32-make.exe的路径
pause
编译¶
执行make.bat, 执行生成的exe会遇到缺少DLL的错误, 到SDL目录下找到下面这些文件, 复制到项目目录下, 再执行生成的exe