banner
[面包]MrTwoC

[面包]MrTwoC

你好,欢迎来到这个基于区块链的个人博客 名字:面包 / MrTwoc 爱好:跑步(5/10KM)、咖啡、游戏(MMORPG、FPS、Minecraft、Warframe) 兴趣方向:Rust、区块链、网络安全、量子信息(量子计算)、游戏设计与开发
bilibili
steam
email
github

[0x02] Bevy-StartSet

Setup

Since Bevy is written in Rust, the prerequisite for using Bevy is to install Rust. The official website has provided the corresponding installation steps, which can be installed step by step.

How to create a Bevy project#

Create a new Rust executable project

cargo new my_bevy_game
cd my_bevy_game

Then, run the command again, and you will see the output "Hello World!"

cargo run

Add Bevy as a dependency#

The simplest and most direct method: enter the following command in the command line

cargo add bevy

Or manually copy the following content in the file: Cargo.toml of the project

[package]
name = "my_bevy_game"
version = "0.1.0"
edition = "2021" # this needs to be 2021, or you need to set "resolver=2"

[dependencies]
bevy = "0.12" # make sure this is the latest version

If you are using Cargo WorkSpace, you also need to add the resolver to the Cargo.toml file in the root directory:

[workspace]
resolver = "2" # Important! wgpu/Bevy needs this!

Enable performance-optimized compilation settings#

Although this may not be a problem for simple projects, debug builds in Rust can be very slow - especially when you start making real games with Bevy.

Using the default configuration, it takes several minutes to load large 3D models in debug mode, or the frame rate of a simple scene drops to a level where it is almost unplayable, which is not uncommon.

Fortunately, there is a simple fix that allows us to keep our fast iteration compile! Add the following to your Cargo.toml:

# Enable a small amount of optimization in debug mode
[profile.dev]
opt-level = 1

# Enable high optimizations for dependencies (incl. Bevy), but not for our code:
[profile.dev.package."*"]
opt-level = 3

Enable fast compilation#

This is the recommended way by the Bevy official website. If you want to perform the fastest iterative compilation, you can try the following configuration

  1. Enable Bevy's dynamic linking feature: This is the most influential way to shorten the compilation time! If it is a dependency, you can use the "dynamic_linking" feature flag (enable dynamic linking) to compile the binary. Important! On Windows, you must also enable performance optimizations, otherwise you will encounter [exported symbol too many](https://github.com/bevyengine/bevy/issues/1110#issuecomment-1312926923) error.
cargo run --features bevy/dynamic_linking

If you don't want to write this cumbersome command every time you run it, you can set it in the file configuration like this: Cargo.toml

[dependencies]
bevy = { version = "0.12.0", features = ["dynamic_linking"] }
  1. LLD linker: The Rust compiler spends a lot of time in the "linking" step. LLD links much faster than the default Rust linker. To install LLD, find your operating system below and run the given command:
Ubuntu: sudo apt-get install lld

Fedora: sudo dnf install lld

Arch: sudo pacman -S lld

Windows: Make sure you have the latest cargo-binutils, as this allows commands like the LLD linker to be used automatically.
cargo install -f cargo-binutils
rustup component add llvm-tools-preview

MacOS: You can install lld manually following the instructions below or install llvm via brew, which includes lld: brew install llvm
  1. Alternative - Mold linker: Mold is 5× (five times!) faster than LLD, but there are some caveats, such as limited platform support and occasional stability issues. To install mold, find your operating system below and run the given command:
Ubuntu: sudo apt-get install mold clang

Fedora: sudo dnf install mold clang

Arch: sudo pacman -S mold clang

Windows: currently not planned for support See this tracking issue for more information.

MacOS: is available commercially with sold
You also need to add the following to your Cargo configuration: YOUR_WORKSPACE/.cargo/config.toml

[target.x86_64-unknown-linux-gnu]
linker = "clang"
rustflags = ["-C", "link-arg=-fuse-ld=/usr/bin/mold"]
Note: Disabling it may improve the performance of this linker.

Build Bevy#

Now run it again. The Bevy dependencies should start building. This will take some time because you are actually building the engine from scratch. You only need to do a full rebuild once. Every build after that will be fast! cargo run

Now that we have set up our Bevy project, we are ready to start making our first Bevy application!

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.