OpenTTD Source 20260218-master-g2123fca5ea
win32_v.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "../openttd.h"
12#include "../error_func.h"
13#include "../gfx_func.h"
14#include "../os/windows/win32.h"
17#include "../core/math_func.hpp"
19#include "../texteff.hpp"
20#include "../thread.h"
21#include "../progress.h"
22#include "../window_gui.h"
23#include "../window_func.h"
24#include "../framerate_type.h"
25#include "../library_loader.h"
26#include "../core/utf8.hpp"
27#include "win32_v.h"
28#include <windows.h>
29#include <imm.h>
30#include <versionhelpers.h>
31#if defined(_MSC_VER) && defined(NTDDI_WIN10_RS4)
32#include <winrt/Windows.UI.ViewManagement.h>
33#endif
34
35#ifdef WITH_OPENGL
36#include <GL/gl.h>
37#include "../3rdparty/opengl/glext.h"
38#include "../3rdparty/opengl/wglext.h"
39#include "opengl.h"
40#endif /* WITH_OPENGL */
41
42#include "../safeguards.h"
43
44/* Missing define in MinGW headers. */
45#ifndef MAPVK_VK_TO_CHAR
46#define MAPVK_VK_TO_CHAR (2)
47#endif
48
49#ifndef PM_QS_INPUT
50#define PM_QS_INPUT 0x20000
51#endif
52
53#ifndef WM_DPICHANGED
54#define WM_DPICHANGED 0x02E0
55#endif
56
57bool _window_maximize;
58static Dimension _bck_resolution;
59DWORD _imm_props;
60
61static Palette _local_palette;
62
64{
65 MyShowCursor(false, true);
66}
67
69 uint8_t vk_from;
70 uint8_t vk_count;
71 uint8_t map_to;
72};
73
74#define AS(x, z) {x, 1, z}
75#define AM(x, y, z, w) {x, y - x + 1, z}
76
77static const Win32VkMapping _vk_mapping[] = {
78 /* Pageup stuff + up/down */
79 AM(VK_PRIOR, VK_DOWN, WKC_PAGEUP, WKC_DOWN),
80 /* Map letters & digits */
81 AM('A', 'Z', 'A', 'Z'),
82 AM('0', '9', '0', '9'),
83
84 AS(VK_ESCAPE, WKC_ESC),
85 AS(VK_PAUSE, WKC_PAUSE),
86 AS(VK_BACK, WKC_BACKSPACE),
87 AM(VK_INSERT, VK_DELETE, WKC_INSERT, WKC_DELETE),
88
89 AS(VK_SPACE, WKC_SPACE),
90 AS(VK_RETURN, WKC_RETURN),
91 AS(VK_TAB, WKC_TAB),
92
93 /* Function keys */
94 AM(VK_F1, VK_F12, WKC_F1, WKC_F12),
95
96 /* Numeric part */
97 AM(VK_NUMPAD0, VK_NUMPAD9, '0', '9'),
98 AS(VK_DIVIDE, WKC_NUM_DIV),
99 AS(VK_MULTIPLY, WKC_NUM_MUL),
100 AS(VK_SUBTRACT, WKC_NUM_MINUS),
101 AS(VK_ADD, WKC_NUM_PLUS),
102 AS(VK_DECIMAL, WKC_NUM_DECIMAL),
103
104 /* Other non-letter keys */
105 AS(0xBF, WKC_SLASH),
106 AS(0xBA, WKC_SEMICOLON),
107 AS(0xBB, WKC_EQUALS),
108 AS(0xDB, WKC_L_BRACKET),
109 AS(0xDC, WKC_BACKSLASH),
110 AS(0xDD, WKC_R_BRACKET),
111
112 AS(0xDE, WKC_SINGLEQUOTE),
113 AS(0xBC, WKC_COMMA),
114 AS(0xBD, WKC_MINUS),
115 AS(0xBE, WKC_PERIOD)
116};
117
118static uint MapWindowsKey(uint sym)
119{
120 uint key = 0;
121
122 for (const auto &map : _vk_mapping) {
123 if (IsInsideBS(sym, map.vk_from, map.vk_count)) {
124 key = sym - map.vk_from + map.map_to;
125 break;
126 }
127 }
128
129 if (GetAsyncKeyState(VK_SHIFT) < 0) key |= WKC_SHIFT;
130 if (GetAsyncKeyState(VK_CONTROL) < 0) key |= WKC_CTRL;
131 if (GetAsyncKeyState(VK_MENU) < 0) key |= WKC_ALT;
132 return key;
133}
134
140{
141 /* Check modes for the relevant fullscreen bpp */
142 return _support8bpp != S8BPP_HARDWARE ? 32 : BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
143}
144
151bool VideoDriver_Win32Base::MakeWindow(bool full_screen, bool resize)
152{
153 /* full_screen is whether the new window should be fullscreen,
154 * _wnd.fullscreen is whether the current window is. */
155 _fullscreen = full_screen;
156
157 /* recreate window? */
158 if ((full_screen != this->fullscreen) && this->main_wnd) {
159 DestroyWindow(this->main_wnd);
160 this->main_wnd = 0;
161 }
162
163 if (full_screen) {
164 DEVMODE settings{};
165 settings.dmSize = sizeof(settings);
166 settings.dmFields =
167 DM_BITSPERPEL |
168 DM_PELSWIDTH |
169 DM_PELSHEIGHT;
170 settings.dmBitsPerPel = this->GetFullscreenBpp();
171 settings.dmPelsWidth = this->width_org;
172 settings.dmPelsHeight = this->height_org;
173
174 /* Check for 8 bpp support. */
175 if (settings.dmBitsPerPel == 8 && ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
176 settings.dmBitsPerPel = 32;
177 }
178
179 /* Test fullscreen with current resolution, if it fails use desktop resolution. */
180 if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
181 RECT r;
182 GetWindowRect(GetDesktopWindow(), &r);
183 /* Guard against recursion. If we already failed here once, just fall through to
184 * the next ChangeDisplaySettings call which will fail and error out appropriately. */
185 if ((int)settings.dmPelsWidth != r.right - r.left || (int)settings.dmPelsHeight != r.bottom - r.top) {
186 return this->ChangeResolution(r.right - r.left, r.bottom - r.top);
187 }
188 }
189
190 if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
191 this->MakeWindow(false, resize); // don't care about the result
192 return false; // the request failed
193 }
194 } else if (this->fullscreen) {
195 /* restore display? */
196 ChangeDisplaySettings(nullptr, 0);
197 /* restore the resolution */
198 this->width = _bck_resolution.width;
199 this->height = _bck_resolution.height;
200 }
201
202 {
203 RECT r;
204 DWORD style, showstyle;
205 int w, h;
206
207 showstyle = SW_SHOWNORMAL;
208 this->fullscreen = full_screen;
209 if (this->fullscreen) {
210 style = WS_POPUP;
211 SetRect(&r, 0, 0, this->width_org, this->height_org);
212 } else {
213 style = WS_OVERLAPPEDWINDOW;
214 /* On window creation, check if we were in maximize mode before */
215 if (_window_maximize) showstyle = SW_SHOWMAXIMIZED;
216 SetRect(&r, 0, 0, this->width, this->height);
217 }
218
219 AdjustWindowRect(&r, style, FALSE);
220 w = r.right - r.left;
221 h = r.bottom - r.top;
222
223 if (this->main_wnd != nullptr) {
224 if (!_window_maximize && resize) SetWindowPos(this->main_wnd, 0, 0, 0, w, h, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
225 } else {
226 int x = 0;
227 int y = 0;
228
229 /* For windowed mode, center on the workspace of the primary display. */
230 if (!this->fullscreen) {
231 MONITORINFO mi;
232 mi.cbSize = sizeof(mi);
233 GetMonitorInfo(MonitorFromWindow(0, MONITOR_DEFAULTTOPRIMARY), &mi);
234
235 x = (mi.rcWork.right - mi.rcWork.left - w) / 2;
236 y = (mi.rcWork.bottom - mi.rcWork.top - h) / 2;
237 }
238
239 std::string caption = VideoDriver::GetCaption();
240 this->main_wnd = CreateWindow(L"OTTD", OTTD2FS(caption).c_str(), style, x, y, w, h, 0, 0, GetModuleHandle(nullptr), this);
241 if (this->main_wnd == nullptr) UserError("CreateWindow failed");
242 ShowWindow(this->main_wnd, showstyle);
243 }
244 }
245
247
249 return true;
250}
251
253static LRESULT HandleCharMsg(uint keycode, char32_t charcode)
254{
255 static char32_t prev_char = 0;
256
257 /* Did we get a lead surrogate? If yes, store and exit. */
258 if (Utf16IsLeadSurrogate(charcode)) {
259 if (prev_char != 0) Debug(driver, 1, "Got two UTF-16 lead surrogates, dropping the first one");
260 prev_char = charcode;
261 return 0;
262 }
263
264 /* Stored lead surrogate and incoming trail surrogate? Combine and forward to input handling. */
265 if (prev_char != 0) {
266 if (Utf16IsTrailSurrogate(charcode)) {
267 charcode = Utf16DecodeSurrogate(prev_char, charcode);
268 } else {
269 Debug(driver, 1, "Got an UTF-16 lead surrogate without a trail surrogate, dropping the lead surrogate");
270 }
271 }
272 prev_char = 0;
273
274 HandleKeypress(keycode, charcode);
275
276 return 0;
277}
278
281{
282 return (_imm_props & IME_PROP_AT_CARET) && !(_imm_props & IME_PROP_SPECIAL_UI);
283}
284
286static void SetCompositionPos(HWND hwnd)
287{
288 HIMC hIMC = ImmGetContext(hwnd);
289 if (hIMC != nullptr) {
290 COMPOSITIONFORM cf;
291 cf.dwStyle = CFS_POINT;
292
293 if (EditBoxInGlobalFocus()) {
294 /* Get caret position. */
295 Point pt = _focused_window->GetCaretPosition();
296 cf.ptCurrentPos.x = _focused_window->left + pt.x;
297 cf.ptCurrentPos.y = _focused_window->top + pt.y;
298 } else {
299 cf.ptCurrentPos.x = 0;
300 cf.ptCurrentPos.y = 0;
301 }
302 ImmSetCompositionWindow(hIMC, &cf);
303 }
304 ImmReleaseContext(hwnd, hIMC);
305}
306
308static void SetCandidatePos(HWND hwnd)
309{
310 HIMC hIMC = ImmGetContext(hwnd);
311 if (hIMC != nullptr) {
312 CANDIDATEFORM cf;
313 cf.dwIndex = 0;
314 cf.dwStyle = CFS_EXCLUDE;
315
316 if (EditBoxInGlobalFocus()) {
317 Point pt = _focused_window->GetCaretPosition();
318 cf.ptCurrentPos.x = _focused_window->left + pt.x;
319 cf.ptCurrentPos.y = _focused_window->top + pt.y;
320 if (_focused_window->window_class == WC_CONSOLE) {
321 cf.rcArea.left = _focused_window->left;
322 cf.rcArea.top = _focused_window->top;
323 cf.rcArea.right = _focused_window->left + _focused_window->width;
324 cf.rcArea.bottom = _focused_window->top + _focused_window->height;
325 } else {
326 cf.rcArea.left = _focused_window->left + _focused_window->nested_focus->pos_x;
327 cf.rcArea.top = _focused_window->top + _focused_window->nested_focus->pos_y;
328 cf.rcArea.right = cf.rcArea.left + _focused_window->nested_focus->current_x;
329 cf.rcArea.bottom = cf.rcArea.top + _focused_window->nested_focus->current_y;
330 }
331 } else {
332 cf.ptCurrentPos.x = 0;
333 cf.ptCurrentPos.y = 0;
334 SetRectEmpty(&cf.rcArea);
335 }
336 ImmSetCandidateWindow(hIMC, &cf);
337 }
338 ImmReleaseContext(hwnd, hIMC);
339}
340
342static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
343{
344 HIMC hIMC = ImmGetContext(hwnd);
345
346 if (hIMC != nullptr) {
347 if (lParam & GCS_RESULTSTR) {
348 /* Read result string from the IME. */
349 LONG len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, nullptr, 0); // Length is always in bytes, even in UNICODE build.
350 std::wstring str(len + 1, L'\0');
351 len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, str.data(), len);
352 str[len / sizeof(wchar_t)] = L'\0';
353
354 /* Transmit text to windowing system. */
355 if (len > 0) {
356 HandleTextInput({}, true); // Clear marked string.
358 }
359 SetCompositionPos(hwnd);
360
361 /* Don't pass the result string on to the default window proc. */
362 lParam &= ~(GCS_RESULTSTR | GCS_RESULTCLAUSE | GCS_RESULTREADCLAUSE | GCS_RESULTREADSTR);
363 }
364
365 if ((lParam & GCS_COMPSTR) && DrawIMECompositionString()) {
366 /* Read composition string from the IME. */
367 LONG len = ImmGetCompositionString(hIMC, GCS_COMPSTR, nullptr, 0); // Length is always in bytes, even in UNICODE build.
368 std::wstring str(len + 1, L'\0');
369 len = ImmGetCompositionString(hIMC, GCS_COMPSTR, str.data(), len);
370 str[len / sizeof(wchar_t)] = L'\0';
371
372 if (len > 0) {
373 static char utf8_buf[1024];
374 convert_from_fs(str, utf8_buf);
375
376 /* Convert caret position from bytes in the input string to a position in the UTF-8 encoded string. */
377 LONG caret_bytes = ImmGetCompositionString(hIMC, GCS_CURSORPOS, nullptr, 0);
378 Utf8View view(utf8_buf);
379 auto caret = view.begin();
380 const auto end = view.end();
381 for (const wchar_t *c = str.c_str(); *c != '\0' && caret != end && caret_bytes > 0; c++, caret_bytes--) {
382 /* Skip DBCS lead bytes or leading surrogates. */
383 if (Utf16IsLeadSurrogate(*c)) {
384 c++;
385 caret_bytes--;
386 }
387 ++caret;
388 }
389
390 HandleTextInput(utf8_buf, true, caret.GetByteOffset());
391 } else {
392 HandleTextInput({}, true);
393 }
394
395 lParam &= ~(GCS_COMPSTR | GCS_COMPATTR | GCS_COMPCLAUSE | GCS_CURSORPOS | GCS_DELTASTART);
396 }
397 }
398 ImmReleaseContext(hwnd, hIMC);
399
400 return lParam != 0 ? DefWindowProc(hwnd, WM_IME_COMPOSITION, wParam, lParam) : 0;
401}
402
404static void CancelIMEComposition(HWND hwnd)
405{
406 HIMC hIMC = ImmGetContext(hwnd);
407 if (hIMC != nullptr) ImmNotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
408 ImmReleaseContext(hwnd, hIMC);
409 /* Clear any marked string from the current edit box. */
410 HandleTextInput({}, true);
411}
412
413#if defined(_MSC_VER) && defined(NTDDI_WIN10_RS4)
414/* We only use WinRT functions on Windows 10 or later. Unfortunately, newer Windows SDKs are now
415 * linking the two functions below directly instead of using dynamic linking as previously.
416 * To avoid any runtime linking errors on Windows 7 or older, we stub in our own dynamic
417 * linking trampoline. */
418
419static LibraryLoader _combase("combase.dll");
420
421extern "C" int32_t __stdcall WINRT_IMPL_RoOriginateLanguageException(int32_t error, void *message, void *languageException) noexcept
422{
423 typedef BOOL(WINAPI *PFNRoOriginateLanguageException)(int32_t, void *, void *);
424 static PFNRoOriginateLanguageException RoOriginateLanguageException = _combase.GetFunction("RoOriginateLanguageException");
425
426 if (RoOriginateLanguageException != nullptr) {
427 return RoOriginateLanguageException(error, message, languageException);
428 } else {
429 return TRUE;
430 }
431}
432
433extern "C" int32_t __stdcall WINRT_IMPL_RoGetActivationFactory(void *classId, winrt::guid const &iid, void **factory) noexcept
434{
435 typedef BOOL(WINAPI *PFNRoGetActivationFactory)(void *, winrt::guid const &, void **);
436 static PFNRoGetActivationFactory RoGetActivationFactory = _combase.GetFunction("RoGetActivationFactory");
437
438 if (RoGetActivationFactory != nullptr) {
439 return RoGetActivationFactory(classId, iid, factory);
440 } else {
441 *factory = nullptr;
442 return winrt::impl::error_class_not_available;
443 }
444}
445#endif
446
447static bool IsDarkModeEnabled()
448{
449 /* Only build if SDK is Windows 10 1803 or later. */
450#if defined(_MSC_VER) && defined(NTDDI_WIN10_RS4)
451 if (IsWindows10OrGreater()) {
452 try {
453 /*
454 * The official documented way to find out if the system is running in dark mode is to
455 * check the brightness of the current theme's colour.
456 * See: https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/ui/apply-windows-themes#know-when-dark-mode-is-enabled
457 *
458 * There are other variants floating around on the Internet, but they all rely on internal,
459 * undocumented Windows functions that may or may not work in the future.
460 */
461 winrt::Windows::UI::ViewManagement::UISettings settings;
462 auto foreground = settings.GetColorValue(winrt::Windows::UI::ViewManagement::UIColorType::Foreground);
463
464 /* If the Foreground colour is a light colour, the system is running in dark mode. */
465 return ((5 * foreground.G) + (2 * foreground.R) + foreground.B) > (8 * 128);
466 } catch (...) {
467 /* Some kind of error, like a too old Windows version. Just return false. */
468 return false;
469 }
470 }
471#endif /* defined(_MSC_VER) && defined(NTDDI_WIN10_RS4) */
472
473 return false;
474}
475
476static void SetDarkModeForWindow(HWND hWnd, bool dark_mode)
477{
478 /* Only build if SDK is Windows 10+. */
479#if defined(NTDDI_WIN10)
480 if (!IsWindows10OrGreater()) return;
481
482 /* This function is documented, but not supported on all Windows 10/11 SDK builds. For this
483 * reason, the code uses dynamic loading and ignores any errors for a best-effort result. */
484 static LibraryLoader _dwmapi("dwmapi.dll");
485 typedef HRESULT(WINAPI *PFNDWMSETWINDOWATTRIBUTE)(HWND, DWORD, LPCVOID, DWORD);
486 static const PFNDWMSETWINDOWATTRIBUTE DwmSetWindowAttribute = _dwmapi.GetFunction("DwmSetWindowAttribute");
487
488 if (DwmSetWindowAttribute != nullptr) {
489 /* Contrary to the published documentation, DWMWA_USE_IMMERSIVE_DARK_MODE does not change the
490 * window chrome according to the current theme, but forces it to either light or dark mode.
491 * As such, the set value has to depend on the current theming mode.*/
492 BOOL value = dark_mode ? TRUE : FALSE;
493 if (DwmSetWindowAttribute(hWnd, 20 /* DWMWA_USE_IMMERSIVE_DARK_MODE */, &value, sizeof(value)) != S_OK) {
494 DwmSetWindowAttribute(hWnd, 19 /* DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 */, &value, sizeof(value)); // Ignore errors. It works or it doesn't.
495 }
496 }
497#endif /* defined(NTDDI_WIN10) */
498}
499
500LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
501{
502 static uint32_t keycode = 0;
503 static bool console = false;
504
505 const float SCROLL_BUILTIN_MULTIPLIER = 14.0f / WHEEL_DELTA;
506
507 VideoDriver_Win32Base *video_driver = (VideoDriver_Win32Base *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
508
509 switch (msg) {
510 case WM_CREATE:
511 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)((LPCREATESTRUCT)lParam)->lpCreateParams);
512 _cursor.in_window = false; // Win32 has mouse tracking.
513 SetCompositionPos(hwnd);
514 _imm_props = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
515
516 /* Enable dark mode theming for window chrome. */
517 SetDarkModeForWindow(hwnd, IsDarkModeEnabled());
518 break;
519
520 case WM_SETTINGCHANGE:
521 /* Synchronize dark mode theming state. */
522 SetDarkModeForWindow(hwnd, IsDarkModeEnabled());
523 break;
524
525 case WM_PAINT: {
526 RECT r;
527 GetUpdateRect(hwnd, &r, FALSE);
528 video_driver->MakeDirty(r.left, r.top, r.right - r.left, r.bottom - r.top);
529
530 ValidateRect(hwnd, nullptr);
531 return 0;
532 }
533
534 case WM_PALETTECHANGED:
535 if ((HWND)wParam == hwnd) return 0;
536 [[fallthrough]];
537
538 case WM_QUERYNEWPALETTE:
539 video_driver->PaletteChanged(hwnd);
540 return 0;
541
542 case WM_CLOSE:
543 HandleExitGameRequest();
544 return 0;
545
546 case WM_DESTROY:
547 if (_window_maximize) _cur_resolution = _bck_resolution;
548 return 0;
549
550 case WM_LBUTTONDOWN:
551 SetCapture(hwnd);
552 _left_button_down = true;
554 return 0;
555
556 case WM_LBUTTONUP:
557 ReleaseCapture();
558 _left_button_down = false;
559 _left_button_clicked = false;
561 return 0;
562
563 case WM_RBUTTONDOWN:
564 SetCapture(hwnd);
565 _right_button_down = true;
568 return 0;
569
570 case WM_RBUTTONUP:
571 ReleaseCapture();
572 _right_button_down = false;
574 return 0;
575
576 case WM_MOUSELEAVE:
577 UndrawMouseCursor();
578 _cursor.in_window = false;
579
580 if (!_left_button_down && !_right_button_down) MyShowCursor(true);
581 return 0;
582
583 case WM_MOUSEMOVE: {
584 int x = (int16_t)LOWORD(lParam);
585 int y = (int16_t)HIWORD(lParam);
586
587 /* If the mouse was not in the window and it has moved it means it has
588 * come into the window, so start drawing the mouse. Also start
589 * tracking the mouse for exiting the window */
590 if (!_cursor.in_window) {
591 _cursor.in_window = true;
592 TRACKMOUSEEVENT tme;
593 tme.cbSize = sizeof(tme);
594 tme.dwFlags = TME_LEAVE;
595 tme.hwndTrack = hwnd;
596
597 TrackMouseEvent(&tme);
598 }
599
600 if (_cursor.fix_at) {
601 /* Get all queued mouse events now in case we have to warp the cursor. In the
602 * end, we only care about the current mouse position and not bygone events. */
603 MSG m;
604 while (PeekMessage(&m, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE | PM_NOYIELD | PM_QS_INPUT)) {
605 x = (int16_t)LOWORD(m.lParam);
606 y = (int16_t)HIWORD(m.lParam);
607 }
608 }
609
610 if (_cursor.UpdateCursorPosition(x, y)) {
611 POINT pt;
612 pt.x = _cursor.pos.x;
613 pt.y = _cursor.pos.y;
614 ClientToScreen(hwnd, &pt);
615 SetCursorPos(pt.x, pt.y);
616 }
617 MyShowCursor(false);
619 return 0;
620 }
621
622 case WM_INPUTLANGCHANGE:
623 _imm_props = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
624 break;
625
626 case WM_IME_SETCONTEXT:
627 /* Don't show the composition window if we draw the string ourself. */
628 if (DrawIMECompositionString()) lParam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
629 break;
630
631 case WM_IME_STARTCOMPOSITION:
632 SetCompositionPos(hwnd);
633 if (DrawIMECompositionString()) return 0;
634 break;
635
636 case WM_IME_COMPOSITION:
637 return HandleIMEComposition(hwnd, wParam, lParam);
638
639 case WM_IME_ENDCOMPOSITION:
640 /* Clear any pending composition string. */
641 HandleTextInput({}, true);
642 if (DrawIMECompositionString()) return 0;
643 break;
644
645 case WM_IME_NOTIFY:
646 if (wParam == IMN_OPENCANDIDATE) SetCandidatePos(hwnd);
647 break;
648
649 case WM_DEADCHAR:
650 console = GB(lParam, 16, 8) == 41;
651 return 0;
652
653 case WM_CHAR: {
654 uint scancode = GB(lParam, 16, 8);
655 uint charcode = wParam;
656
657 /* If the console key is a dead-key, we need to press it twice to get a WM_CHAR message.
658 * But we then get two WM_CHAR messages, so ignore the first one */
659 if (console && scancode == 41) {
660 console = false;
661 return 0;
662 }
663
664 /* IMEs and other input methods sometimes send a WM_CHAR without a WM_KEYDOWN,
665 * clear the keycode so a previous WM_KEYDOWN doesn't become 'stuck'. */
666 uint cur_keycode = keycode;
667 keycode = 0;
668
669 return HandleCharMsg(cur_keycode, charcode);
670 }
671
672 case WM_KEYDOWN: {
673 /* No matter the keyboard layout, we will map the '~' to the console. */
674 uint scancode = GB(lParam, 16, 8);
675 keycode = scancode == 41 ? (uint)WKC_BACKQUOTE : MapWindowsKey(wParam);
676
677 uint charcode = MapVirtualKey(wParam, MAPVK_VK_TO_CHAR);
678
679 /* No character translation? */
680 if (charcode == 0) {
681 HandleKeypress(keycode, 0);
682 return 0;
683 }
684
685 /* If an edit box is in focus, wait for the corresponding WM_CHAR message. */
686 if (!EditBoxInGlobalFocus()) {
687 /* Is the console key a dead key? If yes, ignore the first key down event. */
688 if (HasBit(charcode, 31) && !console) {
689 if (scancode == 41) {
690 console = true;
691 return 0;
692 }
693 }
694 console = false;
695
696 /* IMEs and other input methods sometimes send a WM_CHAR without a WM_KEYDOWN,
697 * clear the keycode so a previous WM_KEYDOWN doesn't become 'stuck'. */
698 uint cur_keycode = keycode;
699 keycode = 0;
700
701 return HandleCharMsg(cur_keycode, LOWORD(charcode));
702 }
703
704 return 0;
705 }
706
707 case WM_SYSKEYDOWN: // user presses F10 or Alt, both activating the title-menu
708 switch (wParam) {
709 case VK_RETURN:
710 case 'F': // Full Screen on ALT + ENTER/F
711 ToggleFullScreen(!video_driver->fullscreen);
712 return 0;
713
714 case VK_MENU: // Just ALT
715 return 0; // do nothing
716
717 case VK_F10: // F10, ignore activation of menu
718 HandleKeypress(MapWindowsKey(wParam), 0);
719 return 0;
720
721 default: // ALT in combination with something else
722 HandleKeypress(MapWindowsKey(wParam), 0);
723 break;
724 }
725 break;
726
727 case WM_SIZE:
728 if (wParam != SIZE_MINIMIZED) {
729 /* Set maximized flag when we maximize (obviously), but also when we
730 * switched to fullscreen from a maximized state */
731 _window_maximize = (wParam == SIZE_MAXIMIZED || (_window_maximize && _fullscreen));
732 if (_window_maximize || _fullscreen) _bck_resolution = _cur_resolution;
733 video_driver->ClientSizeChanged(LOWORD(lParam), HIWORD(lParam));
734 }
735 return 0;
736
737 case WM_SIZING: {
738 RECT *r = (RECT*)lParam;
739 RECT r2;
740 int w, h;
741
742 SetRect(&r2, 0, 0, 0, 0);
743 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
744
745 w = r->right - r->left - (r2.right - r2.left);
746 h = r->bottom - r->top - (r2.bottom - r2.top);
747 w = std::max(w, 64);
748 h = std::max(h, 64);
749 SetRect(&r2, 0, 0, w, h);
750
751 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
752 w = r2.right - r2.left;
753 h = r2.bottom - r2.top;
754
755 switch (wParam) {
756 case WMSZ_BOTTOM:
757 r->bottom = r->top + h;
758 break;
759
760 case WMSZ_BOTTOMLEFT:
761 r->bottom = r->top + h;
762 r->left = r->right - w;
763 break;
764
765 case WMSZ_BOTTOMRIGHT:
766 r->bottom = r->top + h;
767 r->right = r->left + w;
768 break;
769
770 case WMSZ_LEFT:
771 r->left = r->right - w;
772 break;
773
774 case WMSZ_RIGHT:
775 r->right = r->left + w;
776 break;
777
778 case WMSZ_TOP:
779 r->top = r->bottom - h;
780 break;
781
782 case WMSZ_TOPLEFT:
783 r->top = r->bottom - h;
784 r->left = r->right - w;
785 break;
786
787 case WMSZ_TOPRIGHT:
788 r->top = r->bottom - h;
789 r->right = r->left + w;
790 break;
791 }
792 return TRUE;
793 }
794
795 case WM_DPICHANGED: {
796 auto did_adjust = AdjustGUIZoom(true);
797
798 /* Resize the window to match the new DPI setting. */
799 RECT *prcNewWindow = (RECT *)lParam;
800 SetWindowPos(hwnd,
801 nullptr,
802 prcNewWindow->left,
803 prcNewWindow->top,
804 prcNewWindow->right - prcNewWindow->left,
805 prcNewWindow->bottom - prcNewWindow->top,
806 SWP_NOZORDER | SWP_NOACTIVATE);
807
808 if (did_adjust) ReInitAllWindows(true);
809
810 return 0;
811 }
812
813/* needed for wheel */
814#if !defined(WM_MOUSEWHEEL)
815# define WM_MOUSEWHEEL 0x020A
816#endif /* WM_MOUSEWHEEL */
817#if !defined(WM_MOUSEHWHEEL)
818# define WM_MOUSEHWHEEL 0x020E
819#endif /* WM_MOUSEHWHEEL */
820#if !defined(GET_WHEEL_DELTA_WPARAM)
821# define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam))
822#endif /* GET_WHEEL_DELTA_WPARAM */
823
824 case WM_MOUSEWHEEL: {
825 int delta = GET_WHEEL_DELTA_WPARAM(wParam);
826
827 if (delta < 0) {
828 _cursor.wheel++;
829 } else if (delta > 0) {
830 _cursor.wheel--;
831 }
832
833 _cursor.v_wheel -= static_cast<float>(delta) * SCROLL_BUILTIN_MULTIPLIER * _settings_client.gui.scrollwheel_multiplier;
834 _cursor.wheel_moved = true;
836 return 0;
837 }
838
839 case WM_MOUSEHWHEEL: {
840 int delta = GET_WHEEL_DELTA_WPARAM(wParam);
841
842 _cursor.h_wheel += static_cast<float>(delta) * SCROLL_BUILTIN_MULTIPLIER * _settings_client.gui.scrollwheel_multiplier;
843 _cursor.wheel_moved = true;
845 return 0;
846 }
847
848 case WM_SETFOCUS:
849 video_driver->has_focus = true;
850 SetCompositionPos(hwnd);
851 break;
852
853 case WM_KILLFOCUS:
854 video_driver->has_focus = false;
855 break;
856
857 case WM_ACTIVATE: {
858 /* Don't do anything if we are closing openttd */
859 if (_exit_game) break;
860
861 bool active = (LOWORD(wParam) != WA_INACTIVE);
862 bool minimized = (HIWORD(wParam) != 0);
863 if (video_driver->fullscreen) {
864 if (active && minimized) {
865 /* Restore the game window */
866 Dimension d = _bck_resolution; // Save current non-fullscreen window size as it will be overwritten by ShowWindow.
867 ShowWindow(hwnd, SW_RESTORE);
868 _bck_resolution = d;
869 video_driver->MakeWindow(true);
870 } else if (!active && !minimized) {
871 /* Minimise the window and restore desktop */
872 ShowWindow(hwnd, SW_MINIMIZE);
873 ChangeDisplaySettings(nullptr, 0);
874 }
875 }
876 break;
877 }
878 }
879
880 return DefWindowProc(hwnd, msg, wParam, lParam);
881}
882
883static void RegisterWndClass()
884{
885 static bool registered = false;
886
887 if (registered) return;
888
889 HINSTANCE hinst = GetModuleHandle(nullptr);
890 WNDCLASS wnd = {
891 CS_OWNDC,
892 WndProcGdi,
893 0,
894 0,
895 hinst,
896 LoadIcon(hinst, MAKEINTRESOURCE(100)),
897 LoadCursor(nullptr, IDC_ARROW),
898 0,
899 0,
900 L"OTTD"
901 };
902
903 registered = true;
904 if (!RegisterClass(&wnd)) UserError("RegisterClass failed");
905}
906
907static const Dimension default_resolutions[] = {
908 { 640, 480 },
909 { 800, 600 },
910 { 1024, 768 },
911 { 1152, 864 },
912 { 1280, 800 },
913 { 1280, 960 },
914 { 1280, 1024 },
915 { 1400, 1050 },
916 { 1600, 1200 },
917 { 1680, 1050 },
918 { 1920, 1200 }
919};
920
921static void FindResolutions(uint8_t bpp)
922{
923 _resolutions.clear();
924
925 DEVMODE dm;
926 for (uint i = 0; EnumDisplaySettings(nullptr, i, &dm) != 0; i++) {
927 if (dm.dmBitsPerPel != bpp || dm.dmPelsWidth < 640 || dm.dmPelsHeight < 480) continue;
928 if (std::ranges::find(_resolutions, Dimension(dm.dmPelsWidth, dm.dmPelsHeight)) != _resolutions.end()) continue;
929 _resolutions.emplace_back(dm.dmPelsWidth, dm.dmPelsHeight);
930 }
931
932 /* We have found no resolutions, show the default list */
933 if (_resolutions.empty()) {
934 _resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
935 }
936
937 SortResolutions();
938}
939
940void VideoDriver_Win32Base::Initialize()
941{
942 this->UpdateAutoResolution();
943
944 RegisterWndClass();
945 FindResolutions(this->GetFullscreenBpp());
946
947 /* fullscreen uses those */
948 this->width = this->width_org = _cur_resolution.width;
949 this->height = this->height_org = _cur_resolution.height;
950
951 Debug(driver, 2, "Resolution for display: {}x{}", _cur_resolution.width, _cur_resolution.height);
952}
953
955{
956 DestroyWindow(this->main_wnd);
957
958 if (this->fullscreen) ChangeDisplaySettings(nullptr, 0);
959 MyShowCursor(true);
960}
961void VideoDriver_Win32Base::MakeDirty(int left, int top, int width, int height)
962{
963 Rect r = {left, top, left + width, top + height};
964 this->dirty_rect = BoundingRect(this->dirty_rect, r);
965}
966
968{
969 if (!CopyPalette(_local_palette)) return;
970 this->MakeDirty(0, 0, _screen.width, _screen.height);
971}
972
974{
975 bool old_ctrl_pressed = _ctrl_pressed;
976
977 _ctrl_pressed = this->has_focus && GetAsyncKeyState(VK_CONTROL) < 0;
978 _shift_pressed = this->has_focus && GetAsyncKeyState(VK_SHIFT) < 0;
979
980 /* Speedup when pressing tab, except when using ALT+TAB
981 * to switch to another application. */
982 this->fast_forward_key_pressed = this->has_focus && GetAsyncKeyState(VK_TAB) < 0 && GetAsyncKeyState(VK_MENU) >= 0;
983
984 /* Determine which directional keys are down. */
985 if (this->has_focus) {
986 _dirkeys =
987 (GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
988 (GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
989 (GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
990 (GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
991 } else {
992 _dirkeys = 0;
993 }
994
995 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
996}
997
999{
1000 MSG mesg;
1001
1002 if (!PeekMessage(&mesg, nullptr, 0, 0, PM_REMOVE)) return false;
1003
1004 /* Convert key messages to char messages if we want text input. */
1005 if (EditBoxInGlobalFocus()) TranslateMessage(&mesg);
1006 DispatchMessage(&mesg);
1007
1008 return true;
1009}
1010
1012{
1013 this->StartGameThread();
1014
1015 for (;;) {
1016 if (_exit_game) break;
1017
1018 this->Tick();
1019 this->SleepTillNextTick();
1020 }
1021
1022 this->StopGameThread();
1023}
1024
1025void VideoDriver_Win32Base::ClientSizeChanged(int w, int h, bool force)
1026{
1027 /* Allocate backing store of the new size. */
1028 if (this->AllocateBackingStore(w, h, force)) {
1030
1032
1034 }
1035}
1036
1038{
1039 if (_window_maximize) ShowWindow(this->main_wnd, SW_SHOWNORMAL);
1040
1041 this->width = this->width_org = w;
1042 this->height = this->height_org = h;
1043
1044 return this->MakeWindow(_fullscreen); // _wnd.fullscreen screws up ingame resolution switching
1045}
1046
1048{
1049 bool res = this->MakeWindow(full_screen);
1050
1052 return res;
1053}
1054
1061
1062static BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC, LPRECT, LPARAM data)
1063{
1064 auto &list = *reinterpret_cast<std::vector<int>*>(data);
1065
1066 MONITORINFOEX monitorInfo = {};
1067 monitorInfo.cbSize = sizeof(MONITORINFOEX);
1068 GetMonitorInfo(hMonitor, &monitorInfo);
1069
1070 DEVMODE devMode = {};
1071 devMode.dmSize = sizeof(DEVMODE);
1072 devMode.dmDriverExtra = 0;
1073 EnumDisplaySettings(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, &devMode);
1074
1075 if (devMode.dmDisplayFrequency != 0) list.push_back(devMode.dmDisplayFrequency);
1076 return true;
1077}
1078
1080{
1081 std::vector<int> rates = {};
1082 EnumDisplayMonitors(nullptr, nullptr, MonitorEnumProc, reinterpret_cast<LPARAM>(&rates));
1083 return rates;
1084}
1085
1087{
1088 return { static_cast<uint>(GetSystemMetrics(SM_CXSCREEN)), static_cast<uint>(GetSystemMetrics(SM_CYSCREEN)) };
1089}
1090
1092{
1093 if (this->buffer_locked) return false;
1094 this->buffer_locked = true;
1095
1096 _screen.dst_ptr = this->GetVideoPointer();
1097 assert(_screen.dst_ptr != nullptr);
1098
1099 return true;
1100}
1101
1103{
1104 assert(_screen.dst_ptr != nullptr);
1105 if (_screen.dst_ptr != nullptr) {
1106 /* Hand video buffer back to the drawing backend. */
1107 this->ReleaseVideoPointer();
1108 _screen.dst_ptr = nullptr;
1109 }
1110
1111 this->buffer_locked = false;
1112}
1113
1114
1115static FVideoDriver_Win32GDI iFVideoDriver_Win32GDI;
1116
1117std::optional<std::string_view> VideoDriver_Win32GDI::Start(const StringList &param)
1118{
1119 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
1120
1121 this->Initialize();
1122
1123 this->MakePalette();
1125 this->MakeWindow(_fullscreen);
1126
1128
1129 this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
1130
1131 return std::nullopt;
1132}
1133
1135{
1136 DeleteObject(this->gdi_palette);
1137 DeleteObject(this->dib_sect);
1138
1140}
1141
1143{
1145
1146 w = std::max(w, 64);
1147 h = std::max(h, 64);
1148
1149 if (!force && w == _screen.width && h == _screen.height) return false;
1150
1151 BITMAPINFO *bi = (BITMAPINFO *)new char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256]();
1152 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1153
1154 bi->bmiHeader.biWidth = this->width = w;
1155 bi->bmiHeader.biHeight = -(this->height = h);
1156
1157 bi->bmiHeader.biPlanes = 1;
1158 bi->bmiHeader.biBitCount = bpp;
1159 bi->bmiHeader.biCompression = BI_RGB;
1160
1161 if (this->dib_sect) DeleteObject(this->dib_sect);
1162
1163 HDC dc = GetDC(0);
1164 this->dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID **)&this->buffer_bits, nullptr, 0);
1165 if (this->dib_sect == nullptr) {
1166 delete[] bi;
1167 UserError("CreateDIBSection failed");
1168 }
1169 ReleaseDC(0, dc);
1170
1171 _screen.width = w;
1172 _screen.pitch = (bpp == 8) ? Align(w, 4) : w;
1173 _screen.height = h;
1174 _screen.dst_ptr = this->GetVideoPointer();
1175
1176 delete[] bi;
1177 return true;
1178}
1179
1181{
1182 assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
1183 return this->AllocateBackingStore(_screen.width, _screen.height, true) && this->MakeWindow(_fullscreen, false);
1184}
1185
1186void VideoDriver_Win32GDI::MakePalette()
1187{
1189
1190 LOGPALETTE *pal = (LOGPALETTE *)new char[sizeof(LOGPALETTE) + (256 - 1) * sizeof(PALETTEENTRY)]();
1191
1192 pal->palVersion = 0x300;
1193 pal->palNumEntries = 256;
1194
1195 for (uint i = 0; i != 256; i++) {
1196 pal->palPalEntry[i].peRed = _local_palette.palette[i].r;
1197 pal->palPalEntry[i].peGreen = _local_palette.palette[i].g;
1198 pal->palPalEntry[i].peBlue = _local_palette.palette[i].b;
1199 pal->palPalEntry[i].peFlags = 0;
1200
1201 }
1202 this->gdi_palette = CreatePalette(pal);
1203 delete[] pal;
1204 if (this->gdi_palette == nullptr) UserError("CreatePalette failed!\n");
1205}
1206
1207void VideoDriver_Win32GDI::UpdatePalette(HDC dc, uint start, uint count)
1208{
1209 RGBQUAD rgb[256];
1210
1211 for (uint i = 0; i != count; i++) {
1212 rgb[i].rgbRed = _local_palette.palette[start + i].r;
1213 rgb[i].rgbGreen = _local_palette.palette[start + i].g;
1214 rgb[i].rgbBlue = _local_palette.palette[start + i].b;
1215 rgb[i].rgbReserved = 0;
1216 }
1217
1218 SetDIBColorTable(dc, start, count, rgb);
1219}
1220
1222{
1223 HDC hDC = GetWindowDC(hWnd);
1224 HPALETTE hOldPalette = SelectPalette(hDC, this->gdi_palette, FALSE);
1225 UINT nChanged = RealizePalette(hDC);
1226
1227 SelectPalette(hDC, hOldPalette, TRUE);
1228 ReleaseDC(hWnd, hDC);
1229 if (nChanged != 0) this->MakeDirty(0, 0, _screen.width, _screen.height);
1230}
1231
1233{
1234 PerformanceMeasurer framerate(PFE_VIDEO);
1235
1236 if (IsEmptyRect(this->dirty_rect)) return;
1237
1238 HDC dc = GetDC(this->main_wnd);
1239 HDC dc2 = CreateCompatibleDC(dc);
1240
1241 HBITMAP old_bmp = (HBITMAP)SelectObject(dc2, this->dib_sect);
1242 HPALETTE old_palette = SelectPalette(dc, this->gdi_palette, FALSE);
1243
1244 if (_local_palette.count_dirty != 0) {
1246
1247 switch (blitter->UsePaletteAnimation()) {
1249 this->UpdatePalette(dc2, _local_palette.first_dirty, _local_palette.count_dirty);
1250 break;
1251
1254 break;
1255 }
1256
1258 break;
1259
1260 default:
1261 NOT_REACHED();
1262 }
1263 _local_palette.count_dirty = 0;
1264 }
1265
1266 BitBlt(dc, 0, 0, this->width, this->height, dc2, 0, 0, SRCCOPY);
1267 SelectPalette(dc, old_palette, TRUE);
1268 SelectObject(dc2, old_bmp);
1269 DeleteDC(dc2);
1270
1271 ReleaseDC(this->main_wnd, dc);
1272
1273 this->dirty_rect = {};
1274}
1275
1276#ifdef _DEBUG
1277/* Keep this function here..
1278 * It allows you to redraw the screen from within the MSVC debugger */
1279/* static */ int VideoDriver_Win32GDI::RedrawScreenDebug()
1280{
1281 static int _fooctr;
1282
1284
1285 _screen.dst_ptr = drv->GetVideoPointer();
1286 UpdateWindows();
1287
1288 drv->Paint();
1289 GdiFlush();
1290
1291 return _fooctr++;
1292}
1293#endif
1294
1295#ifdef WITH_OPENGL
1296
1297#ifndef PFD_SUPPORT_COMPOSITION
1298# define PFD_SUPPORT_COMPOSITION 0x00008000
1299#endif
1300
1301static PFNWGLCREATECONTEXTATTRIBSARBPROC _wglCreateContextAttribsARB = nullptr;
1302static PFNWGLSWAPINTERVALEXTPROC _wglSwapIntervalEXT = nullptr;
1303static bool _hasWGLARBCreateContextProfile = false;
1304
1306static OGLProc GetOGLProcAddressCallback(const char *proc)
1307{
1308 OGLProc ret = reinterpret_cast<OGLProc>(wglGetProcAddress(proc));
1309 if (ret == nullptr) {
1310 /* Non-extension GL function? Try normal loading. */
1311 ret = reinterpret_cast<OGLProc>(GetProcAddress(GetModuleHandle(L"opengl32"), proc));
1312 }
1313 return ret;
1314}
1315
1321static std::optional<std::string_view> SelectPixelFormat(HDC dc)
1322{
1323 PIXELFORMATDESCRIPTOR pfd = {
1324 sizeof(PIXELFORMATDESCRIPTOR), // Size of this struct.
1325 1, // Version of this struct.
1326 PFD_DRAW_TO_WINDOW | // Require window support.
1327 PFD_SUPPORT_OPENGL | // Require OpenGL support.
1328 PFD_DOUBLEBUFFER | // Use double buffering.
1329 PFD_DEPTH_DONTCARE,
1330 PFD_TYPE_RGBA, // Request RGBA format.
1331 24, // 24 bpp (excluding alpha).
1332 0, 0, 0, 0, 0, 0, 0, 0, // Colour bits and shift ignored.
1333 0, 0, 0, 0, 0, // No accumulation buffer.
1334 0, 0, // No depth/stencil buffer.
1335 0, // No aux buffers.
1336 PFD_MAIN_PLANE, // Main layer.
1337 0, 0, 0, 0 // Ignored/reserved.
1338 };
1339
1340 pfd.dwFlags |= PFD_SUPPORT_COMPOSITION; // Make OpenTTD compatible with Aero.
1341
1342 /* Choose a suitable pixel format. */
1343 int format = ChoosePixelFormat(dc, &pfd);
1344 if (format == 0) return "No suitable pixel format found";
1345 if (!SetPixelFormat(dc, format, &pfd)) return "Can't set pixel format";
1346
1347 return std::nullopt;
1348}
1349
1351static void LoadWGLExtensions()
1352{
1353 /* Querying the supported WGL extensions and loading the matching
1354 * functions requires a valid context, even for the extensions
1355 * regarding context creation. To get around this, we create
1356 * a dummy window with a dummy context. The extension functions
1357 * remain valid even after this context is destroyed. */
1358 HWND wnd = CreateWindow(L"STATIC", L"dummy", WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
1359 HDC dc = GetDC(wnd);
1360
1361 /* Set pixel format of the window. */
1362 if (SelectPixelFormat(dc) == std::nullopt) {
1363 /* Create rendering context. */
1364 HGLRC rc = wglCreateContext(dc);
1365 if (rc != nullptr) {
1366 wglMakeCurrent(dc, rc);
1367
1368#ifdef __MINGW32__
1369 /* GCC doesn't understand the expected usage of wglGetProcAddress(). */
1370#pragma GCC diagnostic push
1371#pragma GCC diagnostic ignored "-Wcast-function-type"
1372#endif /* __MINGW32__ */
1373
1374 /* Get list of WGL extensions. */
1375 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
1376 if (wglGetExtensionsStringARB != nullptr) {
1377 std::string_view wgl_exts = wglGetExtensionsStringARB(dc);
1378 /* Bind supported functions. */
1379 if (HasStringInExtensionList(wgl_exts, "WGL_ARB_create_context")) {
1380 _wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
1381 }
1382 _hasWGLARBCreateContextProfile = HasStringInExtensionList(wgl_exts, "WGL_ARB_create_context_profile");
1383 if (HasStringInExtensionList(wgl_exts, "WGL_EXT_swap_control")) {
1384 _wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
1385 }
1386 }
1387
1388#ifdef __MINGW32__
1389#pragma GCC diagnostic pop
1390#endif
1391 wglMakeCurrent(nullptr, nullptr);
1392 wglDeleteContext(rc);
1393 }
1394 }
1395
1396 ReleaseDC(wnd, dc);
1397 DestroyWindow(wnd);
1398}
1399
1400static FVideoDriver_Win32OpenGL iFVideoDriver_Win32OpenGL;
1401
1402std::optional<std::string_view> VideoDriver_Win32OpenGL::Start(const StringList &param)
1403{
1404 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
1405
1406 Dimension old_res = _cur_resolution; // Save current screen resolution in case of errors, as MakeWindow invalidates it.
1407
1408 LoadWGLExtensions();
1409
1410 this->Initialize();
1411 this->MakeWindow(_fullscreen);
1412
1413 /* Create and initialize OpenGL context. */
1414 auto err = this->AllocateContext();
1415 if (err) {
1416 this->Stop();
1417 _cur_resolution = old_res;
1418 return err;
1419 }
1420
1421 this->driver_info = GetName();
1422 this->driver_info += " (";
1423 this->driver_info += OpenGLBackend::Get()->GetDriverName();
1424 this->driver_info += ")";
1425
1426 this->ClientSizeChanged(this->width, this->height, true);
1427 /* We should have a valid screen buffer now. If not, something went wrong and we should abort. */
1428 if (_screen.dst_ptr == nullptr) {
1429 this->Stop();
1430 _cur_resolution = old_res;
1431 return "Can't get pointer to screen buffer";
1432 }
1433 /* Main loop expects to start with the buffer unmapped. */
1434 this->ReleaseVideoPointer();
1435
1437
1438 this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
1439
1440 return std::nullopt;
1441}
1442
1443void VideoDriver_Win32OpenGL::Stop()
1444{
1445 this->DestroyContext();
1447}
1448
1449void VideoDriver_Win32OpenGL::DestroyContext()
1450{
1452
1453 wglMakeCurrent(nullptr, nullptr);
1454 if (this->gl_rc != nullptr) {
1455 wglDeleteContext(this->gl_rc);
1456 this->gl_rc = nullptr;
1457 }
1458 if (this->dc != nullptr) {
1459 ReleaseDC(this->main_wnd, this->dc);
1460 this->dc = nullptr;
1461 }
1462}
1463
1464void VideoDriver_Win32OpenGL::ToggleVsync(bool vsync)
1465{
1466 if (_wglSwapIntervalEXT != nullptr) {
1467 _wglSwapIntervalEXT(vsync);
1468 } else if (vsync) {
1469 Debug(driver, 0, "OpenGL: Vsync requested, but not supported by driver");
1470 }
1471}
1472
1473std::optional<std::string_view> VideoDriver_Win32OpenGL::AllocateContext()
1474{
1475 this->dc = GetDC(this->main_wnd);
1476
1477 auto err = SelectPixelFormat(this->dc);
1478 if (err) return err;
1479
1480 HGLRC rc = nullptr;
1481
1482 /* Create OpenGL device context. Try to get an 3.2+ context if possible. */
1483 if (_wglCreateContextAttribsARB != nullptr) {
1484 /* Try for OpenGL 4.5 first. */
1485 int attribs[] = {
1486 WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
1487 WGL_CONTEXT_MINOR_VERSION_ARB, 5,
1488 WGL_CONTEXT_FLAGS_ARB, _debug_driver_level >= 8 ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
1489 _hasWGLARBCreateContextProfile ? WGL_CONTEXT_PROFILE_MASK_ARB : 0, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, // Terminate list if WGL_ARB_create_context_profile isn't supported.
1490 0
1491 };
1492 rc = _wglCreateContextAttribsARB(this->dc, nullptr, attribs);
1493
1494 if (rc == nullptr) {
1495 /* Try again for a 3.2 context. */
1496 attribs[1] = 3;
1497 attribs[3] = 2;
1498 rc = _wglCreateContextAttribsARB(this->dc, nullptr, attribs);
1499 }
1500 }
1501
1502 if (rc == nullptr) {
1503 /* Old OpenGL or old driver, let's hope for the best. */
1504 rc = wglCreateContext(this->dc);
1505 if (rc == nullptr) return "Can't create OpenGL context";
1506 }
1507 if (!wglMakeCurrent(this->dc, rc)) return "Can't activate GL context";
1508
1509 this->ToggleVsync(_video_vsync);
1510
1511 this->gl_rc = rc;
1512 return OpenGLBackend::Create(&GetOGLProcAddressCallback, this->GetScreenSize());
1513}
1514
1515bool VideoDriver_Win32OpenGL::ToggleFullscreen(bool full_screen)
1516{
1517 if (_screen.dst_ptr != nullptr) this->ReleaseVideoPointer();
1518 this->DestroyContext();
1519 bool res = this->VideoDriver_Win32Base::ToggleFullscreen(full_screen);
1520 res &= this->AllocateContext() == std::nullopt;
1521 this->ClientSizeChanged(this->width, this->height, true);
1522 return res;
1523}
1524
1525bool VideoDriver_Win32OpenGL::AfterBlitterChange()
1526{
1527 assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
1528 this->ClientSizeChanged(this->width, this->height, true);
1529 return true;
1530}
1531
1532void VideoDriver_Win32OpenGL::PopulateSystemSprites()
1533{
1534 OpenGLBackend::Get()->PopulateCursorCache();
1535}
1536
1537void VideoDriver_Win32OpenGL::ClearSystemSprites()
1538{
1540}
1541
1542bool VideoDriver_Win32OpenGL::AllocateBackingStore(int w, int h, bool force)
1543{
1544 if (!force && w == _screen.width && h == _screen.height) return false;
1545
1546 this->width = w = std::max(w, 64);
1547 this->height = h = std::max(h, 64);
1548
1549 if (this->gl_rc == nullptr) return false;
1550
1551 if (_screen.dst_ptr != nullptr) this->ReleaseVideoPointer();
1552
1553 this->dirty_rect = {};
1554 bool res = OpenGLBackend::Get()->Resize(w, h, force);
1555 SwapBuffers(this->dc);
1556 _screen.dst_ptr = this->GetVideoPointer();
1557
1558 return res;
1559}
1560
1561void *VideoDriver_Win32OpenGL::GetVideoPointer()
1562{
1563 if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
1564 this->anim_buffer = OpenGLBackend::Get()->GetAnimBuffer();
1565 }
1567}
1568
1569void VideoDriver_Win32OpenGL::ReleaseVideoPointer()
1570{
1571 if (this->anim_buffer != nullptr) OpenGLBackend::Get()->ReleaseAnimBuffer(this->dirty_rect);
1572 OpenGLBackend::Get()->ReleaseVideoBuffer(this->dirty_rect);
1573 this->dirty_rect = {};
1574 _screen.dst_ptr = nullptr;
1575 this->anim_buffer = nullptr;
1576}
1577
1578void VideoDriver_Win32OpenGL::Paint()
1579{
1580 PerformanceMeasurer framerate(PFE_VIDEO);
1581
1582 if (_local_palette.count_dirty != 0) {
1584
1585 /* Always push a changed palette to OpenGL. */
1586 OpenGLBackend::Get()->UpdatePalette(_local_palette.palette, _local_palette.first_dirty, _local_palette.count_dirty);
1589 }
1590
1591 _local_palette.count_dirty = 0;
1592 }
1593
1596
1597 SwapBuffers(this->dc);
1598}
1599
1600#endif /* WITH_OPENGL */
#define AS(ap_name, size_x, size_y, min_year, max_year, catchment, noise, maint_cost, ttdpatch_type, class_id, name, preview)
AirportSpec definition for airports with at least one depot.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:138
How all blitters should look like.
Definition base.hpp:29
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
virtual void PaletteAnimate(const Palette &palette)=0
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
@ None
No palette animation.
Definition base.hpp:51
@ Blitter
The blitter takes care of the palette animation.
Definition base.hpp:53
@ VideoBackend
Palette animation should be done by video backend (8bpp only!).
Definition base.hpp:52
virtual void PostResize()
Post resize event.
Definition base.hpp:211
The factory for Windows' video driver.
Definition win32_v.h:124
void Paint()
Render video buffer to the screen.
Definition opengl.cpp:1039
uint8_t * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
Definition opengl.cpp:1170
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
Definition opengl.cpp:1148
bool Resize(int w, int h, bool force=false)
Change the size of the drawing window and allocate matching resources.
Definition opengl.cpp:913
static std::optional< std::string_view > Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
Create and initialize the singleton back-end class.
Definition opengl.cpp:464
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
Definition opengl.cpp:1025
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
Definition opengl.cpp:1231
void ClearCursorCache()
Queue a request for cursor cache clear.
Definition opengl.cpp:1135
static OpenGLBackend * Get()
Get singleton instance of this class.
Definition opengl.h:83
void DrawMouseCursor()
Draw mouse cursor on screen.
Definition opengl.cpp:1071
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
Definition opengl.cpp:1193
static void Destroy()
Free resources and destroy singleton back-end class.
Definition opengl.cpp:477
RAII class for measuring simple elements of performance.
Constant span of UTF-8 encoded data.
Definition utf8.hpp:28
Base class for Windows video drivers.
Definition win32_v.h:19
int height
Height in pixels of our display surface.
Definition win32_v.h:45
bool has_focus
Does our window have system focus?
Definition win32_v.h:42
void EditBoxLostFocus() override
An edit box lost the input focus.
Definition win32_v.cpp:1055
void CheckPaletteAnim() override
Process any pending palette animation.
Definition win32_v.cpp:967
void Stop() override
Stop this driver.
Definition win32_v.cpp:954
int height_org
Original monitor resolution height, before we changed it.
Definition win32_v.h:47
HWND main_wnd
Handle to system window.
Definition win32_v.h:40
bool fullscreen
Whether to use (true) fullscreen mode.
Definition win32_v.h:41
int width_org
Original monitor resolution width, before we changed it.
Definition win32_v.h:46
bool MakeWindow(bool full_screen, bool resize=true)
Instantiate a new window.
Definition win32_v.cpp:151
bool buffer_locked
Video buffer was locked by the main thread.
Definition win32_v.h:49
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition win32_v.cpp:961
bool PollEvent() override
Process a single system event.
Definition win32_v.cpp:998
virtual void PaletteChanged(HWND hWnd)=0
Palette of the window has changed.
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition win32_v.cpp:1091
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition win32_v.cpp:1079
virtual bool AllocateBackingStore(int w, int h, bool force=false)=0
(Re-)create the backing store.
int width
Width in pixels of our display surface.
Definition win32_v.h:44
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition win32_v.cpp:973
virtual uint8_t GetFullscreenBpp()
Colour depth to use for fullscreen display modes.
Definition win32_v.cpp:139
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition win32_v.cpp:1086
virtual void ReleaseVideoPointer()
Hand video buffer back to the painting backend.
Definition win32_v.h:80
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition win32_v.cpp:1102
void MainLoop() override
Perform the actual drawing.
Definition win32_v.cpp:1011
void ClaimMousePointer() override
Claim the exclusive rights for the mouse pointer.
Definition win32_v.cpp:63
Rect dirty_rect
Region of the screen that needs redrawing.
Definition win32_v.h:43
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition win32_v.cpp:1047
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition win32_v.cpp:1037
The GDI video driver for windows.
Definition win32_v.h:92
void * buffer_bits
Internal rendering buffer.
Definition win32_v.h:107
HBITMAP dib_sect
System bitmap object referencing our rendering buffer.
Definition win32_v.h:105
void PaletteChanged(HWND hWnd) override
Palette of the window has changed.
Definition win32_v.cpp:1221
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition win32_v.cpp:1117
void * GetVideoPointer() override
Get a pointer to the video buffer.
Definition win32_v.h:110
HPALETTE gdi_palette
Palette object for 8bpp blitter.
Definition win32_v.h:106
bool AllocateBackingStore(int w, int h, bool force=false) override
(Re-)create the backing store.
Definition win32_v.cpp:1142
void Paint() override
Paint the window.
Definition win32_v.cpp:1232
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition win32_v.cpp:1180
void Stop() override
Stop this driver.
Definition win32_v.cpp:1134
bool fast_forward_key_pressed
The fast-forward key is being pressed.
void Tick()
Give the video-driver a tick.
void SleepTillNextTick()
Sleep till the next tick is about to happen.
void StartGameThread()
Start the loop for game-tick.
static std::string GetCaption()
Get the caption to use for the game's title bar.
void StopGameThread()
Stop the loop for the game-tick.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
static Palette _local_palette
Current palette to use for drawing.
Definition cocoa_ogl.mm:42
static OGLProc GetOGLProcAddressCallback(const char *proc)
Platform-specific callback to get an OpenGL function pointer.
Definition cocoa_ogl.mm:45
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
bool GetDriverParamBool(const StringList &parm, std::string_view name)
Get a boolean parameter the list of parameters.
Definition driver.cpp:67
std::vector< Dimension > _resolutions
List of resolutions.
Definition driver.cpp:28
Dimension _cur_resolution
The current resolution.
Definition driver.cpp:29
Error reporting related functions.
Factory to 'query' all available blitters.
fluid_settings_t * settings
FluidSynth settings handle.
Types for recording game performance data.
@ PFE_VIDEO
Speed of painting drawn video buffer.
Rect BoundingRect(const Rect &r1, const Rect &r2)
Compute the bounding rectangle around two rectangles.
Geometry functions.
bool IsEmptyRect(const Rect &r)
Check if a rectangle is empty.
bool _shift_pressed
Is Shift pressed?
Definition gfx.cpp:40
bool _left_button_down
Is left mouse button pressed?
Definition gfx.cpp:42
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:39
uint8_t _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition gfx.cpp:35
bool _left_button_clicked
Is left mouse button clicked?
Definition gfx.cpp:43
bool _right_button_clicked
Is right mouse button clicked?
Definition gfx.cpp:45
bool _right_button_down
Is right mouse button pressed?
Definition gfx.cpp:44
bool AdjustGUIZoom(bool automatic)
Resolve GUI zoom level and adjust GUI to new zoom, if auto-suggestion is requested.
Definition gfx.cpp:1837
Functions related to the gfx engine.
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition window.cpp:2709
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition window.cpp:3133
void GameSizeChanged()
Size of the application screen changed.
Definition main_gui.cpp:596
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition window.cpp:2972
void HandleKeypress(uint keycode, char32_t key)
Handle keyboard input.
Definition window.cpp:2653
void HandleTextInput(std::string_view str, bool marked=false, std::optional< size_t > caret=std::nullopt, std::optional< size_t > insert_location=std::nullopt, std::optional< size_t > replacement_end=std::nullopt)
Handle text input.
Definition window.cpp:2745
@ S8BPP_HARDWARE
Full 8bpp support by OS and hardware.
Definition gfx_type.h:383
@ WKC_BACKSLASH
\ Backslash
Definition gfx_type.h:101
@ WKC_MINUS
Definition gfx_type.h:106
@ WKC_COMMA
, Comma
Definition gfx_type.h:104
@ WKC_PERIOD
. Period
Definition gfx_type.h:105
@ WKC_EQUALS
= Equals
Definition gfx_type.h:99
@ WKC_SLASH
/ Forward slash
Definition gfx_type.h:97
@ WKC_SINGLEQUOTE
' Single quote
Definition gfx_type.h:103
@ WKC_R_BRACKET
] Right square bracket
Definition gfx_type.h:102
@ WKC_L_BRACKET
[ Left square bracket
Definition gfx_type.h:100
@ WKC_SEMICOLON
; Semicolon
Definition gfx_type.h:98
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1554
Functions/types related to loading libraries dynamically.
#define Point
Macro that prevents name conflicts between included headers.
Integer math functions.
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition math_func.hpp:37
bool HasStringInExtensionList(std::string_view string, std::string_view substring)
Find a substring in a string made of space delimited elements.
Definition opengl.cpp:150
OpenGL video driver support.
Some generic types.
@ Stop
Go to the depot and stop there.
Definition order_type.h:178
void GetKeyboardLayout()
Retrieve keyboard layout from language string or (if set) config file.
Definition osk_gui.cpp:352
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:225
Functions related to modal progress.
Pseudo random number generator.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Definition of base types and functions in a cross-platform compatible way.
char32_t Utf16DecodeSurrogate(uint lead, uint trail)
Convert an UTF-16 surrogate pair to the corresponding Unicode character.
Definition string_func.h:84
bool Utf16IsLeadSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:63
bool Utf16IsTrailSurrogate(uint c)
Is the given character a lead surrogate code point?
Definition string_func.h:73
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
T y
Y coordinate.
T x
X coordinate.
Dimensions (a width and height) of a rectangle in 2D.
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:374
Specification of a rectangle with absolute coordinates of all edges.
Functions related to text effects.
Base of all threads.
Handling of UTF-8 encoded data.
bool _video_vsync
Whether we should use vsync (only if active video driver supports HW acceleration).
std::string_view convert_from_fs(const std::wstring_view src, std::span< char > dst_buf)
Convert to OpenTTD's encoding from that of the environment in UNICODE.
Definition win32.cpp:374
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
Definition win32.cpp:356
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:340
Declarations of functions for MS windows systems.
static LRESULT HandleCharMsg(uint keycode, char32_t charcode)
Forward key presses to the window system.
Definition win32_v.cpp:253
static bool DrawIMECompositionString()
Should we draw the composition string ourself, i.e is this a normal IME?
Definition win32_v.cpp:280
static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
Handle WM_IME_COMPOSITION messages.
Definition win32_v.cpp:342
static void SetCandidatePos(HWND hwnd)
Set the position of the candidate window.
Definition win32_v.cpp:308
static void CancelIMEComposition(HWND hwnd)
Clear the current composition string.
Definition win32_v.cpp:404
static void SetCompositionPos(HWND hwnd)
Set position of the composition window to the caret position.
Definition win32_v.cpp:286
Base of the Windows video driver.
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition window.cpp:3424
bool EditBoxInGlobalFocus()
Check if an edit box is in global focus.
Definition window.cpp:448
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition window.cpp:3325
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ WC_CONSOLE
Console; Window numbers:
@ WC_GAME_OPTIONS
Game options window; Window numbers: