Started to work on Rust only version for Windows

This commit is contained in:
Daniel Collin 2015-11-24 21:46:52 +01:00
parent 858b07f506
commit 298e535de2
4 changed files with 59 additions and 3 deletions

View file

@ -9,4 +9,5 @@ gcc = "0.3.19"
[dependencies]
libc = "0.1.10"
user32-sys = "0.1.2"

View file

@ -8,9 +8,9 @@ fn main() {
&["src/native/macosx/MacMiniFB.m",
"src/native/macosx/OSXWindow.m",
"src/native/macosx/OSXWindowFrameView.m"]); // MacOS
} else if env.contains("windows") {
gcc::compile_library("libminifb_native.a", &["src/native/windows/WinMiniFB.c"]); // Windows
} else {
// } else if env.contains("windows") {
// gcc::compile_library("libminifb_native.a", &["src/native/windows/WinMiniFB.c"]); // Windows
} else if env.contains("linux") {
gcc::compile_library("libminifb_native.a", &["src/native/x11/X11MiniFB.c"]); // Unix
}
}

View file

@ -3,6 +3,9 @@ use std::ffi::CString;
use std::mem::transmute;
use libc::{c_char, c_int, c_void};
#[cfg(target_os = "windows")]
pub mod windows;
#[cfg(target_os = "macos")]
#[link(name = "Cocoa", kind = "framework")]
extern {
@ -11,6 +14,7 @@ extern {
fn mfb_close();
}
/*
#[cfg(target_os = "windows")]
#[link(name = "gdi32")]
extern {
@ -18,6 +22,7 @@ extern {
fn mfb_update(buffer: *mut c_void) -> c_int;
fn mfb_close();
}
*/
#[cfg(target_os = "linux")]
#[link(name = "X11")]
@ -30,6 +35,7 @@ extern {
///
/// Open up a window
///
#[cfg(any(target_os = "linux", target_os = "mac"))]
pub fn open(name: &str, width: usize, height: usize) -> bool {
let s = CString::new(name).unwrap();
let ret;
@ -47,6 +53,7 @@ pub fn open(name: &str, width: usize, height: usize) -> bool {
///
/// Update
///
#[cfg(any(target_os = "linux", target_os = "mac"))]
pub fn update(buffer: &[u32]) -> bool {
let ret;
unsafe {
@ -63,6 +70,7 @@ pub fn update(buffer: &[u32]) -> bool {
///
/// Close
///
#[cfg(any(target_os = "linux", target_os = "mac"))]
pub fn close() {
unsafe {
mfb_close();

47
src/windows.rs Normal file
View file

@ -0,0 +1,47 @@
extern crate user;
pub fn open(name: &str, width: usize, height: usize) -> bool {
let s = CString::new(name).unwrap();
unsafe {
let class = winapi::WNDCLASSEXA {
cbSize: mem::size_of::<winapi::WNDCLASSEXW>() as winapi::UINT,
style: winapi::CS_HREDRAW | winapi::CS_VREDRAW | winapi::CS_OWNDC,
lpfnWndProc: Some(callback::callback),
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: kernel32::GetModuleHandleA(ptr::null()),
hIcon: ptr::null_mut(),
hCursor: ptr::null_mut(),
hbrBackground: ptr::null_mut(),
lpszMenuName: ptr::null(),
lpszClassName: s.as_ptr(),
hIconSm: ptr::null_mut(),
};
user32::RegisterClassExA(&class);
let mut rect = winapi::RECT {
left: 0, right: width as winapi::LONG,
top: 0, bottom: height as winapi::LONG,
}
user32::AdjustWindowRect(&mut rect, winapi::WS_POPUP | winapi::WS_SYSMENU | winapi::WS_CAPTION, 0);
rect.right -= rect.left;
rect.bottom -= rect.top;
let handle = user32::CreateWindowExA(0,
s.as_ptr(), s.as_ptr(),
winapi::WS_OVERLAPPEDWINDOW & ~winapi::WS_MAXIMIZEBOX & ~winapi::WS_TICKFRAME,
winapi::CW_USEDEFAULT, winapi::CW_USEDEFAULT,
rect.right, rect.bottom,
ptr::null_mut(), ptr::null_mut(), ptr::null_mut(), ptr::null_mut());
if handle.is_null() {
return false;
}
user32::ShowWindow(handle, winapi::SW_NORMAL);
}