CodeBlocks安装SDL2.0.3

主机环境: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.0.3_第1张图片

只需开发库就可以了,由于我的开发环境是CodeBlocks所以下载的是SDL2-devel-2.0.3-mingw.tar.gz,解压到本地,如下

CodeBlocks安装SDL2.0.3_第2张图片

i686-w64-mingw32是32bit的,x86_64-w64-mingw32是64bit的,因此只需要前者就可以了。

打开CodeBlocks新建一个空白工程,

CodeBlocks安装SDL2.0.3_第3张图片

之后右击工程-》编译属性CodeBlocks安装SDL2.0.3_第4张图片

在选项中设置头文件和库文件的路径,在Search directories中,点击Add按钮,如下

CodeBlocks安装SDL2.0.3_第5张图片

以及

CodeBlocks安装SDL2.0.3_第6张图片

在连接器设置即Linker Settings中增加链接选项,如下

CodeBlocks安装SDL2.0.3_第7张图片

最后点击OK键确认。

右击工程-》属性-》Build Targets中设置目标类型为控制台应用,如下:

CodeBlocks安装SDL2.0.3_第8张图片

最后将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);
}

编译代码,出现fatal error: winapifamily.h: No such file or directory错误,找不到winapifamily.h文件,如图

CodeBlocks安装SDL2.0.3_第9张图片

网上查看了一哈说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. */

尝试查明我们编译是在WinRT还是non-WinRT,度娘了一下,WinRT只会在win8中使用,我们肯定是后者,如果_USING_V110_SDK71_被定义,意味着我们正在使用v110_xp或者v120_xp工具链。

修改代码在主程序中输出__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)) ===|

由于我们使用的编译器是mingw32,因此__MINGW32__是有定义的,并且值为1,但是我们并不需要winapifamily.h头文件,因此偷个懒,在SDL_platform.h中取消__MINGW32__的宏定义,如下

CodeBlocks安装SDL2.0.3_第10张图片

再次编译代码,发现没有之前的错误, 转而提示error: number of arguments doesn't match prototype

CodeBlocks安装SDL2.0.3_第11张图片

这是因为在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[])

再次编译,成功。

CodeBlocks安装SDL2.0.3_第12张图片

运行的结果如下图所示:

CodeBlocks安装SDL2.0.3_第13张图片

之后我们来一个有图形的代码,

/*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;
}

编译运行,如下

CodeBlocks安装SDL2.0.3_第14张图片

修改RGB颜色代码

SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0x00, 0x00 ) );

此时窗口应该是红色的,如下

CodeBlocks安装SDL2.0.3_第15张图片

至此,SDL2安装就告一段落了。

参考链接:http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/codeblocks/index.php

你可能感兴趣的:(CodeBlocks安装SDL2.0.3)