Development/zh: Difference between revisions

From DDraceNetwork
(Created page with "示例:")
(Created page with "=== 枚举和常量 ===")
Line 161: Line 161:




<div lang="en" dir="ltr" class="mw-content-ltr">
<span id="Enums_and_constants"></span>
=== Enums and constants ===
=== 枚举和常量 ===
</div>


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

Revision as of 04:03, 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等等。


网络

网络协议几乎由python脚本生成并输出c++代码,例如说,datasrc/network.py定义了所有网络包。


代码规范

代码规范的讨论还在持续:ddnet#2945

目前已经应用了下面的部分:


缩进格式

采用Allman 风格

此风格将控制语句关联的大括号放在下一行并且缩进到相同位置,内部语句则缩进到下一级。

while(x == y)
{
    Something();
    SomethingElse();
}

FinalThing();

类和结构体

必须带有前缀C(由于历史遗留原因有一些结构体没有遵循,例如图形代码部分)或者代表接口的I。新的结构体需要带前缀S

示例:

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


枚举和常量

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