Added windows support and made example render something fancier

This commit is contained in:
David Johnson 2022-01-16 08:03:20 -06:00
parent a36b11a934
commit d89a0c92aa
4 changed files with 88 additions and 3 deletions

View file

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

View file

@ -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::<Vec<_>>();
graphics_context.set_buffer(&buffer, width as u16, height as u16);
}

View file

@ -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<W: HasRawWindowHandle>{
window: W,
@ -13,7 +15,10 @@ impl<W: HasRawWindowHandle> GraphicsContext<W> {
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))
};

65
src/win32.rs Normal file
View file

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