在Code::Blocks中配置SDL2項目

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-mingw32php

重要提示:大多數編譯器爲了保證最大兼容性,默認編譯32位程序。在教程中將使用32位庫文件,固然沒必要擔憂你的操做系統是64位,由於咱們使用32位庫編譯32位的程序。 linux

i686-w64-mingw32中包含咱們編譯和運行SDL程序須要include, lib, bin文件夾,把這個文件夾隨便拷貝某個位置。我建議建立一個文件夾,把你全部的MinGW開發庫以及SDL開發庫放到這個文件夾中,在這裏我建立了C:\mingw_dev_lib文件夾並放入其中。 redis

2) 啓動Code::Blocks並建立一個空項目(empty project)。 express

3)打開項目屬性(Project->Properties) windows

4)如今咱們要Code::Blocks知道,如何在已解壓的文件夾中查找頭文件,讓咱們打開構建選項(build options)。 api

咱們須要在搜索目錄(Search Directories)中添加一個新的編譯器目錄(compiler directory),點擊添加(Add),從已解壓文件夾選擇inclue文件夾中的SDL2文件夾,接着在彈出對話框點擊No,意思是不使用相對目錄(relative path),如今Code::Blocks將知道從哪裏去尋找SDL 2的頭文件。 app

若是你編譯時出現錯誤,提示未找到SDL.h頭文件,這意味着你須要從新設置這個步驟。 ide

5) 接下來咱們要讓Code::Blocks如何查找到SDL文件夾中的庫文件。你只須要進入linker標籤,添加已解壓文件夾中lib文件夾的位置就能夠了。 測試

若是編譯時提示the linker complains it can't find -lSDL2 or -lSDL2main,這意味着你須要從新設置這個步驟。 ui

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: */
相關文章
相關標籤/搜索