下载MinGW版的文件

http://www.libsdl.org/release/SDL2-devel-2.0.3-mingw.tar.gz

解压放到mysys下面

运行Makefile

mysys的include目录加入了SDL2的头文件目录

lib目录加入了libSDL2.a等文件

复制一份bin目录下的SDL2.dll以备后用

然后复制mysys的indluce目录下的SDL2目录和lib目录下的几个SDL2.a到MinGW的include和lib目录一份

编译IDE依然使用Codeblocks,设置同Ubuntu

http://www.cnblogs.com/kileyi/p/5005002.html

不同的是windows环境源码要加上一句

#undef main

这个在Visual Studio中使用SDL2也要加这句

 #include <iostream>
#include <SDL2/SDL.h> #define WIDTH 640
#define HEIGHT 480
#define BPP 4
#define DEPTH 32 using namespace std; #undef main int main(int argc, char* argv[])
{
SDL_Surface *screen;
SDL_Event event; int keypress = ;
int h = ; if (SDL_Init(SDL_INIT_EVERYTHING) == - ) {
cout << "error: " << SDL_GetError() << endl;
return ;
} SDL_Window* window = SDL_CreateWindow("hello", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, , , SDL_WINDOW_SHOWN);
if (window == NULL){
cout << "Error: " << SDL_GetError() << endl;
return ;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL){
cout << "Error: " << SDL_GetError() << endl;
return ;
} SDL_Surface* surface = SDL_LoadBMP("bk.bmp");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface); SDL_RenderClear(renderer); //清除屏幕
SDL_RenderCopy(renderer, texture, , ); //把texture画到renderer上
SDL_RenderPresent(renderer); while(!keypress)
{
//DrawScreen(screen, h++);
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
keypress = ;
break;
case SDL_KEYDOWN:
keypress = ;
break;
}
}
} SDL_Quit();
return ;
}

编译可能出现

winapifamily.h: No such file or directory

搜索到这贴,说是SDL2.0.3的一个bug,下载修正的SDL_platform.h文件替换

http://stackoverflow.com/questions/22446008/winapifamily-h-no-such-file-or-directory-when-compiling-sdl-in-codeblocks

https://hg.libsdl.org/SDL/raw-file/e217ed463f25/include/SDL_platform.h

运行提示缺少SDL2.dll

把之前那个SDL2.dll放到测试程序目录下

D:\Program Files\CBProjects\SDL2Test\bin\Debug

运行成功

下一步考虑怎么搞个跨平台工程

05-11 08:06