參考資訊:
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_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) -> 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 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) -> c_int;
}
fn main() {
let mut screen;
unsafe {
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(320, 240, 32, SDL_HWSURFACE);
boxRGBA(screen, 50, 100, 100, 150, 0xff, 0x00, 0x00, 0xff);
SDL_Flip(screen);
SDL_Delay(3000);
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