1. 安装rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
2. 设置rust国内源
#当前用户目录下 /linuxidc/.cargo/ 的.cargo 文件夹,进入.cargo 当前目录,在当前目下创建 config 文件
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"
3. 安装SDL库
dnf install SDL2
dnf install SDL2-devel
dnf install SDL2_image-devel
dnf install SDL2_gfx-devel
dnf install SDL2_mixer-devel
dnf install SDL2_ttf-devel
4. 创建rust项目
cargo new testSDL
cd testSDL
编辑cargo.toml,加入sdl2依赖
[dongfeng@localhost testSDL]$ cat Cargo.toml
[package]
name = "testsdl"
version = "0.1.0"
authors = ["dongfeng <workerwork@qq.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
sdl2 = "0.34"
vim src/main.rs
[dongfeng@localhost testSDL]$ cat src/main.rs
//extern crate sdl2;
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use std::time::Duration;
pub fn main() {
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem.window("rust-sdl2 demo", 800, 600)
.position_centered()
.build()
.unwrap();
let mut canvas = window.into_canvas().build().unwrap();
canvas.set_draw_color(Color::RGB(0, 255, 255));
canvas.clear();
canvas.present();
let mut event_pump = sdl_context.event_pump().unwrap();
let mut i = 0;
'running: loop {
i = (i + 1) % 255;
canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
canvas.clear();
for event in event_pump.poll_iter() {
match event {
Event::Quit {..} |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
_ => {}
}
}
// The rest of the game loop goes here...
canvas.present();
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
}
}
5. 运行项目
cargo run --release