chore: Clean up win32 and x11
This commit is contained in:
parent
abfb2ac4f9
commit
300a4d819a
3 changed files with 122 additions and 33 deletions
67
src/win32.rs
67
src/win32.rs
|
|
@ -1,20 +1,30 @@
|
|||
//! Implementation of software buffering for Windows.
|
||||
//!
|
||||
//! This module converts the input buffer into a bitmap and then stretches it to the window.
|
||||
|
||||
use crate::{GraphicsContextImpl, SwBufError};
|
||||
use raw_window_handle::Win32WindowHandle;
|
||||
|
||||
use std::mem;
|
||||
use std::io;
|
||||
use std::os::raw::c_int;
|
||||
|
||||
use windows_sys::Win32::Foundation::HWND;
|
||||
use windows_sys::Win32::Graphics::Gdi::{
|
||||
StretchDIBits, BITMAPINFOHEADER, BI_BITFIELDS, RGBQUAD, HDC,
|
||||
ValidateRect, GetDC, SRCCOPY, DIB_RGB_COLORS,
|
||||
GetDC, StretchDIBits, ValidateRect, BITMAPINFOHEADER, BI_BITFIELDS, DIB_RGB_COLORS, HDC,
|
||||
RGBQUAD, SRCCOPY,
|
||||
};
|
||||
|
||||
/// The handle to a window for software buffering.
|
||||
pub struct Win32Impl {
|
||||
/// The window handle.
|
||||
window: HWND,
|
||||
|
||||
/// The device context for the window.
|
||||
dc: HDC,
|
||||
}
|
||||
|
||||
// Wrap this so we can have a proper number of bmiColors to write in
|
||||
// From minifb
|
||||
/// The Win32-compatible bitmap information.
|
||||
#[repr(C)]
|
||||
struct BitmapInfo {
|
||||
pub bmi_header: BITMAPINFOHEADER,
|
||||
|
|
@ -22,27 +32,43 @@ struct BitmapInfo {
|
|||
}
|
||||
|
||||
impl Win32Impl {
|
||||
/// Create a new `Win32Impl` from a `Win32WindowHandle`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The `Win32WindowHandle` must be a valid window handle.
|
||||
pub unsafe fn new(handle: &Win32WindowHandle) -> Result<Self, crate::SwBufError> {
|
||||
let dc = GetDC(handle.hwnd as HWND);
|
||||
|
||||
if dc == 0 {
|
||||
return Err(SwBufError::PlatformError(Some("Device Context is null".into()), None));
|
||||
// It is valid for the window handle to be null here. Error out if it is.
|
||||
if handle.hwnd.is_null() {
|
||||
return Err(SwBufError::IncompleteWindowHandle);
|
||||
}
|
||||
|
||||
Ok(
|
||||
Self {
|
||||
dc,
|
||||
window: handle.hwnd as HWND,
|
||||
}
|
||||
)
|
||||
// Get the handle to the device context.
|
||||
// SAFETY: We have confirmed that the window handle is valid.
|
||||
let hwnd = handle.hwnd as HWND;
|
||||
let dc = GetDC(hwnd);
|
||||
|
||||
// GetDC returns null if there is a platform error.
|
||||
if dc == 0 {
|
||||
return Err(SwBufError::PlatformError(
|
||||
Some("Device Context is null".into()),
|
||||
Some(Box::new(io::Error::last_os_error())),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
dc,
|
||||
window: 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();
|
||||
// Create a new bitmap info struct.
|
||||
let mut bitmap_info: BitmapInfo =mem::zeroed();
|
||||
|
||||
bitmap_info.bmi_header.biSize = std::mem::size_of::<BITMAPINFOHEADER>() as u32;
|
||||
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 = BI_BITFIELDS;
|
||||
|
|
@ -52,6 +78,10 @@ impl GraphicsContextImpl for Win32Impl {
|
|||
bitmap_info.bmi_colors[1].rgbGreen = 0xff;
|
||||
bitmap_info.bmi_colors[2].rgbBlue = 0xff;
|
||||
|
||||
// Stretch the bitmap onto the window.
|
||||
// SAFETY:
|
||||
// - The bitmap information is valid.
|
||||
// - The buffer is a valid pointer to image data.
|
||||
StretchDIBits(
|
||||
self.dc,
|
||||
0,
|
||||
|
|
@ -62,12 +92,13 @@ impl GraphicsContextImpl for Win32Impl {
|
|||
0,
|
||||
width as c_int,
|
||||
height as c_int,
|
||||
std::mem::transmute(buffer.as_ptr()),
|
||||
std::mem::transmute(&bitmap_info),
|
||||
buffer.as_ptr().cast(),
|
||||
&bitmap_info as *const BitmapInfo as *const _,
|
||||
DIB_RGB_COLORS,
|
||||
SRCCOPY,
|
||||
);
|
||||
|
||||
// Validate the window.
|
||||
ValidateRect(self.window, std::ptr::null_mut());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue