跳轉到內容

使用gameplay3d進行跨平臺遊戲程式設計/入門

來自Wikibooks,開放世界中的開放書籍

什麼是gameplay3d?

[編輯 | 編輯原始碼]

gameplay3d專案是一個開源的、跨平臺的3D框架,旨在支援想要開發桌面和移動遊戲的獨立遊戲開發者。主要開發語言是C++,但也支援Lua指令碼。

gameplay3d背後的主要動機是允許開發者在儘可能多的平臺上釋出他們的遊戲。它僅在必要時才特定於平臺,這最大限度地提高了跨不同平臺程式碼重用的可能性。

gameplay3d還旨在允許快速迭代和原型設計,這使開發人員能夠快速輕鬆地實現和測試想法。

先決條件

[編輯 | 編輯原始碼]

您的硬體需要支援

  • OpenGL 3.2(桌面);或
  • OpenGL ES 2.0(移動)

要使用gameplay3d,需要牢固掌握C++,並瞭解3D圖形原理。

一些關於OpenGL API和GLSL著色器語言的知識非常有用,儘管開始使用gameplay3d並非嚴格必要。

同樣,如果希望使用gameplay3d的Lua指令碼支援,熟悉Lua也很有幫助(儘管Lua指令碼的使用完全是可選的)。

有關學習C++、3D圖形、OpenGL和Lua的一些有用連結可以在其他有用資源連結中找到。

使用本書

[編輯 | 編輯原始碼]

如果您完全不熟悉gameplay3d,我們建議您先閱讀本書的前4章(即本章、遊戲類概述gameplay3d設計概念建立場景),以瞭解gameplay3d的工作原理。之後,請隨意跳過所需內容!

無論您使用哪個平臺,您都首先需要獲取gameplay3d儲存庫的副本,該儲存庫託管在https://github.com/blackberry/GamePlay

克隆gameplay3d儲存庫最簡單的方法是

  • 點選gameplay3d GitHub頁面上的“下載ZIP”圖示;以及
  • 將檔案解壓縮到您選擇的資料夾中。

或者,如果您已安裝git並希望利用它提供的版本控制功能,則可以

第二步是安裝gameplay3d依賴的外部依賴項。這是透過執行install.bat指令碼完成的。

gameplay3d使用的依賴項如下

  • bullet - 用於物理
  • freetype2 - 用於字型轉換
  • glew - 用於載入OpenGL擴充套件
  • lua - 用於指令碼
  • oggvorbis - 用於壓縮音訊
  • openal - 用於位置音訊
  • png - 用於影像解壓縮
  • tinyxml2 - 用於XML解析
  • zlib - 用於資料解壓縮

第三步是確保您已安裝Visual Studio Express 2013 for Windows Desktop,該軟體免費安裝和使用。

克隆儲存庫、安裝依賴項和安裝VS2013後,您就可以開啟gameplay解決方案(即gameplay.sln)。在您第一次執行任何示例之前,您需要構建gameplay3d庫,這在VS2013中可以透過以下方式完成

  • 構建 > 構建解決方案 (F7)

請參閱網際網路上有關如何在mac上安裝gameplay3d的資訊。讓我們谷歌一下。它將很快在此處新增。

本節演示如何在Ubuntu Linux上安裝gameplay3d。

首先,確保您已安裝GIT。這可以透過Ubuntu軟體中心或從終端鍵入以下內容來完成


sudo apt-get install git


其次,您需要安裝許多其他軟體包。要安裝這些,請從終端鍵入以下內容


sudo apt-get install build-essential gcc cmake libglu1-mesa-dev libogg-dev libopenal-dev gtk-2.0-dev curl libpcrecpp0:i386


