Presentation is loading. Please wait.

Presentation is loading. Please wait.

SDL – 1 簡介 靜宜大學資訊工程系 蔡奇偉副教授 © 2007-2009.

Similar presentations


Presentation on theme: "SDL – 1 簡介 靜宜大學資訊工程系 蔡奇偉副教授 © 2007-2009."— Presentation transcript:

1 SDL – 1 簡介 靜宜大學資訊工程系 蔡奇偉副教授 ©

2 綱要 簡介 相關函式庫與標頭檔 SDL 的命名慣例 SDL 子系統 初始化 SDL 結束 SDL 取得錯誤訊息 SDL 定義的資料型態

3 簡介 SDL 是 Simple DirectMedia Layer 的縮寫
Cross-platform (Windows, Linux, BSD, MacOS, …,etc) 官網:

4 相關函式庫與標頭檔 函式庫 標頭檔 說明 sdl.lib SDL.h SDL 核心函式庫 SDLmain.lib 無
Windows 介面函式庫 SDL_image.lib SDL_image.h 載入 jpg, png, tiff 等圖檔 SDL_mixer.lib SDL_mixer.h 混音函式庫 SDL_net.lib SDL_net.h 網路連線函式庫 SDL_ttf.lib SDL_ttf.h TrueType 字型函式庫

5 SDL 的命名慣例 所有常數都用大寫字母,以 SDL 開頭,並用底線字元將其中的組合字分開,譬如: SDL_INIT_VIDEO
所有函式的名稱都以 SDL_ 開頭,其後的組合字用首字元大寫的方式來區隔,譬如: SDL_GetVideoSurface() SDL 自定結構型態的命名與函式相同,譬如: SDL_Rect 或 SDL_Surface

6 SDL 子系統 名稱 常數名稱 自動初始化 1 Video SDL_INIT_VIDEO 否 2 Audio SDL_INIT_AUDIO
3 Event Handling 4 CDROM SDL_INIT_CDROM 5 Joystick Handling SDL_INIT_JOYSTICK 6 File I/O 7 Timer SDL_INIT_TIMER 8 Thread

7 初始化 SDL int SDL_Init (Uint32 flags);
我們必須先呼叫 SDL_Init() 來初始化 SDL 之後,才能呼叫其他的 SDL 函式。 參數 flags 用來指定想要初始化的子系統。我們可以代入前頁列出的子系統常數名稱或它們的 bit-OR 組合,譬如: SDL_Init(SDL_INIT_VIDEO); SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); 如果初始化成功,函式傳回 0,否則傳回 -1。

8 結束 SDL void SDL_Quit (void); 你應該在結束程式之前呼叫此函式來關閉 SDL 所有的子系統並釋放它們所佔用的資源。

9 初始化 / 結束 SDL 子系統 int SDL_InitSubSystem (Uint32 flags);
void SDL_QuitSubSystem (Uint32 flags); 參數 flags 的指定方式如同 SDL_Init() 一般。 SDL_InitSubSystem() 函式可用來初始化沒被 SDL_Init() 初始化的子系統。

10 Examples /* Seperating Joystick and Video initialization. */
SDL_Init(SDL_INIT_VIDEO); . SDL_SetVideoMode(640,480,16,SDL_DOUBLEBUF|SDL_FULLSCREEN); /* Do Some Video stuff */ /* Initialize the joystick subsystem */ SDL_InitSubSystem(SDL_INIT_JOYSTICK); /* Do some stuff with video and joystick */ /* Shut them both down */ SDL_Quit();

11 取得錯誤訊息 char *SDL_GetError (void);
此函式傳回 SDL 內部最近發生錯誤的文字訊息。我們可以在 SDL 函式傳回錯誤代碼後,呼叫此函式取得錯誤說明文字。

12 Example #include <SDL/SDL.h> /* All SDL App's need this */
#include <stdio.h> int main() { printf("Initializing SDL.\n"); /* Initialize defaults, Video and Audio */ if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) { printf("Could not initialize SDL: %s.\n", SDL_GetError()); exit(-1); } printf("SDL initialized.\n"); printf("Quiting SDL.\n"); /* Shutdown all subsystems */ SDL_Quit(); printf("Quiting....\n"); exit(0);

13 SDL 定義的資料型態 整數型態 Uint8 8 bits 正整數: 0 ~ 255 Sint8 8 bits 整數: -128 ~ 127

14 SDL_Rect(矩形) typedef struct{ Sint16 x, y; // 左上角座標
Uint16 w, h; // 寬度與高度,以像素為單位 } SDL_Rect; (x, y) w h

15 SDL_Color(顏色) typedef struct{ Uint8 r; // 紅色值 Uint8 g; // 綠色值
Uint8 b; // 藍色值 Uint8 unused; } SDL_Color; SDL 的顏色值通用格式,不因像素格式不同而異。然而, SDL 並無直接處理 SDL_Color 的相關函式。


Download ppt "SDL – 1 簡介 靜宜大學資訊工程系 蔡奇偉副教授 © 2007-2009."

Similar presentations


Ads by Google