參考資訊:
https://www.rust-lang.org/learn/get-started
https://users.rust-lang.org/t/calling-c-function-with-string/41448/10
http://bzz.wallizard.com:8081/share/books/RUST/Programming%20Rust%202nd%20Edition.pdf
main.rs
extern crate libc; use libc::{c_void, c_uint, c_int, c_float, c_uchar, uint8_t, uint16_t, uint32_t, int32_t}; pub const SDL_INIT_VIDEO : uint32_t = 0x00000020; pub const SDL_SWSURFACE : uint32_t = 0x00000000; pub struct SDL_Rect { pub x: i16, pub y: i16, pub w: u16, pub h: u16 } pub struct SDL_Color { pub r: uint8_t, pub g: uint8_t, pub b: uint8_t, pub unused: uint8_t } pub struct SDL_Palette { pub ncolors: c_int, pub colors: *mut SDL_Color, } pub struct SDL_PixelFormat { pub palette: *mut SDL_Palette, pub BitsPerPixel: uint8_t, pub BytesPerPixel: uint8_t, pub Rloss: uint8_t, pub Gloss: uint8_t, pub Bloss: uint8_t, pub Aloss: uint8_t, pub Rshift: uint8_t, pub Gshift: uint8_t, pub Bshift: uint8_t, pub Ashift: uint8_t, pub Rmask: uint32_t, pub Gmask: uint32_t, pub Bmask: uint32_t, pub Amask: uint32_t, pub colorkey: uint32_t, pub alpha: uint8_t, } pub struct SDL_Surface { pub flags: uint32_t, pub format: *mut SDL_PixelFormat, pub w: c_int, pub h: c_int, pub pitch: uint16_t, pub pixels: *mut c_void, pub offset: c_int, pub hwdata: *mut c_void, pub clip_rect: SDL_Rect, pub unused1: uint32_t, pub locked: uint32_t, pub map: *mut c_void, pub format_version: c_uint, pub refcount: c_int } #[link(name="SDL")] extern { fn SDL_Quit(); fn SDL_Init(flags: uint32_t) -> c_int; fn SDL_Flip(screen: *mut SDL_Surface) -> c_int; fn SDL_InitSubSystem(flags: uint32_t) -> c_int; fn SDL_FillRect(dst: *mut SDL_Surface, dstrect: *mut SDL_Rect, color: uint32_t) -> c_int; fn SDL_SetVideoMode(width: c_int, height: c_int, bpp: c_int, flags: uint32_t) -> *mut SDL_Surface; } fn main() { let mut screen; unsafe { let mut rt = SDL_Rect{x:0, y:0, w:320, h:240}; SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(320, 240, 16, SDL_SWSURFACE); SDL_FillRect(screen, &mut rt as *mut SDL_Rect, 0xf800); SDL_Flip(screen); } std::thread::sleep_ms(3000); unsafe { SDL_Quit(); } }
Cargo.toml
[package] name = "main" version = "0.0.1" authors = ["Steward-Fu <steward.fu@gmail.com>"] [[bin]] name = "main" path = "main.rs" [dependencies] libc = "0.1"
編譯、執行
$ cargo build $ cargo run