跳轉到內容

SDL (簡單直接媒體層) - 基礎

25% developed
來自華夏公益教科書

在本節中,我們將向您展示如何初始化和關閉 SDL 子系統。

#include <stdio.h> /* printf and fprintf */

#ifdef _WIN32
#include <SDL/SDL.h> /* Windows-specific SDL2 library */
#else
#include <SDL2/SDL.h> /* macOS- and GNU/Linux-specific */
#endif
 
int main (int argc, char **argv)
{
  /*
  * Initialises the SDL video subsystem (as well as the events subsystem).
  * Returns 0 on success or a negative error code on failure using SDL_GetError().
  */
  if (SDL_Init(SDL_INIT_VIDEO) != 0)
  {
    fprintf(stderr, "SDL failed to initialise: %s\n", SDL_GetError());
    return 1;
  }
 	
  printf("SDL initialised!");

  /* Shuts down all SDL subsystems */
  SDL_Quit(); 
  
  return 0;
}

您可以從此處下載本節的原始碼:GitLab 倉庫。所有原始碼都儲存在 此組 中。

連結 SDL2 庫

[編輯 | 編輯原始碼]

SDL2 所需的基本函式位於 <SDL/SDL.h> 庫或 <SDL2/SDL.h> 庫中,具體取決於作業系統,因此上面的原始碼使用

#ifdef _WIN32
#include <SDL/SDL.h> /* Windows-specific SDL2 library */
#else
#include <SDL2/SDL.h> /* macOS- and GNU/Linux-specific */
#endif

Windows 中的 main 宏

[編輯 | 編輯原始碼]

在為 Windows 機器編寫程式碼時,需要包含

int main (int argc, char **argv)

因為 SDL 會覆蓋 main 宏。根據 SDL 的 FAQWindows

您應該使用 main() 而不是 WinMain(),即使您正在建立 Windows 應用程式,因為 SDL 提供了 WinMain() 的版本,它會在呼叫您的主程式碼之前執行一些 SDL 初始化。如果出於某種原因您需要使用 WinMain(),請檢視 SDL 原始碼中的 src/main/win32/SDL_main.c,以瞭解您需要在 WinMain() 函式中執行哪種初始化才能使 SDL 正確工作。

初始化 SDL 子系統

[編輯 | 編輯原始碼]

在使用 SDL 子系統時,您必須始終先對其進行初始化。在以下 if 語句中,我們使用標誌 SDL_INIT_VIDEO 初始化 SDL 影片子系統。如果成功,它將返回 0,否則它將返回一個負的錯誤程式碼,並使用 SDL_GetError 透過 stderr 流列印有關錯誤的資訊。

/*
* Initialises the SDL video subsystem (as well as the events subsystem).
* Returns 0 on success or a negative error code on failure using SDL_GetError().
*/
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
  fprintf(stderr, "SDL failed to initialise: %s\n", SDL_GetError());
  return 1;
}

您可以使用 | 運算子初始化多個子系統

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0)
{
  fprintf(stderr, "SDL failed to initialise: %s\n", SDL_GetError());
  return 1;
}

有關 SDL_Init 函式子系統及其標誌的所有資訊,請參閱 SDL Wiki 的 SDL_Init 頁面

關閉 SDL 子系統

[編輯 | 編輯原始碼]

要關閉 SDL 子系統,您必須執行

SDL_Quit();

但是,這隻會關閉主子系統;關閉 TTF 子系統需要使用不同的函式

TTF_Quit();


在各種作業系統上設定

[編輯 | 編輯原始碼]

在 Windows 上入門

[編輯 | 編輯原始碼]

SDL (簡單直接媒體層)/基礎/在 Windows 上入門

在 macOS 上入門

[編輯 | 編輯原始碼]

SDL (簡單直接媒體層)/基礎/在 macOS 上入門

在 Linux 上入門

[編輯 | 編輯原始碼]

從 Linux 發行版的官方儲存庫中安裝 SDL 的開發庫。

$ gcc sdl.c -o sdl -lSDL2

引數 -lSDL2 表示在使用 gcc 編譯 sdl.c 後,使用 -o 引數命名的二進位制檔案 sdl 將連結到 SDL 2 執行時庫。

注意:將二進位制檔案連結到 SDL 1.2 庫需要 -lSDL 引數。

