Let’s develop cross-platform games with Rust~

I. Introduction

  Ever since I was immersed in the magic of Warcraft III MODs as a child, I have always had a special affection for game scripting languages. Looking back at that time, the JASS language developed by Blizzard was used to develop the game levels of Warcraft 3. Although JASS is extremely crude from today's perspective, with its main features being static typing + no GC function, it was in an era when industry standards had not yet been formed. , represents a bold attempt at game development language.

Why use scripting languages ​​to develop games?

  游戏脚本语言的引入主要是为了提高开发测试的便捷性。如果直接使用 C++ 这样的底层语言,每更改一行代码,都可能需要耗费大量时间等待复杂工具链的编译与打包。而通过使用脚本语言,可以对实现游戏玩法的程序进行热加载执行,显著提升游戏的开发效率。

  Over time, dynamically typed scripting languages ​​like Lua and JavaScript have become commonplace in game development. However, with the development of programming languages, we have the opportunity to redefine a new standard for game scripting languages ​​- both retro and innovative. This is the combination of Rust + WASM.

2. Rust + WASM + Dora SSR: Redefining game script development

  By combining Rust and WASM, we can hot-update and test games directly on Android or iOS devices, for example, without sacrificing performance, without relying on traditional application development tool chains. In addition, with the help of the Web IDE interface of the Dora SSR open source game engine, game code written in Rust can be compiled once and tested and run on a variety of game devices.

Why choose Rust?

  Rust provides unparalleled memory safety guarantees without the need for garbage collector (GC) intervention, which makes it ideal for game development, especially in performance-sensitive scenarios. Combined with WASM, Rust can not only provide high-performance execution efficiency, but also maintain cross-platform consistency and security.

Quick start guide

  Before starting development, we need to install the Dora SSR game engine. The engine supports multiple platforms, including Windows, Linux, macOS, iOS, and Android. For specific installation steps and requirements, please see the official quick start guide: Dora SSR Quick Start .

Dora SSR v1.3.17 version running on macOS

Step 1: Create a new project

  After the Dora SSR engine binary program is started, open the Dora SSR Web IDE in the browser, right-click the game resource tree on the left, select "New" and create a new folder named "Hello".

Access Dora SSR's Web IDE in the browser and create a new folder

Step 2: Write game code

  Then create a new Rust project on the command line:

rustup target add wasm32-wasi
cargo new hello-dora --name init
cd hello-dora
cargo add dora_ssr

src/main.rsWrite code in   :

use dora_ssr::*;

fn main () {
  let mut sprite = match Sprite::with_file("Image/logo.png") {
    Some(sprite) => sprite,
    None => return,
  };
  let mut sprite_clone = sprite.clone();
  sprite.schedule(once(move |mut co| async move {
    for i in (1..=3).rev() {
      p!("{}", i);
      sleep!(co, 1.0);
    }
    p!("Hello World");
    sprite_clone.perform_def(ActionDef::sequence(&vec![
      ActionDef::scale(0.1, 1.0, 0.5, EaseType::Linear),
      ActionDef::scale(0.5, 0.5, 1.0, EaseType::OutBack),
    ]));
  }));
}

  Build and generate WASM files:

cargo build --release --target wasm32-wasi

Step 3: Upload and run the game

  In Dora SSR Web IDE, right-click the newly created folder "Hello", select "Upload" and upload the compiled WASM file init.wasm.

Uploading files through Web IDE may be more convenient than using auxiliary scripts

  Or use the auxiliary script upload.py to upload the WASM file in the Rust project folder. The command is as follows. The IP parameter is the Web IDE address displayed after Dora SSR is started. The latter parameter is the relative path of the directory to be uploaded:

python3 upload.py "192.168.3.1" "Hello"

Use scripts to complete one-click compilation, upload and start running

Step 4: Publish the game

  In the game resource tree on the left side of the editor, right-click the newly created project folder and select "Download".

  Wait for the browser to pop up a download prompt for the packaged project file.

