1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
//! A responsive Windows startup window that does not depend on the game renderer.
use std::{cell::Cell, io, mem, ptr, sync::mpsc, thread};
use windows_sys::Win32::{
Foundation::{HWND, LPARAM, LRESULT, RECT, WPARAM},
Graphics::Gdi::*,
System::LibraryLoader::GetModuleHandleW,
UI::WindowsAndMessaging::*,
};
const SET_STAGE: u32 = WM_APP + 1;
const WIDTH: i32 = 510;
const HEIGHT: i32 = 182;
/// Keeps a startup window on its own message loop until the game can present.
pub struct StartupWindow {
handle: usize,
thread: Option<thread::JoinHandle<()>>,
}
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() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"no startup stages",
));
}
let subtitle = subtitle.encode_utf16().collect();
let stages = stages
.iter()
.map(|stage| stage.encode_utf16().collect())
.collect();
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,
);
})?;
match receiver.recv() {
Ok(Ok(handle)) => Ok(Self {
handle,
thread: Some(thread),
}),
result => {
let _ = thread.join();
Err(result
.ok()
.and_then(Result::err)
.unwrap_or_else(|| io::Error::other("startup window did not initialize")))
}
}
}
/// 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) };
}
}
impl Drop for StartupWindow {
fn drop(&mut self) {
// SAFETY: WM_CLOSE destroys the window on its owning thread. No buffer
// crosses the thread boundary.
unsafe { PostMessageW(self.handle as HWND, WM_CLOSE, 0, 0) };
if let Some(thread) = self.thread.take() {
let _ = thread.join();
}
}
}
struct State {
stage: Cell<usize>,
subtitle: Vec<u16>,
stages: Vec<Vec<u16>>,
}
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();
// SAFETY: The class and window belong to this thread. The creation strings
// stay alive through their calls, and `state` outlives all window messages.
unsafe {
let instance = GetModuleHandleW(ptr::null());
let class = WNDCLASSW {
lpfnWndProc: Some(window_proc),
hInstance: instance,
hCursor: LoadCursorW(ptr::null_mut(), IDC_ARROW),
lpszClassName: class_name.as_ptr(),
..mem::zeroed()
};
if RegisterClassW(&class) == 0 {
let _ = ready.send(Err(io::Error::last_os_error()));
return;
}
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,
ptr::null_mut(),
ptr::null_mut(),
instance,
(&state as *const State).cast(),
);
if window.is_null() {
let _ = ready.send(Err(io::Error::last_os_error()));
} else {
ShowWindow(window, SW_SHOW);
UpdateWindow(window);
if ready.send(Ok(window as usize)).is_ok() {
let mut message: MSG = mem::zeroed();
while GetMessageW(&mut message, ptr::null_mut(), 0, 0) > 0 {
TranslateMessage(&message);
DispatchMessageW(&message);
}
}
if IsWindow(window) != 0 {
DestroyWindow(window);
}
}
UnregisterClassW(class_name.as_ptr(), instance);
}
}
unsafe extern "system" fn window_proc(
window: HWND,
message: u32,
stage: WPARAM,
data: LPARAM,
) -> LRESULT {
// SAFETY: Win32 supplies CREATESTRUCTW for WM_NCCREATE. The userdata points
// to the thread-local State from `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 => {
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);
}
return 0;
}
}
WM_DESTROY => {
PostQuitMessage(0);
return 0;
}
_ => {}
}
DefWindowProcW(window, message, stage, 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.
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,
0,
1,
0,
0,
5,
0,
font_face.as_ptr(),
);
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);
}
}
|