mirror of
https://github.com/italicsjenga/rust_minifb.git
synced 2025-01-10 02:51:32 +11:00
Working Win32 version in pure Rust
This commit is contained in:
parent
a9b0a11853
commit
15ec55aae2
|
@ -12,3 +12,4 @@ libc = "0.1.10"
|
||||||
user32-sys = "0.1.2"
|
user32-sys = "0.1.2"
|
||||||
winapi = "0.2.4"
|
winapi = "0.2.4"
|
||||||
kernel32-sys = "0.1.4"
|
kernel32-sys = "0.1.4"
|
||||||
|
gdi32-sys = "0.1.1"
|
||||||
|
|
|
@ -6,20 +6,15 @@ const WIDTH: usize = 640;
|
||||||
const HEIGHT: usize = 360;
|
const HEIGHT: usize = 360;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
//let mut noise;
|
let mut noise;
|
||||||
//let mut carry;
|
let mut carry;
|
||||||
//let mut seed = 0xbeefu32;
|
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();
|
let mut mfb = Minifb::new("Noise Test - Press ESC to exit", WIDTH, HEIGHT).unwrap();
|
||||||
|
|
||||||
while mfb.update() {
|
while mfb.update(&buffer) {
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
while mfb.update() {
|
|
||||||
|
|
||||||
for i in buffer.iter_mut() {
|
for i in buffer.iter_mut() {
|
||||||
noise = seed;
|
noise = seed;
|
||||||
noise >>= 3;
|
noise >>= 3;
|
||||||
|
@ -32,7 +27,4 @@ fn main() {
|
||||||
*i = (noise << 16) | (noise << 8) | noise;
|
*i = (noise << 16) | (noise << 8) | noise;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
//minifb::close();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
extern crate user32;
|
extern crate user32;
|
||||||
extern crate kernel32;
|
extern crate kernel32;
|
||||||
extern crate winapi;
|
extern crate winapi;
|
||||||
|
extern crate gdi32;
|
||||||
|
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::os::windows::ffi::OsStrExt;
|
use std::os::windows::ffi::OsStrExt;
|
||||||
|
@ -10,9 +12,18 @@ use std::mem;
|
||||||
use self::winapi::windef::HWND;
|
use self::winapi::windef::HWND;
|
||||||
use self::winapi::winuser::WS_OVERLAPPEDWINDOW;
|
use self::winapi::winuser::WS_OVERLAPPEDWINDOW;
|
||||||
use self::winapi::winuser::WNDCLASSW;
|
use self::winapi::winuser::WNDCLASSW;
|
||||||
|
use self::winapi::wingdi::BITMAPINFOHEADER;
|
||||||
|
use self::winapi::wingdi::RGBQUAD;
|
||||||
|
|
||||||
static mut CLOSE_APP: bool = false;
|
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,
|
unsafe extern "system" fn wnd_proc(window: winapi::HWND, msg: winapi::UINT,
|
||||||
wparam: winapi::WPARAM, lparam: winapi::LPARAM)
|
wparam: winapi::WPARAM, lparam: winapi::LPARAM)
|
||||||
-> winapi::LRESULT
|
-> 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 {
|
unsafe {
|
||||||
let mut msg = mem::uninitialized();
|
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);
|
user32::InvalidateRect(self.window, ptr::null_mut(), winapi::TRUE);
|
||||||
|
|
||||||
while user32::PeekMessageW(&mut msg, self.window, 0, 0, winapi::winuser::PM_REMOVE) != 0 {
|
while user32::PeekMessageW(&mut msg, self.window, 0, 0, winapi::winuser::PM_REMOVE) != 0 {
|
||||||
|
|
Loading…
Reference in a new issue