rust_minifb/src/lib.rs

79 lines
1.5 KiB
Rust
Raw Normal View History

2015-11-22 18:55:38 +01:00
extern crate libc;
use std::ffi::CString;
use std::mem::transmute;
use libc::{c_char, c_int, c_void};
#[cfg(target_os = "windows")]
pub mod windows;
2015-11-23 20:21:11 +01:00
#[cfg(target_os = "macos")]
2015-11-22 18:55:38 +01:00
#[link(name = "Cocoa", kind = "framework")]
extern {
fn mfb_open(name: *const c_char, width: c_int, height: c_int) -> c_int;
fn mfb_update(buffer: *mut c_void) -> c_int;
fn mfb_close();
}
/*
2015-11-23 20:21:11 +01:00
#[cfg(target_os = "windows")]
#[link(name = "gdi32")]
extern {
fn mfb_open(name: *const c_char, width: c_int, height: c_int) -> c_int;
fn mfb_update(buffer: *mut c_void) -> c_int;
fn mfb_close();
}
*/
2015-11-23 20:21:11 +01:00
2015-11-23 20:44:21 +01:00
#[cfg(target_os = "linux")]
#[link(name = "X11")]
extern {
fn mfb_open(name: *const c_char, width: c_int, height: c_int) -> c_int;
fn mfb_update(buffer: *mut c_void) -> c_int;
fn mfb_close();
}
2015-11-23 20:21:11 +01:00
2015-11-22 18:55:38 +01:00
///
/// Open up a window
///
#[cfg(any(target_os = "linux", target_os = "mac"))]
2015-11-22 18:55:38 +01:00
pub fn open(name: &str, width: usize, height: usize) -> bool {
let s = CString::new(name).unwrap();
let ret;
unsafe {
ret = mfb_open(s.as_ptr(), width as c_int, height as c_int);
}
match ret {
0 => false,
_ => true,
}
}
///
/// Update
///
#[cfg(any(target_os = "linux", target_os = "mac"))]
2015-11-22 18:55:38 +01:00
pub fn update(buffer: &[u32]) -> bool {
let ret;
unsafe {
ret = mfb_update(transmute(buffer.as_ptr()));
}
if ret < 0 {
return false;
} else {
return true;
}
}
///
/// Close
///
#[cfg(any(target_os = "linux", target_os = "mac"))]
2015-11-22 18:55:38 +01:00
pub fn close() {
unsafe {
mfb_close();
}
}