要繼續使用終端構建原始碼,您可以使用 GNU Make、CMake 或普通的 Makefile。

Code::Blocks

[編輯 | 編輯原始碼]


建立視窗

[編輯 | 編輯原始碼]

在本節中,我們將演示如何建立和銷燬基本的 SDL 視窗。以下程式碼將建立一個名為“SDL 示例”的視窗,寬度為 800 畫素,高度為 600 畫素,並在螢幕上顯示 3000 毫秒。

#include <stdio.h> /* printf and fprintf */

#ifdef _WIN32
#include <SDL/SDL.h> /* Windows-specific SDL2 library */
#else
#include <SDL2/SDL.h> /* macOS- and GNU/Linux-specific */
#endif

/* Sets constants */
#define WIDTH 800
#define HEIGHT 600
#define DELAY 3000

int main (int argc, char **argv)
{
  /* Initialises data */
  SDL_Window *window = NULL;
  
  /*
  * Initialises the SDL video subsystem (as well as the events subsystem).
  * Returns 0 on success or a negative error code on failure using SDL_GetError().
  */
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    fprintf(stderr, "SDL failed to initialise: %s\n", SDL_GetError());
    return 1;
  }

  /* Creates a SDL window */
  window = SDL_CreateWindow("SDL Example", /* Title of the SDL window */
			    SDL_WINDOWPOS_UNDEFINED, /* Position x of the window */
			    SDL_WINDOWPOS_UNDEFINED, /* Position y of the window */
			    WIDTH, /* Width of the window in pixels */
			    HEIGHT, /* Height of the window in pixels */
			    0); /* Additional flag(s) */

  /* Checks if window has been created; if not, exits program */
  if (window == NULL) {
    fprintf(stderr, "SDL window failed to initialise: %s\n", SDL_GetError());
    return 1;
  }

  /* Pauses all SDL subsystems for a variable amount of milliseconds */
  SDL_Delay(DELAY);

  /* Frees memory */
  SDL_DestroyWindow(window);
  
  /* Shuts down all SDL subsystems */
  SDL_Quit(); 
  
  return 0;
}

您可以從此處下載本節的原始碼:GitLab 倉庫。所有原始碼都儲存在 此組 中。

設定資料

[編輯 | 編輯原始碼]

為了建立視窗,我們需要指定其高度和寬度。在這種情況下,我們使用宏定義全域性常量。我們將寬度設定為 800 畫素,高度設定為 600 畫素。

#define WIDTH 800
#define HEIGHT 600

在此程式中,我們將不得不暫停 SDL 子系統以使視窗保持開啟狀態。我們將延遲時間設定為 3000 毫秒。

#define DELAY 3000

在 C 中,最好先初始化變數,以便了解它們是在何時以及如何使用的。在這種情況下,我們初始化一個名為 windowSDL_Window 指標。在 SDL 中,幾乎所有物件都以指標的形式初始化。

SDL_Window *window = NULL;

建立視窗

[編輯 | 編輯原始碼]

為了建立視窗,我們需要使用 SDL_CreateWindow 函式並設定一些引數。

window = SDL_CreateWindow("SDL Example", /* Title of the SDL window */
			  SDL_WINDOWPOS_UNDEFINED, /* Position x of the window */
			  SDL_WINDOWPOS_UNDEFINED, /* Position y of the window */
			  WIDTH, /* Width of the window in pixels */
			  HEIGHT, /* Height of the window in pixels */
			  0); /* Additional flag(s) */

您可以在 SDL Wiki 中瞭解有關 SDL_CreateWindow 的更多資訊。

使用 SDL_Delay

[編輯 | 編輯原始碼]

在示例程式碼中,我們使用了 SDL_Delay 函式來暫停 SDL 子系統一段時間(以毫秒為單位),並在視窗開啟時允許視窗顯示在螢幕上。在本例中,暫停時間由 DELAY 宏定義為 3000 毫秒。

SDL_Delay(DELAY);

您可以在 SDL Wiki 中瞭解更多關於 SDL_Delay 的資訊。

銷燬視窗

[編輯 | 編輯原始碼]

為了關閉視窗並釋放記憶體,我們必須銷燬視窗。銷燬 window 比建立它要簡單得多。

SDL_DestroyWindow(window);

您可以在 SDL Wiki 中瞭解更多關於 SDL_DestroyWindow 的資訊。

華夏公益教科書