Skip to content

Commit d89a0c9

Browse files
committed
Added windows support and made example render something fancier
1 parent a36b11a commit d89a0c9

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ edition = "2021"
55

66
[dependencies]
77
raw-window-handle = "0.4.2"
8+
9+
[target.'cfg(target_os = "linux")'.dependencies]
810
x11-dl = "2.19.1"
911

12+
[target.'cfg(target_os = "windows")'.dependencies]
13+
winapi = "0.3.9"
14+
1015
[dev-dependencies]
1116
winit = "0.26.1"

examples/winit.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ fn main() {
1717
let size = graphics_context.window().inner_size();
1818
(size.width, size.height)
1919
};
20-
let buffer = vec![0x00FF00FF; (width * height) as usize];
20+
let buffer = (0..((width*height) as usize)).map(|index|{
21+
let y = index / (width as usize);
22+
let x = index % (width as usize);
23+
let red = x % 255;
24+
let green = y % 255;
25+
let blue = (x*y) % 255;
26+
27+
let color = blue | (green << 8) | (red << 16);
28+
29+
color as u32
30+
}).collect::<Vec<_>>();
2131

2232
graphics_context.set_buffer(&buffer, width as u16, height as u16);
2333
}

src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
#[cfg(target_os = "linux")]
12
mod x11;
3+
#[cfg(target_os = "windows")]
4+
mod win32;
25

36
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
4-
use crate::x11::X11Impl;
57

68
pub struct GraphicsContext<W: HasRawWindowHandle>{
79
window: W,
@@ -13,7 +15,10 @@ impl<W: HasRawWindowHandle> GraphicsContext<W> {
1315
pub unsafe fn new(window: W) -> Self{
1416
let raw_handle = window.raw_window_handle();
1517
let imple = match raw_handle{
16-
RawWindowHandle::Xlib(xlib_handle) => Box::new(X11Impl::new(xlib_handle)),
18+
#[cfg(target_os = "linux")]
19+
RawWindowHandle::Xlib(xlib_handle) => Box::new(x11::X11Impl::new(xlib_handle)),
20+
#[cfg(target_os = "windows")]
21+
RawWindowHandle::Win32(win32_handle) => Box::new(win32::Win32Impl::new(&win32_handle)),
1722
unimplemented_handle_type => unimplemented!("Unsupported window handle type: {}.", window_handle_type_name(&unimplemented_handle_type))
1823
};
1924

src/win32.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::os::raw::c_int;
2+
use raw_window_handle::Win32Handle;
3+
use crate::GraphicsContextImpl;
4+
use winapi::um::wingdi::{BITMAPINFOHEADER, BI_BITFIELDS, RGBQUAD, StretchDIBits};
5+
use winapi::um::winuser::{ValidateRect, GetDC};
6+
use winapi::shared::windef::{HWND, HDC};
7+
8+
pub struct Win32Impl{
9+
window: HWND,
10+
dc: HDC
11+
}
12+
13+
// Wrap this so we can have a proper number of bmiColors to write in
14+
// From minifb
15+
#[repr(C)]
16+
struct BitmapInfo {
17+
pub bmi_header: BITMAPINFOHEADER,
18+
pub bmi_colors: [RGBQUAD; 3],
19+
}
20+
21+
impl Win32Impl {
22+
23+
pub unsafe fn new(handle: &Win32Handle) -> Self{
24+
let dc = GetDC(handle.hwnd as HWND);
25+
Self{
26+
dc,
27+
window: handle.hwnd as HWND
28+
}
29+
}
30+
31+
}
32+
33+
impl GraphicsContextImpl for Win32Impl {
34+
unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
35+
let mut bitmap_info: BitmapInfo = std::mem::zeroed();
36+
37+
bitmap_info.bmi_header.biSize = std::mem::size_of::<BITMAPINFOHEADER>() as u32;
38+
bitmap_info.bmi_header.biPlanes = 1;
39+
bitmap_info.bmi_header.biBitCount = 32;
40+
bitmap_info.bmi_header.biCompression = BI_BITFIELDS;
41+
bitmap_info.bmi_header.biWidth = width as i32;
42+
bitmap_info.bmi_header.biHeight = -(height as i32);
43+
bitmap_info.bmi_colors[0].rgbRed = 0xff;
44+
bitmap_info.bmi_colors[1].rgbGreen = 0xff;
45+
bitmap_info.bmi_colors[2].rgbBlue = 0xff;
46+
47+
StretchDIBits(
48+
self.dc,
49+
0,
50+
0,
51+
width as c_int,
52+
height as c_int,
53+
0,
54+
0,
55+
width as c_int,
56+
height as c_int,
57+
std::mem::transmute(buffer.as_ptr()),
58+
std::mem::transmute(&bitmap_info),
59+
winapi::um::wingdi::DIB_RGB_COLORS,
60+
winapi::um::wingdi::SRCCOPY
61+
);
62+
63+
ValidateRect(self.window, std::ptr::null_mut());
64+
}
65+
}

0 commit comments

Comments
 (0)