aboutsummaryrefslogtreecommitdiff
path: root/adapters/fparkan-platform-winit/src
diff options
context:
space:
mode:
Diffstat (limited to 'adapters/fparkan-platform-winit/src')
-rw-r--r--adapters/fparkan-platform-winit/src/startup_window.rs224
1 files changed, 94 insertions, 130 deletions
diff --git a/adapters/fparkan-platform-winit/src/startup_window.rs b/adapters/fparkan-platform-winit/src/startup_window.rs
index 9e486f36..87feb984 100644
--- a/adapters/fparkan-platform-winit/src/startup_window.rs
+++ b/adapters/fparkan-platform-winit/src/startup_window.rs
@@ -1,6 +1,10 @@
//! A responsive Windows startup window that does not depend on the game renderer.
-use std::{cell::Cell, io, mem, ptr, sync::mpsc, thread};
+use std::{
+ io, mem, ptr,
+ sync::{mpsc, Arc, Mutex},
+ thread,
+};
use windows_sys::Win32::{
Foundation::{HWND, LPARAM, LRESULT, RECT, WPARAM},
Graphics::Gdi::*,
@@ -8,47 +12,44 @@ use windows_sys::Win32::{
UI::WindowsAndMessaging::*,
};
-const SET_STAGE: u32 = WM_APP + 1;
-const WIDTH: i32 = 510;
-const HEIGHT: i32 = 182;
+const SET_FRAME: u32 = WM_APP + 1;
-/// Keeps a startup window on its own message loop until the game can present.
+/// Keeps a startup bitmap visible until the game renderer can present.
pub struct StartupWindow {
handle: usize,
thread: Option<thread::JoinHandle<()>>,
+ state: Arc<State>,
}
impl StartupWindow {
- /// Opens the window at the first stage. Stage strings must describe real work.
- pub fn open(subtitle: &str, stages: &[&str]) -> io::Result<Self> {
- if stages.is_empty() {
+ /// Opens a window with the supplied top-down 0x00RRGGBB bitmap.
+ pub fn open(extent: [u32; 2], pixels: Vec<u32>) -> io::Result<Self> {
+ let [width, height] = extent;
+ if width == 0
+ || height == 0
+ || width > i32::MAX as u32
+ || height > i32::MAX as u32
+ || (width as usize).checked_mul(height as usize) != Some(pixels.len())
+ {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
- "no startup stages",
+ "invalid startup bitmap dimensions",
));
}
- let subtitle = subtitle.encode_utf16().collect();
- let stages = stages
- .iter()
- .map(|stage| stage.encode_utf16().collect())
- .collect();
+ let state = Arc::new(State {
+ extent: [width as i32, height as i32],
+ pixels: Mutex::new(pixels),
+ });
+ let window_state = Arc::clone(&state);
let (ready, receiver) = mpsc::sync_channel(0);
let thread = thread::Builder::new()
.name("startup-window".into())
- .spawn(move || {
- show(
- State {
- stage: Cell::new(0),
- subtitle,
- stages,
- },
- ready,
- );
- })?;
+ .spawn(move || show(&window_state, ready))?;
match receiver.recv() {
Ok(Ok(handle)) => Ok(Self {
handle,
thread: Some(thread),
+ state,
}),
result => {
let _ = thread.join();
@@ -60,18 +61,22 @@ impl StartupWindow {
}
}
- /// Displays the given zero-based stage.
- pub fn set_stage(&self, stage: usize) {
- // SAFETY: PostMessage accepts a window handle from another thread. The
- // message carries an integer only and is ignored if the window closed.
- unsafe { PostMessageW(self.handle as HWND, SET_STAGE, stage, 0) };
+ /// Replaces the current bitmap and schedules a repaint.
+ pub fn set_frame(&self, pixels: Vec<u32>) {
+ if let Ok(mut frame) = self.state.pixels.lock() {
+ if frame.len() != pixels.len() {
+ return;
+ }
+ *frame = pixels;
+ }
+ // SAFETY: The message carries no references and is ignored if the window closed.
+ unsafe { PostMessageW(self.handle as HWND, SET_FRAME, 0, 0) };
}
}
impl Drop for StartupWindow {
fn drop(&mut self) {
- // SAFETY: WM_CLOSE destroys the window on its owning thread. No buffer
- // crosses the thread boundary.
+ // SAFETY: WM_CLOSE destroys the window on its owning thread.
unsafe { PostMessageW(self.handle as HWND, WM_CLOSE, 0, 0) };
if let Some(thread) = self.thread.take() {
let _ = thread.join();
@@ -80,16 +85,15 @@ impl Drop for StartupWindow {
}
struct State {
- stage: Cell<usize>,
- subtitle: Vec<u16>,
- stages: Vec<Vec<u16>>,
+ extent: [i32; 2],
+ pixels: Mutex<Vec<u32>>,
}
-fn show(state: State, ready: mpsc::SyncSender<io::Result<usize>>) {
+fn show(state: &State, ready: mpsc::SyncSender<io::Result<usize>>) {
let class_name: Vec<_> = "FParkanStartup\0".encode_utf16().collect();
- let title: Vec<_> = "FParkan — загрузка миссии\0".encode_utf16().collect();
+ let title: Vec<_> = "FParkan mission\0".encode_utf16().collect();
// SAFETY: The class and window belong to this thread. The creation strings
- // stay alive through their calls, and `state` outlives all window messages.
+ // stay alive through their calls, and state outlives all window messages.
unsafe {
let instance = GetModuleHandleW(ptr::null());
let class = WNDCLASSW {
@@ -103,19 +107,30 @@ fn show(state: State, ready: mpsc::SyncSender<io::Result<usize>>) {
let _ = ready.send(Err(io::Error::last_os_error()));
return;
}
+ let style = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
+ let [width, height] = state.extent;
+ let mut outer = RECT {
+ left: 0,
+ top: 0,
+ right: width,
+ bottom: height,
+ };
+ AdjustWindowRectEx(&mut outer, style, 0, WS_EX_APPWINDOW);
+ let width = outer.right - outer.left;
+ let height = outer.bottom - outer.top;
let window = CreateWindowExW(
WS_EX_APPWINDOW,
class_name.as_ptr(),
title.as_ptr(),
- WS_POPUP | WS_BORDER,
- (GetSystemMetrics(SM_CXSCREEN) - WIDTH) / 2,
- (GetSystemMetrics(SM_CYSCREEN) - HEIGHT) / 2,
- WIDTH,
- HEIGHT,
+ style,
+ (GetSystemMetrics(SM_CXSCREEN) - width) / 2,
+ (GetSystemMetrics(SM_CYSCREEN) - height) / 2,
+ width,
+ height,
ptr::null_mut(),
ptr::null_mut(),
instance,
- (&state as *const State).cast(),
+ (state as *const State).cast(),
);
if window.is_null() {
let _ = ready.send(Err(io::Error::last_os_error()));
@@ -140,28 +155,26 @@ fn show(state: State, ready: mpsc::SyncSender<io::Result<usize>>) {
unsafe extern "system" fn window_proc(
window: HWND,
message: u32,
- stage: WPARAM,
+ wparam: WPARAM,
data: LPARAM,
) -> LRESULT {
// SAFETY: Win32 supplies CREATESTRUCTW for WM_NCCREATE. The userdata points
- // to the thread-local State from `show` until destruction.
+ // to the State kept alive by show until destruction.
unsafe {
match message {
WM_NCCREATE => {
let create = &*(data as *const CREATESTRUCTW);
SetWindowLongPtrW(window, GWLP_USERDATA, create.lpCreateParams as isize);
}
- WM_PAINT | SET_STAGE => {
+ SET_FRAME => {
+ InvalidateRect(window, ptr::null(), 0);
+ UpdateWindow(window);
+ return 0;
+ }
+ WM_PAINT => {
let state = GetWindowLongPtrW(window, GWLP_USERDATA) as *const State;
if let Some(state) = state.as_ref() {
- if message == SET_STAGE {
- if stage < state.stages.len() && stage > state.stage.get() {
- state.stage.set(stage);
- InvalidateRect(window, ptr::null(), 0);
- }
- } else {
- draw(window, state);
- }
+ draw(window, state);
return 0;
}
}
@@ -171,95 +184,46 @@ unsafe extern "system" fn window_proc(
}
_ => {}
}
- DefWindowProcW(window, message, stage, data)
+ DefWindowProcW(window, message, wparam, data)
}
}
fn draw(window: HWND, state: &State) {
- // SAFETY: Called on WM_PAINT for this thread's window. Every brush and font
- // is deleted after drawing, and BeginPaint/EndPaint delimit the borrowed DC.
+ // SAFETY: The paint context is borrowed until EndPaint. The bitmap is
+ // locked while GDI reads its validated width × height 32-bit pixels.
unsafe {
let mut paint: PAINTSTRUCT = mem::zeroed();
let dc = BeginPaint(window, &mut paint);
if !dc.is_null() {
- let area = RECT {
- left: 0,
- top: 0,
- right: WIDTH,
- bottom: HEIGHT,
- };
- let fill = |rect: &RECT, color| {
- let brush = CreateSolidBrush(color);
- if !brush.is_null() {
- FillRect(dc, rect, brush);
- DeleteObject(brush);
- }
- };
- fill(&area, 0x211a13);
- SetBkMode(dc, TRANSPARENT as i32);
- let font_face: Vec<_> = "Segoe UI\0".encode_utf16().collect();
- let line = |text: &[u16], y, size: i32, weight, color| {
- let font = CreateFontW(
- -size,
- 0,
- 0,
- 0,
- weight,
- 0,
- 0,
+ if let Ok(frame) = state.pixels.lock() {
+ let [width, height] = state.extent;
+ let mut info: BITMAPINFO = mem::zeroed();
+ info.bmiHeader = BITMAPINFOHEADER {
+ biSize: mem::size_of::<BITMAPINFOHEADER>() as u32,
+ biWidth: width,
+ biHeight: -height,
+ biPlanes: 1,
+ biBitCount: 32,
+ biCompression: BI_RGB,
+ ..mem::zeroed()
+ };
+ let mut area: RECT = mem::zeroed();
+ GetClientRect(window, &mut area);
+ StretchDIBits(
+ dc,
0,
- 1,
0,
+ area.right,
+ area.bottom,
0,
- 5,
0,
- font_face.as_ptr(),
+ width,
+ height,
+ frame.as_ptr().cast(),
+ &info,
+ DIB_RGB_COLORS,
+ SRCCOPY,
);
- let previous = if font.is_null() {
- ptr::null_mut()
- } else {
- SelectObject(dc, font)
- };
- SetTextColor(dc, color);
- let mut rect = RECT {
- left: 30,
- top: y,
- right: WIDTH - 30,
- bottom: y + size + 12,
- };
- DrawTextW(
- dc,
- text.as_ptr(),
- text.len() as i32,
- &mut rect,
- DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX,
- );
- if !font.is_null() {
- if !previous.is_null() {
- SelectObject(dc, previous);
- }
- DeleteObject(font);
- }
- };
- let title: Vec<_> = "Загрузка миссии".encode_utf16().collect();
- line(&title, 26, 27, 600, 0xf1ede5);
- line(&state.subtitle, 64, 14, 400, 0xbeb3a6);
- line(&state.stages[state.stage.get()], 109, 17, 400, 0xf1ede5);
- let count = state.stages.len() as i32;
- let span = WIDTH - 60;
- for index in 0..count {
- let color = match (index as usize).cmp(&state.stage.get()) {
- std::cmp::Ordering::Less => 0x8e9684,
- std::cmp::Ordering::Equal => 0x59bce3,
- std::cmp::Ordering::Greater => 0x493c30,
- };
- let rect = RECT {
- left: 30 + span * index / count,
- top: 145,
- right: 30 + span * (index + 1) / count - 4,
- bottom: 149,
- };
- fill(&rect, color);
}
}
EndPaint(window, &paint);