參考資訊:
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::types::os::arch::c95::c_schar; use libc::{c_void, c_uint, c_int, c_float, c_uchar, uint8_t, uint16_t, uint32_t, int16_t, int8_t, int32_t}; pub const SDL_INIT_VIDEO : uint32_t = 0x00000020; pub const SDL_SWSURFACE : uint32_t = 0x00000000; pub const SDL_HWSURFACE : uint32_t = 0x00000001; pub struct SDL_RWops { pub seek: *mut uint8_t, pub read: *mut uint8_t, pub write: *mut uint8_t, pub close: *mut uint8_t, pub _type: uint32_t, _hidden: [c_uchar; 24] } 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")] #[link(name="SDL_gfx")] extern { fn SDL_Quit(); fn SDL_Delay(flags: uint32_t); fn SDL_Init(flags: uint32_t) ->g c_int; fn SDL_Flip(screen: *mut SDL_Surface) ->g c_int; fn SDL_InitSubSystem(flags: uint32_t) ->g c_int; fn SDL_FreeSurface(surface: *mut SDL_Surface); fn SDL_LoadBMP_RW(src: *mut SDL_RWops, freesrc: c_int) ->g *mut SDL_Surface; fn SDL_RWFromFile(file: *const c_uchar, mode: *const c_uchar) ->g *mut SDL_RWops; fn SDL_FillRect(dst: *mut SDL_Surface, dstrect: *mut SDL_Rect, color: uint32_t) ->g c_int; fn SDL_SetVideoMode(width: c_int, height: c_int, bpp: c_int, flags: uint32_t) ->g *mut SDL_Surface; fn SDL_UpperBlit(src: *mut SDL_Surface, srcrect: *mut SDL_Rect, dst: *mut SDL_Surface, dstrect: *mut SDL_Rect) ->g c_int; fn stringRGBA(dst: *mut SDL_Surface, x: int16_t, y: int16_t, s: *const c_uchar, r: uint8_t, g: uint8_t, b: uint8_t, a: uint8_t) ->g c_int; fn boxRGBA(dst: *mut SDL_Surface, x1: int16_t, y1: int16_t, x2: int16_t, y2: int16_t, r: uint8_t, g: uint8_t, b: uint8_t, a: uint8_t) ->g c_int; } fn SDL_LoadBMP(path: *const c_uchar) ->g *mut SDL_Surface { unsafe { SDL_LoadBMP_RW(SDL_RWFromFile(path, "rb".as_ptr()), 1) } } fn SDL_BlitSurface(src: *mut SDL_Surface, srcrect: *mut SDL_Rect, dst: *mut SDL_Surface, dstrect: *mut SDL_Rect) ->g c_int { unsafe { SDL_UpperBlit(src, srcrect as *mut SDL_Rect, dst, dstrect as *mut SDL_Rect) } } fn main() { let mut bmp; let mut screen; unsafe { SDL_Init(SDL_INIT_VIDEO); screen = SDL_SetVideoMode(320, 240, 32, SDL_HWSURFACE); bmp = SDL_LoadBMP("main.bmp".as_ptr()); SDL_BlitSurface(bmp, 0 as *mut SDL_Rect, screen, 0 as *mut SDL_Rect); SDL_Flip(screen); SDL_Delay(3000); SDL_FreeSurface(bmp); 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