https://github.com/Rust-SDL2/rust-sdl2 を使う
macならbrewで入る
brew install sdl2 sdl2_gfx sdl2_image sdl2_ttf
Cargo.tomlに sdl2
を追加する
sdl2 = "0.31.0"
拡張を使う場合はfeaturesを指定する
[dependencies.sdl2]
version = "0.31.0"
default-features = false
features = ["image"]
描画に関わるオブジェクト
- SDL_Window
- SDL_Surface
- SDL_Renderer
- SDL_Texture
rust-sdl2はSDL2のCのAPIがラップされていて、オブジェクト指向っぽく使える
描画は sdl2::render::Canvas
から行う
https://docs.rs/sdl2/0.30.0/sdl2/render/index.html
Canvas.texture_creator
からTextureCreator
を取得する。
画像を読み込む場合はsdl2_imageのラッパーである、sdl2::imageを初期化して、TextureCreator
のload_texture
に渡す。
let _image_context = sdl2::image::init(INIT_PNG | INIT_JPG).unwrap();
let texture_creator = canvas.texture_creator();
let texture = texture_creator.load_texture(png).unwrap();
描画はCanvas.copy
でおこなう。
canvas.copy(&texture, None, None).expect("Render failed");
- SDL2 Official Wiki https://wiki.libsdl.org/FrontPage