Win7 64位 MinGW环境测试SDL2.0.3

下载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也要加这句

 

 1 #include <iostream>
 2 #include <SDL2/SDL.h>
 3 
 4 #define WIDTH 640
 5 #define HEIGHT 480
 6 #define BPP 4
 7 #define DEPTH 32
 8 
 9 using namespace std;
10 
11 #undef main
12 
13 int main(int argc, char* argv[])
14 {
15     SDL_Surface *screen;
16     SDL_Event event;
17 
18     int keypress = 0;
19     int h = 0;
20 
21     if (SDL_Init(SDL_INIT_EVERYTHING) == -1 ) {
22         cout << "error: " << SDL_GetError() << endl;
23         return 1;
24     }
25 
26     SDL_Window* window = SDL_CreateWindow("hello", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
27     if (window == NULL){
28         cout << "Error: " << SDL_GetError() << endl;
29         return 1;
30     }
31     SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
32     if (renderer == NULL){
33         cout << "Error: " << SDL_GetError() << endl;
34         return 1;
35     }
36 
37     SDL_Surface* surface = SDL_LoadBMP("bk.bmp");
38     SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
39     SDL_FreeSurface(surface);
40 
41     SDL_RenderClear(renderer);                  //清除屏幕
42     SDL_RenderCopy(renderer, texture, 0, 0);    //把texture画到renderer上
43     SDL_RenderPresent(renderer);
44 
45     while(!keypress)
46     {
47         //DrawScreen(screen, h++);
48         while(SDL_PollEvent(&event))
49         {
50             switch (event.type)
51             {
52                 case SDL_QUIT:
53                     keypress = 1;
54                     break;
55                 case SDL_KEYDOWN:
56                     keypress = 1;
57                     break;
58             }
59         }
60     }
61 
62     SDL_Quit();
63     return 0;
64 }

编译可能出现

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

 

运行成功

 

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

你可能感兴趣的:(Win7 64位 MinGW环境测试SDL2.0.3)