Development/zh: Difference between revisions

From DDraceNetwork
(Created page with "客户端由许多组件构成,都是继承<code>CComponent</code>的类:这些组件通过实现视觉方法来提供功能,例如<code>OnInit</code>,<code>OnMessage</code>等等。")
(Created page with "==== 网络 ====")
Line 113: Line 113:




<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Networking"></span>
==== Networking ====
==== 网络 ====
</div>


<div lang="en" dir="ltr" class="mw-content-ltr">
<div lang="en" dir="ltr" class="mw-content-ltr">

Revision as of 03:26, 30 July 2023

本文皆在教您如何對 DDNet 開發 ,因為這是一款開源遊戲(源代碼公開),它依賴於一些善良的人們在空餘時間的貢獻。


開發環境

截至目前,基於以下原因,我們強烈推薦使用Linux系統進行DDNet開發:

  • 大多數DDNet貢獻者使用Linux,使用Linux與之前的開發者交流起來更加方便。
  • 軟件包管理更簡單,您可以輕鬆地安裝所有需要的庫。
  • 本文專注於Linux,還沒有涉及Windows。

首先,DDNet由 C++ 語言編寫,開發者需要對它相當熟悉,但您也可以在只有基礎知識的情況下慢慢學習深入。

學習C++的一些有用資源:

DDNet的源代碼使用Git託管,這是一個版本控制系統,也是與多人協作開發的重要工具。

如果您的Linux發行版中還沒有git,請安裝它,例如在大多數基於debian/ubuntu的發行版中,您可以打開終端,輸入:sudo-apt-install git


獲取源代碼

源代碼位於Github,您沒有帳戶的情況下可以通過Clone獲得源代碼,但如果想更改官方源代碼,則需要登錄賬號。

如果您不熟悉git/github,可以在這裡學習基礎知識: Hello World - Github


安裝依賴項

如果您在Linux上,您可以通過閱讀DDNet github頁面上的自述文件來安裝所有需要的依賴項:https://github.com/ddnet/ddnet#dependencies-在linux上--macos

對於Arch Linux:

控制台輸入 sudo pacman -S --needed base-devel cmake curl ffmpeg freetype2 git glew glslang gmock libnotify libpng opusfile python rust sdl2 spirv-tools sqlite vulkan-headers vulkan-icd-loader wavpack x264

對於Debian:

控制台輸入 sudo apt install build-essential cargo cmake git glslang-tools google-mock libavcodec-extra libavdevice-dev libavfilter-dev libavformat-dev libavutil-dev libcurl4-openssl-dev libfreetype6-dev libglew-dev libnotify-dev libogg-dev libopus-dev libopusfile-dev libpng-dev libsdl2-dev libsqlite3-dev libssl-dev libvulkan-dev libwavpack-dev libx264-dev python rustc spirv-tools4

編譯DDNet

我們使用CMake來控制編譯進程,如果你安裝完成所有的依賴項,之後只需要簡單輸入這些命令(確保你在DDNet文件夾下):

mkdir build
cd build
cmake ..
make -j$(nproc)


基本信息

以下是一些基本信息:

  • 目前,源代碼是使用C++17標準編譯的,但您會看到代碼的許多部分更像C,因為只有大多數新代碼使用C++17。
  • std::string很少使用,字符數組加system.h方法才是常態。
  • 大多數輸入輸出代碼、格式化和Print都是使用system.h提供的方法完成的。


源代碼布局

現在你可以構建DDNet並開始編輯它了。


src/base 目錄

由於DDNet是一個跨平台遊戲,需要一個抽象層來簡化開發,因此該目錄包含許多有用的函數來處理這一問題。


src/engine 目錄

遊戲引擎所在位置,它處理大多數與玩法無關的東西,比如圖形、聲音、網絡等。


src/game 目錄

所有玩法的代碼所在位置,分為客戶端和服務器。


服務器端