3. How to achieve

  The process of implementing Rust language development support and WASM runtime embedding in Dora SSR is a new technical exploration and attempt, which mainly includes three key steps:

1. Design of Interface Definition Language (IDL)

  To embed the WASM runtime and support the Rust language on a game engine written in C++, you first need to design an interface definition language (IDL) to facilitate communication and data exchange between different programming languages. The design of IDL is based on the C++ interface, and tags adapted to the Rust language features are added, such as object, readonly, optionaletc., to perform cross-language interface mapping. The following is an interface example of a WASM IDL designed by Dora SSR:

object class EntityGroup @ Group
{
	readonly common int count;
	optional readonly common Entity* first;
	optional Entity* find(function<bool(Entity* e)> func) const;
	static EntityGroup* create(VecStr components);
};

  Considering the differences between the object-oriented features of C++ and the design philosophy of Rust, we partially simulated the object-oriented behavior in C++ in Rust, which required writing some additional mechanisms in Rust to correspond to classes and methods in C++. Although this approach adds some development work, it keeps the interface clean and the system maintainable.

2. Program to generate glue code

  The second step is to write a program to generate glue code for calling each other between C++, WASM and Rust through IDL. In order to achieve this, we chose to use the Yuescript language created by the Dora SSR project. Yuescript is a dynamic programming language based on Lua. It combines the lpeg syntax parsing library in the Lua language ecosystem to handle IDL parsing and glue code generation. The advantage of using Yuescript is that it inherits the flexibility and lightweight of Lua, while providing richer syntax and functions, suitable for handling complex code generation tasks. The following is a code excerpt from an IDL parser written using PEG grammar.

Param = P {
	"Param"
	Param: V"Func" * White * Name / mark"callback" + Type * White * Name / mark"variable"
	Func: Ct P"function<" * White * Type * White * Ct P"(" * White * (V"Param" * (White * P"," * White * V"Param")^0 * White)^-1 * P")" * White * P">"
}

Method = Docs * Ct(White * MethodLabel) * White * Type * White * (C(P"operator==") + Name) * White * (P"@" * White * Name + Cc false) * White * Ct(P"(" * White * (Param * (White * P"," * White * Param)^0 * White)^-1 * P")") * White * C(P"const")^-1 * White * P";" / mark"method"

3. Embed WASM runtime and code integration

  The final step is to embed the WASM runtime and the generated C++ glue code into the game engine to complete the code integration. For the WASM runtime, we chose to use WASM3, a high-performance, lightweight WebAssembly interpreter that supports multiple CPU architectures, simplifies the complexity of the compilation chain, and improves cross-platform compatibility. In this way, Dora SSR can support running games developed in Rust on hardware devices of various architectures, greatly improving the accessibility and flexibility of game projects.

  During the integration process, we released a crate package for Rust developers, which contains all necessary interfaces and tools so that developers can easily develop and re-publish other game modules written in the Rust language based on the Dora SSR game engine in the future.

4. Conclusion

  Choosing Dora SSR + Rust as a game development tool is not only a pursuit of the cutting edge of technology, but also a new exploration of the game development process. We sincerely invite everyone who loves game development to join our community and explore this exciting technological journey together. Welcome to visit our GitHub repository and project homepage to learn more and participate in our development. Let’s create a new era of game development together!

  Our Q group is here, welcome to play: 512620381

Fellow chicken "open sourced" deepin-IDE and finally achieved bootstrapping! Good guy, Tencent has really turned Switch into a "thinking learning machine" Tencent Cloud's April 8 failure review and situation explanation RustDesk remote desktop startup reconstruction Web client WeChat's open source terminal database based on SQLite WCDB ushered in a major upgrade TIOBE April list: PHP fell to an all-time low, Fabrice Bellard, the father of FFmpeg, released the audio compression tool TSAC , Google released a large code model, CodeGemma , is it going to kill you? It’s so good that it’s open source - open source picture & poster editor tool
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4925410/blog/11052782