- Rust Programming By Example
- Guillaume Gomez Antoni Boucher
- 329字
- 2021-07-02 19:13:06
Playing with images
Just like textures, we need to initialize the image context. Now that we've activated the image feature, we can call the linked functions and import them. Let's add some new imports:
use sdl2::image::{LoadTexture, INIT_PNG, INIT_JPG};
Then we create the image context:
sdl2::image::init(INIT_PNG | INIT_JPG).expect("Couldn't initialize
image context");
Now that the context has been initialized, let's actually load the image:
let image_texture =
texture_creator.load_texture("assets/my_image.png") .expect("Couldn't load image");
A few explanations for the preceding code:
load_texture takes a file path as an argument. Be very careful with paths, even more when they're relative!
After that, it's just like we did with other textures. Let's put our image into our Window's background:
canvas.copy(&Image_texture, None, None).expect("Render failed");
To sum everything up, here's what your project's folder should look like now:
|- your_project/ | |- Cargo.toml |- src/ | | | |- main.rs |- assets/ | |- my_image.png
And that's it!
Here's the full code in case you missed a step:
extern crate sdl2; use sdl2::pixels::Color; use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::render::TextureCreator; use sdl2::image::{LoadTexture, INIT_PNG, INIT_JPG}; use std::time::Duration; 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"); sdl2::image::init(INIT_PNG | INIT_JPG).expect("Couldn't
initialize
image context"); let window = video_subsystem.window("rust-sdl2 image demo", 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"); let texture_creator: TextureCreator<_> =
canvas.texture_creator(); let image_texture =
texture_creator.load_texture("assets/my_image.png") .expect("Couldn't load image"); 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 }, _ => {} } } canvas.set_draw_color(Color::RGB(0, 0, 0)); canvas.clear(); canvas.copy(&image_texture, None, None).expect("Render
failed"); canvas.present(); ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); } }
In my case, it gives the following output:

Figure 2.6
Now that we know how to make Windows and play with events and textures, let's see how to save and load high scores from files!
- 區(qū)塊鏈在電子檔案管理中的應(yīng)用
- 專業(yè)圖書館發(fā)展之道
- 北宋書籍刊刻與古文運動
- 國內(nèi)圖書情報知識圖譜實證研究
- 檔案社會控制功能研究
- 中國古代戲曲目錄研究
- 李一氓文存(第四卷):古籍整理
- 現(xiàn)代圖書館全面質(zhì)量管理與創(chuàng)新服務(wù)研究
- 大學(xué)圖書館信息服務(wù)與信息素養(yǎng)教育理論與實踐研究
- 圖書館保障弱勢群體公共信息獲取權(quán)益的對策研究
- 信息檢索
- 中國人民大學(xué)復(fù)印報刊資料轉(zhuǎn)載指數(shù)排名研究報告2017
- 新國學(xué)(第十五卷)
- 李一氓文存(第三卷):題跋·詩聯(lián)·劇本
- 金開誠文選