- Rust Programming By Example
- Guillaume Gomez Antoni Boucher
- 196字
- 2021-07-02 19:13:04
Tetris
Okay, we're now ready to start writing down our tetris!
First, let's fulfill our main.rs file in order to check whether everything is working as expected:
extern crate sdl2; use sdl2::pixels::Color; use sdl2::event::Event; use sdl2::keyboard::Keycode; use std::time::Duration;
use std::thread::sleep; pub fn main() { let sdl_context = sdl2::init().expect("SDL initialization
failed"); let video_subsystem = sdl_context.video().expect("Couldn't get
SDL video subsystem"); let window = video_subsystem.window("rust-sdl2 demo: Video", 800,
600) .position_centered() .opengl() .build() .expect("Failed to create window"); let mut canvas = window.into_canvas().build().expect("Failed to
convert window into canvas"); canvas.set_draw_color(Color::RGB(255, 0, 0)); canvas.clear(); canvas.present(); let mut event_pump = sdl_context.event_pump().expect("Failed to
get SDL event pump"); 'running: loop { for event in event_pump.poll_iter() { match event { Event::Quit { .. } | Event::KeyDown { keycode: Some(Keycode::Escape), .. } =>
{ break 'running }, _ => {} } } sleep(Duration::new(0, 1_000_000_000u32 / 60)); } }
You'll note the following line:
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
It allows you to avoid using all your computer CPU time needlessly and only rendering 60 times every second at most.
Now run the following in your terminal:
$ cargo run
If you have a window filled with red (just as shown in the following screenshot), then everything's fine!

Figure 2.4
推薦閱讀
- 普通高校中文學(xué)科基礎(chǔ)教材古典文獻(xiàn)學(xué)基礎(chǔ)
- 地方檔案與文獻(xiàn)研究(第2輯)
- 胡華文集(第一卷)
- 全民閱讀組織活動(dòng)讀本
- 蒲城文獻(xiàn)征錄
- 北宋書籍刊刻與古文運(yùn)動(dòng)
- 檔案社會(huì)控制功能研究
- 圖書館知識(shí)整合與知識(shí)服務(wù)研究:以西部社會(huì)科學(xué)院圖書館為例
- 李一氓文存(第四卷):古籍整理
- 檔案庫(kù)房技術(shù)管理
- 現(xiàn)代圖書館全面質(zhì)量管理與創(chuàng)新服務(wù)研究
- 李一氓文存(第二卷):存在集三編下冊(cè)
- 《葉赫那拉氏族譜》整理與研究
- 天南學(xué)術(shù)(第一輯)
- 用戶畫像與博物館用戶體驗(yàn)