When I first started learning Rust, I had no idea what to do next after reading Rust Book and other official recommended tutorials. I think many people feel the same way. Then I found a tutorial for developing a complete application with Rust. Unfortunately, I can’t find the series of tutorials on the author’s blog, but the code I wrote in the beginning is still there, and I would like to restore that tutorial.
First, a project to develop a desktop application using rust-SDL2 and most of rust’s syntax features.
directory
Create a complete application 0x01 using Rust. Create a window 0x02 using SDL2. Handle window close 0x03. Use macros to define event 0x04. View render 0x05. View operation 0x06. Rectangle 0x07 on window. Handle Sprite 0x08. Add game background 0x09. Game menu
Desired effect
Preparation before starting
Before we start coding, let’s get a few things together, so install SDL2 and run the commands directly from the terminal on macOS to install BREW install SDL2. Install the higher version of Rust, preferably nightly. There are some unstable features for 2018 Edition.
The Rust version of my machine
Rust 1.37.0 – the nightly e4a56b4b (0 2019-06-13)
Then select a directory to create the project
cargo new rs-arcade
Copy the code
Modify the Cargo. Toml file and add the rust-SDl2 dependency. I’ll use version 0.29 here, because I haven’t learned the version after 0.30, and migrating to a new version can also be a learning goal when the project is complete.
[package]
name = "rs-arcade"
version = "0.1.0 from"
authors = ["LimitLiu <[email protected]>"]
edition = "2018"
[dependencies]
sdl2 = "0.29"
Copy the code
Next, execute cargo Run startup directly at the project root while installing the dependencies. Considering that the domestic network is very good, it is recommended to configure a domestic source. The official admin repository is at https://crates.io, we create a config file in the $HOME/.cargo directory and add these contents
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
Copy the code
Once saved, execute cargo Run.
Once the execution is successful, we upload the code to git hosting platform. I use domestic Coding platform to host, reason or domestic excellent network. / / while it becomes painful training of https://e.coding.net/limitLiu/rs-arcade.git
Now that you’ve done the initial work of the project, let’s start by displaying the window.