這將安裝以下軟體包(在它們尚未安裝的情況下)

  • build-essential - 包含從原始碼編譯/構建軟體所需的各種工具
  • gcc - GNU編譯器集合
  • cmake - CMake,用於管理構建過程
  • libglu1-mesa-dev - OpenGL實用程式庫開發檔案;包括用於使用GLU編譯程式的標頭檔案和靜態庫
  • libogg-dev - Ogg位元流庫開發檔案(用於壓縮音訊)
  • libopenal-dev - OpenAL(音訊)API軟體實現的開發檔案
  • gtk-2.0-dev - GTK+庫的開發檔案
  • curl - cURL,一個使用URL語法傳輸資料的命令列工具
  • libpcrecpp0:i386 - 支援正則表示式的函式的C++庫

第三,如果您尚未這樣做,請使用git將gameplay3d儲存庫克隆到您想要的位置。

第四,從gameplay3d儲存庫的頂層目錄執行以下命令列,該命令將安裝上面#Windows中列出的外部依賴項


./install.sh


此指令碼需要curl。

第五,透過從gameplay3d儲存庫的頂層目錄執行以下命令來構建gameplay3d庫


mkdir build
cd build
cmake ..
make


Blackberry

[編輯 | 編輯原始碼]

嘗試示例

[編輯 | 編輯原始碼]

Gameplay3d 帶有大量示例。它們涵蓋了 Gameplay3d 中的大多數概念和類。我們強烈建議您熟悉所有示例(特別是“samples-browser”示例),並在您自己的專案中借鑑/改編這些示例中的程式碼。

  • 右鍵單擊您要執行的示例
  • 設定為啟動專案
  • 除錯 > 開始除錯 (F5)

為了執行示例(此處使用“sample-browser”示例作為演示),請從 GamePlay 儲存庫的頂層目錄執行以下命令:


cd build/samples/browser
./sample-browser


Blackberry

[編輯 | 編輯原始碼]

建立您的第一個 Gameplay3d 專案

[編輯 | 編輯原始碼]

所有平臺

[編輯 | 編輯原始碼]

要建立一個新的跨平臺遊戲專案,請從儲存庫根目錄執行指令碼 newproject

  • newproject.bat(在 Windows 上)
  • newproject.sh(在 Mac 或 Linux 上)。

以下是執行 newproject.bat 指令碼的示例

  1. Enter a name for the new project.
  
   This name will be given to the project 
   executable and a folder with this name
   will be created to store all project files.
  
  Project name: test
  
  2. Enter a game title.
  
   On some platforms, this title is used to
   identify the game during installation and
   on shortcuts/icons.
  
  Title: Test
  
  3. Enter a short game description.
   
  Description: Test Game
  
  4. Enter a unique identifier for your project.
  
   This should be a human readable package name,
   containing at least two words separated by a
   period (eg. com.surname.gamename).
  
  Unique ID: org.gameplay3d.test
  
  5. Enter author name.
  
   On BlackBerry targets, this is used for
   signing and must match the developer name
   of your development certificate.
  
  Author: My Company
  
  6. Enter your game's main class name.
  
   Your initial game header and source file
   will be given this name and a class with
   this name will be created in these files.
  
  Class name: TestGame
  
  7. Enter the project path.
  
   This can be a relative path, absolute path,
   or empty for the current folder. Note that
   a project folder named test will also
   be created inside this folder.
  
  Path: samples
  
    1 file copied.
    ...

新增並執行新專案

[編輯 | 編輯原始碼]
  • 將 Visual Studio 專案新增到現有的 gameplay.sln 解決方案中;
  • 將“gameplay”專案設定為依賴項(右鍵單擊新專案,單擊“專案依賴項…”,然後選擇“gameplay”專案)
  • 構建並執行。
  • 開啟終端並導航到新專案資料夾中的“build”資料夾。
  • 執行以下命令
cmake ..
make
  • 執行新建立的可執行檔案,該檔案位於[projectname]/build/bin/linux/中。

BlackBerry

[編輯 | 編輯原始碼]
華夏公益教科書