您的位置:首页 > 其它

在Code::Blocks中配置SDL2项目

2015-02-17 00:00 295 查看
1) 首先你需要下载SDL,你可以进入SDL官网下载
http://www.libsdl.org/download-2.0.php。下载白色箭头指示处的开发包。(最新版本为2.0.3)。



打开压缩包后,应该会包含一个名为SDL2-2.xx.xx的文件夹。里面会有一些文件与文件夹,最重要的文件夹是包含32位库的i686-w64-mingw32与包含64位库的x86_64-w64-mingw32

重要提示:大多数编译器为了保证最大兼容性,默认编译32位程序。在教程中将使用32位库文件,当然不必担心你的操作系统是64位,因为我们使用32位库编译32位的程序。

i686-w64-mingw32中包含我们编译和运行SDL程序需要include, lib, bin文件夹,把这个文件夹随便拷贝某个位置。我建议创建一个文件夹,把你所有的MinGW开发库以及SDL开发库放到这个文件夹中,在这里我创建了C:\mingw_dev_lib文件夹并放入其中。

2) 启动Code::Blocks并创建一个空项目(empty project)。



3)打开项目属性(Project->Properties)



4)现在我们要Code::Blocks知道,如何在已解压的文件夹中查找头文件,让我们打开构建选项(build options)。



我们需要在搜索目录(Search Directories)中添加一个新的编译器目录(compiler directory),点击添加(Add),从已解压文件夹选择inclue文件夹中的SDL2文件夹,接着在弹出对话框点击No,意思是不使用相对目录(relative path),现在Code::Blocks将知道从哪里去寻找SDL 2的头文件。



如果你编译时出现错误,提示未找到SDL.h头文件,这意味着你需要重新设置这个步骤。

5) 接下来我们要让Code::Blocks如何查找到SDL文件夹中的库文件。你只需要进入linker标签,添加已解压文件夹中lib文件夹的位置就可以了。



如果编译时提示the linker complains it can't find -lSDL2 or -lSDL2main,这意味着你需要重新设置这个步骤。

6) 为了编译SDL 2代码,我们还需要设置编译器的链接参数,把下面的参数复制粘贴到链接设置(Linker Settings)

-lmingw32 -lSDL2main -lSDL2
然后点击OK确认。



7) 返回项目设置,在构建目标(Build Targets)标签页中选择构建类型(build type)。



如果你不需要控制台输出可以选择GUI程序(GUI Application)。

8)当运行SDL 2程序时,操作系统需要找到dll文件。

在已解压文件夹中bin文件夹找到SDL2.dll文件,并拷贝到你的执行文件同一目录,或者拷贝到系统目录, 32位系统的系统目录为C:\WINDOWS\SYSTEM32,64位系统的系统目录为C:\Windows\SysWOW64。在这个教程中我们创建的是32位程序。

9)将下面代码复制并创建源码文件添加到你的工程中运行测试,如果工程配置成功的话,运行该代码将显示一个背景为白色的windows窗口。

/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/

//Using SDL and standard IO
#include <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;
}

出现的问题:在根据本教程创建好工程后,在编译时出现fatal error: winapifamily.h: No such file or directory

的错误,意思是\include\SDL2\SDL_platform.h头文件中包含了winapifamily.h,但系统无法找到该头文件。这个问题属于SDL 2.0.3的BUG,将在下个版本中修复。解决的方法是用一下代码覆盖SDL_platform.h头文件就可以了,或者也可以下载现成的修复后的SDL_platform.h,
点击下载
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

/**
*  \file SDL_platform.h
*
*  Try to get a standard set of platform defines.
*/

#ifndef _SDL_platform_h
#define _SDL_platform_h

#if defined(_AIX)
#undef __AIX__
#define __AIX__     1
#endif
#if defined(__HAIKU__)
#undef __HAIKU__
#define __HAIKU__   1
#endif
#if defined(bsdi) || defined(__bsdi) || defined(__bsdi__)
#undef __BSDI__
#define __BSDI__    1
#endif
#if defined(_arch_dreamcast)
#undef __DREAMCAST__
#define __DREAMCAST__   1
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
#undef __FREEBSD__
#define __FREEBSD__ 1
#endif
#if defined(hpux) || defined(__hpux) || defined(__hpux__)
#undef __HPUX__
#define __HPUX__    1
#endif
#if defined(sgi) || defined(__sgi) || defined(__sgi__) || defined(_SGI_SOURCE)
#undef __IRIX__
#define __IRIX__    1
#endif
#if defined(linux) || defined(__linux) || defined(__linux__)
#undef __LINUX__
#define __LINUX__   1
#endif
#if defined(ANDROID) || defined(__ANDROID__)
#undef __ANDROID__
#undef __LINUX__ /* do we need to do this? */
#define __ANDROID__ 1
#endif

#if defined(__APPLE__)
/* lets us know what version of Mac OS X we're compiling on */
#include "AvailabilityMacros.h"
#include "TargetConditionals.h"
#if TARGET_OS_IPHONE
/* if compiling for iPhone */
#undef __IPHONEOS__
#define __IPHONEOS__ 1
#undef __MACOSX__
#else
/* if not compiling for iPhone */
#undef __MACOSX__
#define __MACOSX__  1
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
# error SDL for Mac OS X only supports deploying on 10.5 and above.
#endif /* MAC_OS_X_VERSION_MIN_REQUIRED < 1050 */
#endif /* TARGET_OS_IPHONE */
#endif /* defined(__APPLE__) */

#if defined(__NetBSD__)
#undef __NETBSD__
#define __NETBSD__  1
#endif
#if defined(__OpenBSD__)
#undef __OPENBSD__
#define __OPENBSD__ 1
#endif
#if defined(__OS2__)
#undef __OS2__
#define __OS2__     1
#endif
#if defined(osf) || defined(__osf) || defined(__osf__) || defined(_OSF_SOURCE)
#undef __OSF__
#define __OSF__     1
#endif
#if defined(__QNXNTO__)
#undef __QNXNTO__
#define __QNXNTO__  1
#endif
#if defined(riscos) || defined(__riscos) || defined(__riscos__)
#undef __RISCOS__
#define __RISCOS__  1
#endif
#if defined(__SVR4)
#undef __SOLARIS__
#define __SOLARIS__ 1
#endif

#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
/* 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. */
#if (defined(_MSC_VER) && (_MSC_VER >= 1700) && !_USING_V110_SDK71_)	/* _MSC_VER==1700 for MSVC 2012 */
#include <winapifamily.h>
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
#undef __WINDOWS__
#define __WINDOWS__   1
/* See if we're compiling for WinRT: */
#elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
#undef __WINRT__
#define __WINRT__ 1
#endif
#else
#undef __WINDOWS__
#define __WINDOWS__   1
#endif /* _MSC_VER < 1700 */
#endif /* defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) */

#if defined(__WINDOWS__)
#undef __WIN32__
#define __WIN32__ 1
#endif
#if defined(__PSP__)
#undef __PSP__
#define __PSP__ 1
#endif

#include "begin_code.h"
/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
#endif

/**
*  \brief Gets the name of the platform.
*/
extern DECLSPEC const char * SDLCALL SDL_GetPlatform (void);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif
#include "close_code.h"

#endif /* _SDL_platform_h */

/* vi: set ts=4 sw=4 expandtab: */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SDL Code::Blocks