這個遊戲使用自己的面向對象的實體分級系統,所有其他實體源於主類CEntity,位於src/game/server/e ntity.h目錄。

遊戲世界在src/game/server/gameworld.h路徑下,管理這些實體。

一些重要實體有:

  • CCharacter: 代表一個活着的Tee , 當一個Tee生成時會被實例化,當它死亡時會被刪除。有關玩家在死亡之間的信息,請參閱CPlayer


客戶端

客戶端由許多組件構成,都是繼承CComponent的類:這些組件通過實現視覺方法來提供功能,例如OnInitOnMessage等等。


網絡

The network protocol is mostly generated by python scripts that output C++ code, for example, datasrc/network.py defines all network packets.


Code conventions

The ongoing discussion on code conventions is located here: ddnet#2945

Currently, the following applies:


Indentation style

Allman style is used.

This style puts the brace associated with a control statement on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level.

while(x == y)
{
    Something();
    SomethingElse();
}
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
FinalThing();

Classes and Structs

Must be prefixed by C (for legacy reasons this is ignored for structs in some places, such as in graphics code) or I for interfaces. New structs should be prefixed by S.

Example:

class CCharacter : public CEntity
{
    // ...
}


Enums and constants

Should be screaming snake case, for example: MAX_PLAYERS

enum
{
	FAKETUNE_FREEZE = 1,
	FAKETUNE_SOLO = 2,
	FAKETUNE_NOJUMP = 4,
	FAKETUNE_NOCOLL = 8,
	FAKETUNE_NOHOOK = 16,
	FAKETUNE_JETPACK = 32,
	FAKETUNE_NOHAMMER = 64,
};


Variable naming

  • The names of variables contain 3 parts: qualifier, prefix and name.
  • Variable names should start with uppercase unless they are 1 char long without any prefix or qualifier, for example: i, x, y.
  • Variables can have more than 1 qualifier (or zero) and more than 1 prefix (or zero).

These are laid out like this: [qualifiers]_[prefixes][Name]


Qualifiers

  • m for member variables: m_MyVariable.
  • s for static variables: s_MyStaticVariable, ms_MyStaticMemberVariable.
  • g for global variables with external linkage: gs_MyGlobalStaticVar.


Prefixes

  • p for pointers: pMyPointer, m_pCharacter, ppMyPointerToPointer.
  • a for arrays: aMyArray, aBuf.
  • v for std::vectors: vMyVector, vpvMyVectorOfPointersToVectors.
  • fn for functions: pfnMyCallback, m_papfnMyPointerToArrayOfCallbacks.


Common snippets

Here is a list of code that you may frequently see across the codebase:


Formatting text

char aBuf[128];
str_format(aBuf, sizeof(aBuf), "number: %d", 2);

See printf documentation for a list of all format specifiers.

Iterating over all players

for(int i = 0; i < MAX_CLIENTS; i++)
{
    // Server-side
    CPlayer *pPlayer = GameServer()->m_apPlayers[i];
}


Printing to the game console

Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "wikiprint", "Hello from the ddnet wiki!");


Debug printing

int i = 2;
dbg_msg("wikiprint", "Hello from the ddnet wiki: %d", i);

Translating text in the client

Localize can be used in the game client to get the translation for a specific string from the language file selected by the user.
DoButton(..., Localize("Connect"), ...);
The string can also contain format specifiers. The translated string must contain the same format specifiers.
char aBuf[128];
str_format(aBuf, sizeof(aBuf), Localize("%d of %d servers"), NumServers, TotalServers);
A script is used to scan the code for calls to Localize and collect the strings to update the translation files. For this reason, the call to Localize must not contain any other code, or the script cannot determine the text correctly.
// Do NOT do this:
const char *pStr = Localize(Team == TEAM_RED ? "Red team" : "Blue team");
</div>

<div lang="en" dir="ltr" class="mw-content-ltr">
// Do this instead:
const char *pStr = Team == TEAM_RED ? Localize("Red team") : Localize("Blue team");

External resources