主机环境:Windows XP 32bit
CodeBlocks版本:13.12
SDL版本:2.0.3
最近想学习一下视频的有关知识,一开始就说到了SDL,因此就来查看一哈SDL学习学习,其中也遇到了一些问题,在此记录一下,以备不时只需。
到官网下载SDL.2.0.3代码,地址:http://www.libsdl.org/download-2.0.php
只需开发库就可以了,由于我的开发环境是CodeBlocks所以下载的是SDL2-devel-2.0.3-mingw.tar.gz,解压到本地,如下
i686-w64-mingw32是32bit的,x86_64-w64-mingw32是64bit的,因此只需要前者就可以了。
打开CodeBlocks新建一个空白工程,
在选项中设置头文件和库文件的路径,在Search directories中,点击Add按钮,如下
以及
在连接器设置即Linker Settings中增加链接选项,如下
最后点击OK键确认。
右击工程-》属性-》Build Targets中设置目标类型为控制台应用,如下:
最后将i686-w64-mingw32\bin目录下的SDL2.dll文件拷贝到C:\WINDOWS\system32目录下,至此环境设置完毕,开写代码
#include <SDL2/SDL.h> /*All SDL App's need this*/ #include <stdio.h> int main() { printf("Initializing SDL."); /* Initialize defaults, Video and Audio */ if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) { printf("Could not initialize SDL: %s.", SDL_GetError()); exit(-1); } printf("SDL initialized."); printf("Quiting SDL."); /* Shutdown all subsystems */ SDL_Quit(); printf("Quiting...."); exit(0); }
网上查看了一哈说winapifamily.h是在win8中才有的头文件,而我的主机环境是windows XP,显然没得这个头文件,在SDL_platform.h中有如下解释
/* Try to find out if we're compiling for WinRT or non-WinRT */ /* If _USING_V110_SDK71_ is defined it means we are using the v110_xp or v120_xp toolset. */
修改代码在主程序中输出__MINGW32__、_MSC_VER、_USING_V110_SDK71这三个宏定义的值,
printf("__MINGW32__:%d\n",__MINGW32__); printf("_MSC_VER:%d\n",_MSC_VER); printf("_USING_V110_SDK71_:%d\n",_USING_V110_SDK71_);
||=== Build: Debug in test (compiler: GNU GCC Compiler) ===| F:\E\SDL2-devel-2.0.3-mingw\test\test\test.c||In function 'main':| F:\E\SDL2-devel-2.0.3-mingw\test\test\test.c|13|warning: implicit declaration of function 'print' [-Wimplicit-function-declaration]| F:\E\SDL2-devel-2.0.3-mingw\test\test\test.c|14|error: '_MSC_VER' undeclared (first use in this function)| F:\E\SDL2-devel-2.0.3-mingw\test\test\test.c|14|note: each undeclared identifier is reported only once for each function it appears in| F:\E\SDL2-devel-2.0.3-mingw\test\test\test.c|15|error: '_USING_V110_SDK71_' undeclared (first use in this function)| F:\E\SDL2-devel-2.0.3-mingw\test\test\test.c|20|warning: implicit declaration of function 'exit' [-Wimplicit-function-declaration]| F:\E\SDL2-devel-2.0.3-mingw\test\test\test.c|20|warning: incompatible implicit declaration of built-in function 'exit' [enabled by default]| ||=== Build failed: 2 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
再次编译代码,发现没有之前的错误, 转而提示error: number of arguments doesn't match prototype
这是因为在SDL中重定义了main函数,在SDL_main.h中,
#if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE) #define main SDL_main #endif /** * The prototype for the application's main() function */ extern C_LINKAGE int SDL_main(int argc, char *argv[]);
int main(int argc, char *argv[])
运行的结果如下图所示:
之后我们来一个有图形的代码,
/*This source code copyrighted by Lazy Foo' Productions (2004-2013) and may not be redistributed without written permission.*/ //Using SDL and standard IO #include <SDL2/SDL.h> #include <stdio.h> //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; int main( int argc, char* args[] ) { //The window we'll be rendering to SDL_Window* window = NULL; //The surface contained by the window SDL_Surface* screenSurface = NULL; //Initialize SDL if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() ); } else { //Create window window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); if( window == NULL ) { printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); } else { //Get window surface screenSurface = SDL_GetWindowSurface( window ); //Fill the surface white SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) ); //Update the surface SDL_UpdateWindowSurface( window ); //Wait two seconds SDL_Delay( 2000 ); } } //Destroy window SDL_DestroyWindow( window ); //Quit SDL subsystems SDL_Quit(); return 0; }
修改RGB颜色代码
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0x00, 0x00 ) );
至此,SDL2安装就告一段落了。
参考链接:http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/codeblocks/index.php