From 15ec55aae2b9d36dc283a74fffe2d92d9be19a91 Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Fri, 27 Nov 2015 23:42:28 +0100 Subject: [PATCH] Working Win32 version in pure Rust --- Cargo.toml | 1 + examples/noise.rs | 18 +++++------------- src/windows.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c2e89df..6333080 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ libc = "0.1.10" user32-sys = "0.1.2" winapi = "0.2.4" kernel32-sys = "0.1.4" +gdi32-sys = "0.1.1" diff --git a/examples/noise.rs b/examples/noise.rs index 98029b1..28a3155 100644 --- a/examples/noise.rs +++ b/examples/noise.rs @@ -6,20 +6,15 @@ const WIDTH: usize = 640; const HEIGHT: usize = 360; fn main() { - //let mut noise; - //let mut carry; - //let mut seed = 0xbeefu32; + let mut noise; + let mut carry; + let mut seed = 0xbeefu32; - //let mut buffer: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT]; + let mut buffer: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT]; let mut mfb = Minifb::new("Noise Test - Press ESC to exit", WIDTH, HEIGHT).unwrap(); - while mfb.update() { - } - - /* - while mfb.update() { - + while mfb.update(&buffer) { for i in buffer.iter_mut() { noise = seed; noise >>= 3; @@ -32,7 +27,4 @@ fn main() { *i = (noise << 16) | (noise << 8) | noise; } } - */ - - //minifb::close(); } diff --git a/src/windows.rs b/src/windows.rs index fbf6fd7..df4b2af 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,6 +1,8 @@ extern crate user32; extern crate kernel32; extern crate winapi; +extern crate gdi32; + use std::ffi::CString; use std::ptr; use std::os::windows::ffi::OsStrExt; @@ -10,9 +12,18 @@ use std::mem; use self::winapi::windef::HWND; use self::winapi::winuser::WS_OVERLAPPEDWINDOW; use self::winapi::winuser::WNDCLASSW; +use self::winapi::wingdi::BITMAPINFOHEADER; +use self::winapi::wingdi::RGBQUAD; static mut CLOSE_APP: bool = false; +// Wrap this so we can have a proper numbef of bmiColors to write in +#[repr(C)] +struct BitmapInfo { + pub bmi_header: BITMAPINFOHEADER, + pub bmi_colors: [RGBQUAD; 3], +} + unsafe extern "system" fn wnd_proc(window: winapi::HWND, msg: winapi::UINT, wparam: winapi::WPARAM, lparam: winapi::LPARAM) -> winapi::LRESULT @@ -24,6 +35,39 @@ unsafe extern "system" fn wnd_proc(window: winapi::HWND, msg: winapi::UINT, } } + winapi::winuser::WM_PAINT => { + let mut rect: winapi::RECT = mem::uninitialized(); + let buffer = user32::GetWindowLongPtrW(window, winapi::winuser::GWLP_USERDATA); + + user32::GetClientRect(window, &mut rect); + + let mut bitmap_info: BitmapInfo = mem::zeroed(); + let width = rect.right - rect.left; + let height = rect.bottom - rect.top; + + bitmap_info.bmi_header.biSize = mem::size_of::() as u32; + bitmap_info.bmi_header.biPlanes = 1; + bitmap_info.bmi_header.biBitCount = 32; + bitmap_info.bmi_header.biCompression = winapi::wingdi::BI_BITFIELDS; + bitmap_info.bmi_header.biWidth = width; + bitmap_info.bmi_header.biHeight = -height; + bitmap_info.bmi_colors[0].rgbRed = 0xff; + bitmap_info.bmi_colors[1].rgbGreen = 0xff; + bitmap_info.bmi_colors[2].rgbBlue = 0xff; + + let dc = user32::GetDC(window); + + gdi32::StretchDIBits(dc, 0, 0, width, height, 0, 0, width, height, + mem::transmute(buffer), + mem::transmute(&bitmap_info), + winapi::wingdi::DIB_RGB_COLORS, + winapi::wingdi::SRCCOPY); + + user32::ValidateRect(window, ptr::null_mut()); + + user32::ReleaseDC(window, dc); + } + _ => (), } @@ -98,10 +142,11 @@ impl Minifb { } } - pub fn update(&mut self) -> bool { + pub fn update(&mut self, buffer: &[u32]) -> bool { unsafe { let mut msg = mem::uninitialized(); + user32::SetWindowLongPtrW(self.window, winapi::winuser::GWLP_USERDATA, buffer.as_ptr() as i64); user32::InvalidateRect(self.window, ptr::null_mut(), winapi::TRUE); while user32::PeekMessageW(&mut msg, self.window, 0, 0, winapi::winuser::PM_REMOVE) != 0 {