From d89a0c92aa0d89829a638e89a49f160de4ddeab0 Mon Sep 17 00:00:00 2001 From: David Johnson Date: Sun, 16 Jan 2022 08:03:20 -0600 Subject: [PATCH] Added windows support and made example render something fancier --- Cargo.toml | 5 ++++ examples/winit.rs | 12 ++++++++- src/lib.rs | 9 +++++-- src/win32.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/win32.rs diff --git a/Cargo.toml b/Cargo.toml index 83fa30c..da20db4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,12 @@ edition = "2021" [dependencies] raw-window-handle = "0.4.2" + +[target.'cfg(target_os = "linux")'.dependencies] x11-dl = "2.19.1" +[target.'cfg(target_os = "windows")'.dependencies] +winapi = "0.3.9" + [dev-dependencies] winit = "0.26.1" \ No newline at end of file diff --git a/examples/winit.rs b/examples/winit.rs index 1fa128a..094e5c9 100644 --- a/examples/winit.rs +++ b/examples/winit.rs @@ -17,7 +17,17 @@ fn main() { let size = graphics_context.window().inner_size(); (size.width, size.height) }; - let buffer = vec![0x00FF00FF; (width * height) as usize]; + let buffer = (0..((width*height) as usize)).map(|index|{ + let y = index / (width as usize); + let x = index % (width as usize); + let red = x % 255; + let green = y % 255; + let blue = (x*y) % 255; + + let color = blue | (green << 8) | (red << 16); + + color as u32 + }).collect::>(); graphics_context.set_buffer(&buffer, width as u16, height as u16); } diff --git a/src/lib.rs b/src/lib.rs index 9f35689..37451a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,9 @@ +#[cfg(target_os = "linux")] mod x11; +#[cfg(target_os = "windows")] +mod win32; use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; -use crate::x11::X11Impl; pub struct GraphicsContext{ window: W, @@ -13,7 +15,10 @@ impl GraphicsContext { pub unsafe fn new(window: W) -> Self{ let raw_handle = window.raw_window_handle(); let imple = match raw_handle{ - RawWindowHandle::Xlib(xlib_handle) => Box::new(X11Impl::new(xlib_handle)), + #[cfg(target_os = "linux")] + RawWindowHandle::Xlib(xlib_handle) => Box::new(x11::X11Impl::new(xlib_handle)), + #[cfg(target_os = "windows")] + RawWindowHandle::Win32(win32_handle) => Box::new(win32::Win32Impl::new(&win32_handle)), unimplemented_handle_type => unimplemented!("Unsupported window handle type: {}.", window_handle_type_name(&unimplemented_handle_type)) }; diff --git a/src/win32.rs b/src/win32.rs new file mode 100644 index 0000000..a78f7db --- /dev/null +++ b/src/win32.rs @@ -0,0 +1,65 @@ +use std::os::raw::c_int; +use raw_window_handle::Win32Handle; +use crate::GraphicsContextImpl; +use winapi::um::wingdi::{BITMAPINFOHEADER, BI_BITFIELDS, RGBQUAD, StretchDIBits}; +use winapi::um::winuser::{ValidateRect, GetDC}; +use winapi::shared::windef::{HWND, HDC}; + +pub struct Win32Impl{ + window: HWND, + dc: HDC +} + +// Wrap this so we can have a proper number of bmiColors to write in +// From minifb +#[repr(C)] +struct BitmapInfo { + pub bmi_header: BITMAPINFOHEADER, + pub bmi_colors: [RGBQUAD; 3], +} + +impl Win32Impl { + + pub unsafe fn new(handle: &Win32Handle) -> Self{ + let dc = GetDC(handle.hwnd as HWND); + Self{ + dc, + window: handle.hwnd as HWND + } + } + +} + +impl GraphicsContextImpl for Win32Impl { + unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) { + let mut bitmap_info: BitmapInfo = std::mem::zeroed(); + + bitmap_info.bmi_header.biSize = std::mem::size_of::() as u32; + bitmap_info.bmi_header.biPlanes = 1; + bitmap_info.bmi_header.biBitCount = 32; + bitmap_info.bmi_header.biCompression = BI_BITFIELDS; + bitmap_info.bmi_header.biWidth = width as i32; + bitmap_info.bmi_header.biHeight = -(height as i32); + bitmap_info.bmi_colors[0].rgbRed = 0xff; + bitmap_info.bmi_colors[1].rgbGreen = 0xff; + bitmap_info.bmi_colors[2].rgbBlue = 0xff; + + StretchDIBits( + self.dc, + 0, + 0, + width as c_int, + height as c_int, + 0, + 0, + width as c_int, + height as c_int, + std::mem::transmute(buffer.as_ptr()), + std::mem::transmute(&bitmap_info), + winapi::um::wingdi::DIB_RGB_COLORS, + winapi::um::wingdi::SRCCOPY + ); + + ValidateRect(self.window, std::ptr::null_mut()); + } +} \ No newline at end of file