Working Win32 version in pure Rust

This commit is contained in:
Daniel Collin 2015-11-27 23:42:28 +01:00
parent a9b0a11853
commit 15ec55aae2
3 changed files with 52 additions and 14 deletions

View file

@ -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"

View file

@ -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();
}

View file

@ -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::<BITMAPINFOHEADER>() 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 {