OpenTTD Source 20260311-master-g511d3794ce
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
258static LRESULT HandleCharMsg(uint keycode, char32_t charcode)
259{
260 static char32_t prev_char = 0;
261
262 /* Did we get a lead surrogate? If yes, store and exit. */
263 if (Utf16IsLeadSurrogate(charcode)) {
264 if (prev_char != 0) Debug(driver, 1, "Got two UTF-16 lead surrogates, dropping the first one");
265 prev_char = charcode;
266 return 0;
267 }
268
269 /* Stored lead surrogate and incoming trail surrogate? Combine and forward to input handling. */
270 if (prev_char != 0) {
271 if (Utf16IsTrailSurrogate(charcode)) {
272 charcode = Utf16DecodeSurrogate(prev_char, charcode);
273 } else {
274 Debug(driver, 1, "Got an UTF-16 lead surrogate without a trail surrogate, dropping the lead surrogate");
275 }
276 }
277 prev_char = 0;
278
279 HandleKeypress(keycode, charcode);
280
281 return 0;
282}
283
289{
290 return (_imm_props & IME_PROP_AT_CARET) && !(_imm_props & IME_PROP_SPECIAL_UI);
291}
292
297static void SetCompositionPos(HWND hwnd)
298{
299 HIMC hIMC = ImmGetContext(hwnd);
300 if (hIMC != nullptr) {
301 COMPOSITIONFORM cf;
302 cf.dwStyle = CFS_POINT;
303
304 if (EditBoxInGlobalFocus()) {
305 /* Get caret position. */
306 Point pt = _focused_window->GetCaretPosition();
307 cf.ptCurrentPos.x = _focused_window->left + pt.x;
308 cf.ptCurrentPos.y = _focused_window->top + pt.y;
309 } else {
310 cf.ptCurrentPos.x = 0;
311 cf.ptCurrentPos.y = 0;
312 }
313 ImmSetCompositionWindow(hIMC, &cf);
314 }
315 ImmReleaseContext(hwnd, hIMC);
316}
317
322static void SetCandidatePos(HWND hwnd)
323{
324 HIMC hIMC = ImmGetContext(hwnd);
325 if (hIMC != nullptr) {
326 CANDIDATEFORM cf;
327 cf.dwIndex = 0;
328 cf.dwStyle = CFS_EXCLUDE;
329
330 if (EditBoxInGlobalFocus()) {
331 Point pt = _focused_window->GetCaretPosition();
332 cf.ptCurrentPos.x = _focused_window->left + pt.x;
333 cf.ptCurrentPos.y = _focused_window->top + pt.y;
334 if (_focused_window->window_class == WC_CONSOLE) {
335 cf.rcArea.left = _focused_window->left;
336 cf.rcArea.top = _focused_window->top;
337 cf.rcArea.right = _focused_window->left + _focused_window->width;
338 cf.rcArea.bottom = _focused_window->top + _focused_window->height;
339 } else {
340 cf.rcArea.left = _focused_window->left + _focused_window->nested_focus->pos_x;
341 cf.rcArea.top = _focused_window->top + _focused_window->nested_focus->pos_y;
342 cf.rcArea.right = cf.rcArea.left + _focused_window->nested_focus->current_x;
343 cf.rcArea.bottom = cf.rcArea.top + _focused_window->nested_focus->current_y;
344 }
345 } else {
346 cf.ptCurrentPos.x = 0;
347 cf.ptCurrentPos.y = 0;
348 SetRectEmpty(&cf.rcArea);
349 }
350 ImmSetCandidateWindow(hIMC, &cf);
351 }
352 ImmReleaseContext(hwnd, hIMC);
353}
354
362static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
363{
364 HIMC hIMC = ImmGetContext(hwnd);
365
366 if (hIMC != nullptr) {
367 if (lParam & GCS_RESULTSTR) {
368 /* Read result string from the IME. */
369 LONG len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, nullptr, 0); // Length is always in bytes, even in UNICODE build.
370 std::wstring str(len + 1, L'\0');
371 len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, str.data(), len);
372 str[len / sizeof(wchar_t)] = L'\0';
373
374 /* Transmit text to windowing system. */
375 if (len > 0) {
376 HandleTextInput({}, true); // Clear marked string.
378 }
379 SetCompositionPos(hwnd);
380
381 /* Don't pass the result string on to the default window proc. */
382 lParam &= ~(GCS_RESULTSTR | GCS_RESULTCLAUSE | GCS_RESULTREADCLAUSE | GCS_RESULTREADSTR);
383 }
384
385 if ((lParam & GCS_COMPSTR) && DrawIMECompositionString()) {
386 /* Read composition string from the IME. */
387 LONG len = ImmGetCompositionString(hIMC, GCS_COMPSTR, nullptr, 0); // Length is always in bytes, even in UNICODE build.
388 std::wstring str(len + 1, L'\0');
389 len = ImmGetCompositionString(hIMC, GCS_COMPSTR, str.data(), len);
390 str[len / sizeof(wchar_t)] = L'\0';
391
392 if (len > 0) {
393 static char utf8_buf[1024];
394 convert_from_fs(str, utf8_buf);
395
396 /* Convert caret position from bytes in the input string to a position in the UTF-8 encoded string. */
397 LONG caret_bytes = ImmGetCompositionString(hIMC, GCS_CURSORPOS, nullptr, 0);
398 Utf8View view(utf8_buf);
399 auto caret = view.begin();
400 const auto end = view.end();
401 for (const wchar_t *c = str.c_str(); *c != '\0' && caret != end && caret_bytes > 0; c++, caret_bytes--) {
402 /* Skip DBCS lead bytes or leading surrogates. */
403 if (Utf16IsLeadSurrogate(*c)) {
404 c++;
405 caret_bytes--;
406 }
407 ++caret;
408 }
409
410 HandleTextInput(utf8_buf, true, caret.GetByteOffset());
411 } else {
412 HandleTextInput({}, true);
413 }
414
415 lParam &= ~(GCS_COMPSTR | GCS_COMPATTR | GCS_COMPCLAUSE | GCS_CURSORPOS | GCS_DELTASTART);
416 }
417 }
418 ImmReleaseContext(hwnd, hIMC);
419
420 return lParam != 0 ? DefWindowProc(hwnd, WM_IME_COMPOSITION, wParam, lParam) : 0;
421}
422
427static void CancelIMEComposition(HWND hwnd)
428{
429 HIMC hIMC = ImmGetContext(hwnd);
430 if (hIMC != nullptr) ImmNotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
431 ImmReleaseContext(hwnd, hIMC);
432 /* Clear any marked string from the current edit box. */
433 HandleTextInput({}, true);
434}
435
436#if defined(_MSC_VER) && defined(NTDDI_WIN10_RS4)
437/* We only use WinRT functions on Windows 10 or later. Unfortunately, newer Windows SDKs are now
438 * linking the two functions below directly instead of using dynamic linking as previously.
439 * To avoid any runtime linking errors on Windows 7 or older, we stub in our own dynamic
440 * linking trampoline. */
441
442static LibraryLoader _combase("combase.dll");
443
444extern "C" int32_t __stdcall WINRT_IMPL_RoOriginateLanguageException(int32_t error, void *message, void *languageException) noexcept
445{
446 typedef BOOL(WINAPI *PFNRoOriginateLanguageException)(int32_t, void *, void *);
447 static PFNRoOriginateLanguageException RoOriginateLanguageException = _combase.GetFunction("RoOriginateLanguageException");
448
449 if (RoOriginateLanguageException != nullptr) {
450 return RoOriginateLanguageException(error, message, languageException);
451 } else {
452 return TRUE;
453 }
454}
455
456extern "C" int32_t __stdcall WINRT_IMPL_RoGetActivationFactory(void *classId, winrt::guid const &iid, void **factory) noexcept
457{
458 typedef BOOL(WINAPI *PFNRoGetActivationFactory)(void *, winrt::guid const &, void **);
459 static PFNRoGetActivationFactory RoGetActivationFactory = _combase.GetFunction("RoGetActivationFactory");
460
461 if (RoGetActivationFactory != nullptr) {
462 return RoGetActivationFactory(classId, iid, factory);
463 } else {
464 *factory = nullptr;
465 return winrt::impl::error_class_not_available;
466 }
467}
468#endif
469
470static bool IsDarkModeEnabled()
471{
472 /* Only build if SDK is Windows 10 1803 or later. */
473#if defined(_MSC_VER) && defined(NTDDI_WIN10_RS4)
474 if (IsWindows10OrGreater()) {
475 try {
476 /*
477 * The official documented way to find out if the system is running in dark mode is to
478 * check the brightness of the current theme's colour.
479 * See: https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/ui/apply-windows-themes#know-when-dark-mode-is-enabled
480 *
481 * There are other variants floating around on the Internet, but they all rely on internal,
482 * undocumented Windows functions that may or may not work in the future.
483 */
484 winrt::Windows::UI::ViewManagement::UISettings settings;
485 auto foreground = settings.GetColorValue(winrt::Windows::UI::ViewManagement::UIColorType::Foreground);
486
487 /* If the Foreground colour is a light colour, the system is running in dark mode. */
488 return ((5 * foreground.G) + (2 * foreground.R) + foreground.B) > (8 * 128);
489 } catch (...) {
490 /* Some kind of error, like a too old Windows version. Just return false. */
491 return false;
492 }
493 }
494#endif /* defined(_MSC_VER) && defined(NTDDI_WIN10_RS4) */
495
496 return false;
497}
498
499static void SetDarkModeForWindow(HWND hWnd, bool dark_mode)
500{
501 /* Only build if SDK is Windows 10+. */
502#if defined(NTDDI_WIN10)
503 if (!IsWindows10OrGreater()) return;
504
505 /* This function is documented, but not supported on all Windows 10/11 SDK builds. For this
506 * reason, the code uses dynamic loading and ignores any errors for a best-effort result. */
507 static LibraryLoader _dwmapi("dwmapi.dll");
508 typedef HRESULT(WINAPI *PFNDWMSETWINDOWATTRIBUTE)(HWND, DWORD, LPCVOID, DWORD);
509 static const PFNDWMSETWINDOWATTRIBUTE DwmSetWindowAttribute = _dwmapi.GetFunction("DwmSetWindowAttribute");
510
511 if (DwmSetWindowAttribute != nullptr) {
512 /* Contrary to the published documentation, DWMWA_USE_IMMERSIVE_DARK_MODE does not change the
513 * window chrome according to the current theme, but forces it to either light or dark mode.
514 * As such, the set value has to depend on the current theming mode.*/
515 BOOL value = dark_mode ? TRUE : FALSE;
516 if (DwmSetWindowAttribute(hWnd, 20 /* DWMWA_USE_IMMERSIVE_DARK_MODE */, &value, sizeof(value)) != S_OK) {
517 DwmSetWindowAttribute(hWnd, 19 /* DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 */, &value, sizeof(value)); // Ignore errors. It works or it doesn't.
518 }
519 }
520#endif /* defined(NTDDI_WIN10) */
521}
522
523LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
524{
525 static uint32_t keycode = 0;
526 static bool console = false;
527
528 const float SCROLL_BUILTIN_MULTIPLIER = 14.0f / WHEEL_DELTA;
529
530 VideoDriver_Win32Base *video_driver = (VideoDriver_Win32Base *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
531
532 switch (msg) {
533 case WM_CREATE:
534 SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)((LPCREATESTRUCT)lParam)->lpCreateParams);
535 _cursor.in_window = false; // Win32 has mouse tracking.
536 SetCompositionPos(hwnd);
537 _imm_props = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
538
539 /* Enable dark mode theming for window chrome. */
540 SetDarkModeForWindow(hwnd, IsDarkModeEnabled());
541 break;
542
543 case WM_SETTINGCHANGE:
544 /* Synchronize dark mode theming state. */
545 SetDarkModeForWindow(hwnd, IsDarkModeEnabled());
546 break;
547
548 case WM_PAINT: {
549 RECT r;
550 GetUpdateRect(hwnd, &r, FALSE);
551 video_driver->MakeDirty(r.left, r.top, r.right - r.left, r.bottom - r.top);
552
553 ValidateRect(hwnd, nullptr);
554 return 0;
555 }
556
557 case WM_PALETTECHANGED:
558 if ((HWND)wParam == hwnd) return 0;
559 [[fallthrough]];
560
561 case WM_QUERYNEWPALETTE:
562 video_driver->PaletteChanged(hwnd);
563 return 0;
564
565 case WM_CLOSE:
566 HandleExitGameRequest();
567 return 0;
568
569 case WM_DESTROY:
570 if (_window_maximize) _cur_resolution = _bck_resolution;
571 return 0;
572
573 case WM_LBUTTONDOWN:
574 SetCapture(hwnd);
575 _left_button_down = true;
577 return 0;
578
579 case WM_LBUTTONUP:
580 ReleaseCapture();
581 _left_button_down = false;
582 _left_button_clicked = false;
584 return 0;
585
586 case WM_RBUTTONDOWN:
587 SetCapture(hwnd);
588 _right_button_down = true;
591 return 0;
592
593 case WM_RBUTTONUP:
594 ReleaseCapture();
595 _right_button_down = false;
597 return 0;
598
599 case WM_MOUSELEAVE:
600 UndrawMouseCursor();
601 _cursor.in_window = false;
602
603 if (!_left_button_down && !_right_button_down) MyShowCursor(true);
604 return 0;
605
606 case WM_MOUSEMOVE: {
607 int x = (int16_t)LOWORD(lParam);
608 int y = (int16_t)HIWORD(lParam);
609
610 /* If the mouse was not in the window and it has moved it means it has
611 * come into the window, so start drawing the mouse. Also start
612 * tracking the mouse for exiting the window */
613 if (!_cursor.in_window) {
614 _cursor.in_window = true;
615 TRACKMOUSEEVENT tme;
616 tme.cbSize = sizeof(tme);
617 tme.dwFlags = TME_LEAVE;
618 tme.hwndTrack = hwnd;
619
620 TrackMouseEvent(&tme);
621 }
622
623 if (_cursor.fix_at) {
624 /* Get all queued mouse events now in case we have to warp the cursor. In the
625 * end, we only care about the current mouse position and not bygone events. */
626 MSG m;
627 while (PeekMessage(&m, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE | PM_NOYIELD | PM_QS_INPUT)) {
628 x = (int16_t)LOWORD(m.lParam);
629 y = (int16_t)HIWORD(m.lParam);
630 }
631 }
632
633 if (_cursor.UpdateCursorPosition(x, y)) {
634 POINT pt;
635 pt.x = _cursor.pos.x;
636 pt.y = _cursor.pos.y;
637 ClientToScreen(hwnd, &pt);
638 SetCursorPos(pt.x, pt.y);
639 }
640 MyShowCursor(false);
642 return 0;
643 }
644
645 case WM_INPUTLANGCHANGE:
646 _imm_props = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
647 break;
648
649 case WM_IME_SETCONTEXT:
650 /* Don't show the composition window if we draw the string ourself. */
651 if (DrawIMECompositionString()) lParam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
652 break;
653
654 case WM_IME_STARTCOMPOSITION:
655 SetCompositionPos(hwnd);
656 if (DrawIMECompositionString()) return 0;
657 break;
658
659 case WM_IME_COMPOSITION:
660 return HandleIMEComposition(hwnd, wParam, lParam);
661
662 case WM_IME_ENDCOMPOSITION:
663 /* Clear any pending composition string. */
664 HandleTextInput({}, true);
665 if (DrawIMECompositionString()) return 0;
666 break;
667
668 case WM_IME_NOTIFY:
669 if (wParam == IMN_OPENCANDIDATE) SetCandidatePos(hwnd);
670 break;
671
672 case WM_DEADCHAR:
673 console = GB(lParam, 16, 8) == 41;
674 return 0;
675
676 case WM_CHAR: {
677 uint scancode = GB(lParam, 16, 8);
678 uint charcode = wParam;
679
680 /* If the console key is a dead-key, we need to press it twice to get a WM_CHAR message.
681 * But we then get two WM_CHAR messages, so ignore the first one */
682 if (console && scancode == 41) {
683 console = false;
684 return 0;
685 }
686
687 /* IMEs and other input methods sometimes send a WM_CHAR without a WM_KEYDOWN,
688 * clear the keycode so a previous WM_KEYDOWN doesn't become 'stuck'. */
689 uint cur_keycode = keycode;
690 keycode = 0;
691
692 return HandleCharMsg(cur_keycode, charcode);
693 }
694
695 case WM_KEYDOWN: {
696 /* No matter the keyboard layout, we will map the '~' to the console. */
697 uint scancode = GB(lParam, 16, 8);
698 keycode = scancode == 41 ? (uint)WKC_BACKQUOTE : MapWindowsKey(wParam);
699
700 uint charcode = MapVirtualKey(wParam, MAPVK_VK_TO_CHAR);
701
702 /* No character translation? */
703 if (charcode == 0) {
704 HandleKeypress(keycode, 0);
705 return 0;
706 }
707
708 /* If an edit box is in focus, wait for the corresponding WM_CHAR message. */
709 if (!EditBoxInGlobalFocus()) {
710 /* Is the console key a dead key? If yes, ignore the first key down event. */
711 if (HasBit(charcode, 31) && !console) {
712 if (scancode == 41) {
713 console = true;
714 return 0;
715 }
716 }
717 console = false;
718
719 /* IMEs and other input methods sometimes send a WM_CHAR without a WM_KEYDOWN,
720 * clear the keycode so a previous WM_KEYDOWN doesn't become 'stuck'. */
721 uint cur_keycode = keycode;
722 keycode = 0;
723
724 return HandleCharMsg(cur_keycode, LOWORD(charcode));
725 }
726
727 return 0;
728 }
729
730 case WM_SYSKEYDOWN: // user presses F10 or Alt, both activating the title-menu
731 switch (wParam) {
732 case VK_RETURN:
733 case 'F': // Full Screen on ALT + ENTER/F
734 ToggleFullScreen(!video_driver->fullscreen);
735 return 0;
736
737 case VK_MENU: // Just ALT
738 return 0; // do nothing
739
740 case VK_F10: // F10, ignore activation of menu
741 HandleKeypress(MapWindowsKey(wParam), 0);
742 return 0;
743
744 default: // ALT in combination with something else
745 HandleKeypress(MapWindowsKey(wParam), 0);
746 break;
747 }
748 break;
749
750 case WM_SIZE:
751 if (wParam != SIZE_MINIMIZED) {
752 /* Set maximized flag when we maximize (obviously), but also when we
753 * switched to fullscreen from a maximized state */
754 _window_maximize = (wParam == SIZE_MAXIMIZED || (_window_maximize && _fullscreen));
755 if (_window_maximize || _fullscreen) _bck_resolution = _cur_resolution;
756 video_driver->ClientSizeChanged(LOWORD(lParam), HIWORD(lParam));
757 }
758 return 0;
759
760 case WM_SIZING: {
761 RECT *r = (RECT*)lParam;
762 RECT r2;
763 int w, h;
764
765 SetRect(&r2, 0, 0, 0, 0);
766 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
767
768 w = r->right - r->left - (r2.right - r2.left);
769 h = r->bottom - r->top - (r2.bottom - r2.top);
770 w = std::max(w, 64);
771 h = std::max(h, 64);
772 SetRect(&r2, 0, 0, w, h);
773
774 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
775 w = r2.right - r2.left;
776 h = r2.bottom - r2.top;
777
778 switch (wParam) {
779 case WMSZ_BOTTOM:
780 r->bottom = r->top + h;
781 break;
782
783 case WMSZ_BOTTOMLEFT:
784 r->bottom = r->top + h;
785 r->left = r->right - w;
786 break;
787
788 case WMSZ_BOTTOMRIGHT:
789 r->bottom = r->top + h;
790 r->right = r->left + w;
791 break;
792
793 case WMSZ_LEFT:
794 r->left = r->right - w;
795 break;
796
797 case WMSZ_RIGHT:
798 r->right = r->left + w;
799 break;
800
801 case WMSZ_TOP:
802 r->top = r->bottom - h;
803 break;
804
805 case WMSZ_TOPLEFT:
806 r->top = r->bottom - h;
807 r->left = r->right - w;
808 break;
809
810 case WMSZ_TOPRIGHT:
811 r->top = r->bottom - h;
812 r->right = r->left + w;
813 break;
814 }
815 return TRUE;
816 }
817
818 case WM_DPICHANGED: {
819 auto did_adjust = AdjustGUIZoom(true);
820
821 /* Resize the window to match the new DPI setting. */
822 RECT *prcNewWindow = (RECT *)lParam;
823 SetWindowPos(hwnd,
824 nullptr,
825 prcNewWindow->left,
826 prcNewWindow->top,
827 prcNewWindow->right - prcNewWindow->left,
828 prcNewWindow->bottom - prcNewWindow->top,
829 SWP_NOZORDER | SWP_NOACTIVATE);
830
831 if (did_adjust) ReInitAllWindows(true);
832
833 return 0;
834 }
835
836/* needed for wheel */
837#if !defined(WM_MOUSEWHEEL)
838# define WM_MOUSEWHEEL 0x020A
839#endif /* WM_MOUSEWHEEL */
840#if !defined(WM_MOUSEHWHEEL)
841# define WM_MOUSEHWHEEL 0x020E
842#endif /* WM_MOUSEHWHEEL */
843#if !defined(GET_WHEEL_DELTA_WPARAM)
844# define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam))
845#endif /* GET_WHEEL_DELTA_WPARAM */
846
847 case WM_MOUSEWHEEL: {
848 int delta = GET_WHEEL_DELTA_WPARAM(wParam);
849
850 if (delta < 0) {
851 _cursor.wheel++;
852 } else if (delta > 0) {
853 _cursor.wheel--;
854 }
855
856 _cursor.v_wheel -= static_cast<float>(delta) * SCROLL_BUILTIN_MULTIPLIER * _settings_client.gui.scrollwheel_multiplier;
857 _cursor.wheel_moved = true;
859 return 0;
860 }
861
862 case WM_MOUSEHWHEEL: {
863 int delta = GET_WHEEL_DELTA_WPARAM(wParam);
864
865 _cursor.h_wheel += static_cast<float>(delta) * SCROLL_BUILTIN_MULTIPLIER * _settings_client.gui.scrollwheel_multiplier;
866 _cursor.wheel_moved = true;
868 return 0;
869 }
870
871 case WM_SETFOCUS:
872 video_driver->has_focus = true;
873 SetCompositionPos(hwnd);
874 break;
875
876 case WM_KILLFOCUS:
877 video_driver->has_focus = false;
878 break;
879
880 case WM_ACTIVATE: {
881 /* Don't do anything if we are closing openttd */
882 if (_exit_game) break;
883
884 bool active = (LOWORD(wParam) != WA_INACTIVE);
885 bool minimized = (HIWORD(wParam) != 0);
886 if (video_driver->fullscreen) {
887 if (active && minimized) {
888 /* Restore the game window */
889 Dimension d = _bck_resolution; // Save current non-fullscreen window size as it will be overwritten by ShowWindow.
890 ShowWindow(hwnd, SW_RESTORE);
891 _bck_resolution = d;
892 video_driver->MakeWindow(true);
893 } else if (!active && !minimized) {
894 /* Minimise the window and restore desktop */
895 ShowWindow(hwnd, SW_MINIMIZE);
896 ChangeDisplaySettings(nullptr, 0);
897 }
898 }
899 break;
900 }
901 }
902
903 return DefWindowProc(hwnd, msg, wParam, lParam);
904}
905
906static void RegisterWndClass()
907{
908 static bool registered = false;
909
910 if (registered) return;
911
912 HINSTANCE hinst = GetModuleHandle(nullptr);
913 WNDCLASS wnd = {
914 CS_OWNDC,
915 WndProcGdi,
916 0,
917 0,
918 hinst,
919 LoadIcon(hinst, MAKEINTRESOURCE(100)),
920 LoadCursor(nullptr, IDC_ARROW),
921 0,
922 0,
923 L"OTTD"
924 };
925
926 registered = true;
927 if (!RegisterClass(&wnd)) UserError("RegisterClass failed");
928}
929
930static const Dimension default_resolutions[] = {
931 { 640, 480 },
932 { 800, 600 },
933 { 1024, 768 },
934 { 1152, 864 },
935 { 1280, 800 },
936 { 1280, 960 },
937 { 1280, 1024 },
938 { 1400, 1050 },
939 { 1600, 1200 },
940 { 1680, 1050 },
941 { 1920, 1200 }
942};
943
944static void FindResolutions(uint8_t bpp)
945{
946 _resolutions.clear();
947
948 DEVMODE dm;
949 for (uint i = 0; EnumDisplaySettings(nullptr, i, &dm) != 0; i++) {
950 if (dm.dmBitsPerPel != bpp || dm.dmPelsWidth < 640 || dm.dmPelsHeight < 480) continue;
951 if (std::ranges::find(_resolutions, Dimension(dm.dmPelsWidth, dm.dmPelsHeight)) != _resolutions.end()) continue;
952 _resolutions.emplace_back(dm.dmPelsWidth, dm.dmPelsHeight);
953 }
954
955 /* We have found no resolutions, show the default list */
956 if (_resolutions.empty()) {
957 _resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
958 }
959
960 SortResolutions();
961}
962
963void VideoDriver_Win32Base::Initialize()
964{
965 this->UpdateAutoResolution();
966
967 RegisterWndClass();
968 FindResolutions(this->GetFullscreenBpp());
969
970 /* fullscreen uses those */
971 this->width = this->width_org = _cur_resolution.width;
972 this->height = this->height_org = _cur_resolution.height;
973
974 Debug(driver, 2, "Resolution for display: {}x{}", _cur_resolution.width, _cur_resolution.height);
975}
976
978{
979 DestroyWindow(this->main_wnd);
980
981 if (this->fullscreen) ChangeDisplaySettings(nullptr, 0);
982 MyShowCursor(true);
983}
984void VideoDriver_Win32Base::MakeDirty(int left, int top, int width, int height)
985{
986 Rect r = {left, top, left + width, top + height};
987 this->dirty_rect = BoundingRect(this->dirty_rect, r);
988}
989
991{
992 if (!CopyPalette(_local_palette)) return;
993 this->MakeDirty(0, 0, _screen.width, _screen.height);
994}
995
997{
998 bool old_ctrl_pressed = _ctrl_pressed;
999
1000 _ctrl_pressed = this->has_focus && GetAsyncKeyState(VK_CONTROL) < 0;
1001 _shift_pressed = this->has_focus && GetAsyncKeyState(VK_SHIFT) < 0;
1002
1003 /* Speedup when pressing tab, except when using ALT+TAB
1004 * to switch to another application. */
1005 this->fast_forward_key_pressed = this->has_focus && GetAsyncKeyState(VK_TAB) < 0 && GetAsyncKeyState(VK_MENU) >= 0;
1006
1007 /* Determine which directional keys are down. */
1008 if (this->has_focus) {
1009 _dirkeys =
1010 (GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
1011 (GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
1012 (GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
1013 (GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
1014 } else {
1015 _dirkeys = 0;
1016 }
1017
1018 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
1019}
1020
1022{
1023 MSG mesg;
1024
1025 if (!PeekMessage(&mesg, nullptr, 0, 0, PM_REMOVE)) return false;
1026
1027 /* Convert key messages to char messages if we want text input. */
1028 if (EditBoxInGlobalFocus()) TranslateMessage(&mesg);
1029 DispatchMessage(&mesg);
1030
1031 return true;
1032}
1033
1035{
1036 this->StartGameThread();
1037
1038 for (;;) {
1039 if (_exit_game) break;
1040
1041 this->Tick();
1042 this->SleepTillNextTick();
1043 }
1044
1045 this->StopGameThread();
1046}
1047
1054void VideoDriver_Win32Base::ClientSizeChanged(int w, int h, bool force)
1055{
1056 /* Allocate backing store of the new size. */
1057 if (this->AllocateBackingStore(w, h, force)) {
1059
1061
1063 }
1064}
1065
1067{
1068 if (_window_maximize) ShowWindow(this->main_wnd, SW_SHOWNORMAL);
1069
1070 this->width = this->width_org = w;
1071 this->height = this->height_org = h;
1072
1073 return this->MakeWindow(_fullscreen); // _wnd.fullscreen screws up ingame resolution switching
1074}
1075
1077{
1078 bool res = this->MakeWindow(full_screen);
1079
1081 return res;
1082}
1083
1090
1091static BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC, LPRECT, LPARAM data)
1092{
1093 auto &list = *reinterpret_cast<std::vector<int>*>(data);
1094
1095 MONITORINFOEX monitorInfo = {};
1096 monitorInfo.cbSize = sizeof(MONITORINFOEX);
1097 GetMonitorInfo(hMonitor, &monitorInfo);
1098
1099 DEVMODE devMode = {};
1100 devMode.dmSize = sizeof(DEVMODE);
1101 devMode.dmDriverExtra = 0;
1102 EnumDisplaySettings(monitorInfo.szDevice, ENUM_CURRENT_SETTINGS, &devMode);
1103
1104 if (devMode.dmDisplayFrequency != 0) list.push_back(devMode.dmDisplayFrequency);
1105 return true;
1106}
1107
1109{
1110 std::vector<int> rates = {};
1111 EnumDisplayMonitors(nullptr, nullptr, MonitorEnumProc, reinterpret_cast<LPARAM>(&rates));
1112 return rates;
1113}
1114
1116{
1117 return { static_cast<uint>(GetSystemMetrics(SM_CXSCREEN)), static_cast<uint>(GetSystemMetrics(SM_CYSCREEN)) };
1118}
1119
1121{
1122 if (this->buffer_locked) return false;
1123 this->buffer_locked = true;
1124
1125 _screen.dst_ptr = this->GetVideoPointer();
1126 assert(_screen.dst_ptr != nullptr);
1127
1128 return true;
1129}
1130
1132{
1133 assert(_screen.dst_ptr != nullptr);
1134 if (_screen.dst_ptr != nullptr) {
1135 /* Hand video buffer back to the drawing backend. */
1136 this->ReleaseVideoPointer();
1137 _screen.dst_ptr = nullptr;
1138 }
1139
1140 this->buffer_locked = false;
1141}
1142
1143
1144static FVideoDriver_Win32GDI iFVideoDriver_Win32GDI;
1145
1146std::optional<std::string_view> VideoDriver_Win32GDI::Start(const StringList &param)
1147{
1148 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
1149
1150 this->Initialize();
1151
1152 this->MakePalette();
1154 this->MakeWindow(_fullscreen);
1155
1157
1158 this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
1159
1160 return std::nullopt;
1161}
1162
1164{
1165 DeleteObject(this->gdi_palette);
1166 DeleteObject(this->dib_sect);
1167
1169}
1170
1172{
1174
1175 w = std::max(w, 64);
1176 h = std::max(h, 64);
1177
1178 if (!force && w == _screen.width && h == _screen.height) return false;
1179
1180 BITMAPINFO *bi = (BITMAPINFO *)new char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256]();
1181 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1182
1183 bi->bmiHeader.biWidth = this->width = w;
1184 bi->bmiHeader.biHeight = -(this->height = h);
1185
1186 bi->bmiHeader.biPlanes = 1;
1187 bi->bmiHeader.biBitCount = bpp;
1188 bi->bmiHeader.biCompression = BI_RGB;
1189
1190 if (this->dib_sect) DeleteObject(this->dib_sect);
1191
1192 HDC dc = GetDC(0);
1193 this->dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID **)&this->buffer_bits, nullptr, 0);
1194 if (this->dib_sect == nullptr) {
1195 delete[] bi;
1196 UserError("CreateDIBSection failed");
1197 }
1198 ReleaseDC(0, dc);
1199
1200 _screen.width = w;
1201 _screen.pitch = (bpp == 8) ? Align(w, 4) : w;
1202 _screen.height = h;
1203 _screen.dst_ptr = this->GetVideoPointer();
1204
1205 delete[] bi;
1206 return true;
1207}
1208
1210{
1211 assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
1212 return this->AllocateBackingStore(_screen.width, _screen.height, true) && this->MakeWindow(_fullscreen, false);
1213}
1214
1215void VideoDriver_Win32GDI::MakePalette()
1216{
1218
1219 LOGPALETTE *pal = (LOGPALETTE *)new char[sizeof(LOGPALETTE) + (256 - 1) * sizeof(PALETTEENTRY)]();
1220
1221 pal->palVersion = 0x300;
1222 pal->palNumEntries = 256;
1223
1224 for (uint i = 0; i != 256; i++) {
1225 pal->palPalEntry[i].peRed = _local_palette.palette[i].r;
1226 pal->palPalEntry[i].peGreen = _local_palette.palette[i].g;
1227 pal->palPalEntry[i].peBlue = _local_palette.palette[i].b;
1228 pal->palPalEntry[i].peFlags = 0;
1229
1230 }
1231 this->gdi_palette = CreatePalette(pal);
1232 delete[] pal;
1233 if (this->gdi_palette == nullptr) UserError("CreatePalette failed!\n");
1234}
1235
1236void VideoDriver_Win32GDI::UpdatePalette(HDC dc, uint start, uint count)
1237{
1238 RGBQUAD rgb[256];
1239
1240 for (uint i = 0; i != count; i++) {
1241 rgb[i].rgbRed = _local_palette.palette[start + i].r;
1242 rgb[i].rgbGreen = _local_palette.palette[start + i].g;
1243 rgb[i].rgbBlue = _local_palette.palette[start + i].b;
1244 rgb[i].rgbReserved = 0;
1245 }
1246
1247 SetDIBColorTable(dc, start, count, rgb);
1248}
1249
1251{
1252 HDC hDC = GetWindowDC(hWnd);
1253 HPALETTE hOldPalette = SelectPalette(hDC, this->gdi_palette, FALSE);
1254 UINT nChanged = RealizePalette(hDC);
1255
1256 SelectPalette(hDC, hOldPalette, TRUE);
1257 ReleaseDC(hWnd, hDC);
1258 if (nChanged != 0) this->MakeDirty(0, 0, _screen.width, _screen.height);
1259}
1260
1262{
1263 PerformanceMeasurer framerate(PFE_VIDEO);
1264
1265 if (IsEmptyRect(this->dirty_rect)) return;
1266
1267 HDC dc = GetDC(this->main_wnd);
1268 HDC dc2 = CreateCompatibleDC(dc);
1269
1270 HBITMAP old_bmp = (HBITMAP)SelectObject(dc2, this->dib_sect);
1271 HPALETTE old_palette = SelectPalette(dc, this->gdi_palette, FALSE);
1272
1273 if (_local_palette.count_dirty != 0) {
1275
1276 switch (blitter->UsePaletteAnimation()) {
1278 this->UpdatePalette(dc2, _local_palette.first_dirty, _local_palette.count_dirty);
1279 break;
1280
1283 break;
1284 }
1285
1287 break;
1288
1289 default:
1290 NOT_REACHED();
1291 }
1292 _local_palette.count_dirty = 0;
1293 }
1294
1295 BitBlt(dc, 0, 0, this->width, this->height, dc2, 0, 0, SRCCOPY);
1296 SelectPalette(dc, old_palette, TRUE);
1297 SelectObject(dc2, old_bmp);
1298 DeleteDC(dc2);
1299
1300 ReleaseDC(this->main_wnd, dc);
1301
1302 this->dirty_rect = {};
1303}
1304
1305#ifdef _DEBUG
1306/* Keep this function here..
1307 * It allows you to redraw the screen from within the MSVC debugger */
1308/* static */ int VideoDriver_Win32GDI::RedrawScreenDebug()
1309{
1310 static int _fooctr;
1311
1313
1314 _screen.dst_ptr = drv->GetVideoPointer();
1315 UpdateWindows();
1316
1317 drv->Paint();
1318 GdiFlush();
1319
1320 return _fooctr++;
1321}
1322#endif
1323
1324#ifdef WITH_OPENGL
1325
1326#ifndef PFD_SUPPORT_COMPOSITION
1327# define PFD_SUPPORT_COMPOSITION 0x00008000
1328#endif
1329
1330static PFNWGLCREATECONTEXTATTRIBSARBPROC _wglCreateContextAttribsARB = nullptr;
1331static PFNWGLSWAPINTERVALEXTPROC _wglSwapIntervalEXT = nullptr;
1332static bool _hasWGLARBCreateContextProfile = false;
1333
1339static OGLProc GetOGLProcAddressCallback(const char *proc)
1340{
1341 OGLProc ret = reinterpret_cast<OGLProc>(wglGetProcAddress(proc));
1342 if (ret == nullptr) {
1343 /* Non-extension GL function? Try normal loading. */
1344 ret = reinterpret_cast<OGLProc>(GetProcAddress(GetModuleHandle(L"opengl32"), proc));
1345 }
1346 return ret;
1347}
1348
1354static std::optional<std::string_view> SelectPixelFormat(HDC dc)
1355{
1356 PIXELFORMATDESCRIPTOR pfd = {
1357 sizeof(PIXELFORMATDESCRIPTOR), // Size of this struct.
1358 1, // Version of this struct.
1359 PFD_DRAW_TO_WINDOW | // Require window support.
1360 PFD_SUPPORT_OPENGL | // Require OpenGL support.
1361 PFD_DOUBLEBUFFER | // Use double buffering.
1362 PFD_DEPTH_DONTCARE,
1363 PFD_TYPE_RGBA, // Request RGBA format.
1364 24, // 24 bpp (excluding alpha).
1365 0, 0, 0, 0, 0, 0, 0, 0, // Colour bits and shift ignored.
1366 0, 0, 0, 0, 0, // No accumulation buffer.
1367 0, 0, // No depth/stencil buffer.
1368 0, // No aux buffers.
1369 PFD_MAIN_PLANE, // Main layer.
1370 0, 0, 0, 0 // Ignored/reserved.
1371 };
1372
1373 pfd.dwFlags |= PFD_SUPPORT_COMPOSITION; // Make OpenTTD compatible with Aero.
1374
1375 /* Choose a suitable pixel format. */
1376 int format = ChoosePixelFormat(dc, &pfd);
1377 if (format == 0) return "No suitable pixel format found";
1378 if (!SetPixelFormat(dc, format, &pfd)) return "Can't set pixel format";
1379
1380 return std::nullopt;
1381}
1382
1384static void LoadWGLExtensions()
1385{
1386 /* Querying the supported WGL extensions and loading the matching
1387 * functions requires a valid context, even for the extensions
1388 * regarding context creation. To get around this, we create
1389 * a dummy window with a dummy context. The extension functions
1390 * remain valid even after this context is destroyed. */
1391 HWND wnd = CreateWindow(L"STATIC", L"dummy", WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
1392 HDC dc = GetDC(wnd);
1393
1394 /* Set pixel format of the window. */
1395 if (SelectPixelFormat(dc) == std::nullopt) {
1396 /* Create rendering context. */
1397 HGLRC rc = wglCreateContext(dc);
1398 if (rc != nullptr) {
1399 wglMakeCurrent(dc, rc);
1400
1401#ifdef __MINGW32__
1402 /* GCC doesn't understand the expected usage of wglGetProcAddress(). */
1403#pragma GCC diagnostic push
1404#pragma GCC diagnostic ignored "-Wcast-function-type"
1405#endif /* __MINGW32__ */
1406
1407 /* Get list of WGL extensions. */
1408 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
1409 if (wglGetExtensionsStringARB != nullptr) {
1410 std::string_view wgl_exts = wglGetExtensionsStringARB(dc);
1411 /* Bind supported functions. */
1412 if (HasStringInExtensionList(wgl_exts, "WGL_ARB_create_context")) {
1413 _wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
1414 }
1415 _hasWGLARBCreateContextProfile = HasStringInExtensionList(wgl_exts, "WGL_ARB_create_context_profile");
1416 if (HasStringInExtensionList(wgl_exts, "WGL_EXT_swap_control")) {
1417 _wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
1418 }
1419 }
1420
1421#ifdef __MINGW32__
1422#pragma GCC diagnostic pop
1423#endif
1424 wglMakeCurrent(nullptr, nullptr);
1425 wglDeleteContext(rc);
1426 }
1427 }
1428
1429 ReleaseDC(wnd, dc);
1430 DestroyWindow(wnd);
1431}
1432
1433static FVideoDriver_Win32OpenGL iFVideoDriver_Win32OpenGL;
1434
1435std::optional<std::string_view> VideoDriver_Win32OpenGL::Start(const StringList &param)
1436{
1437 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
1438
1439 Dimension old_res = _cur_resolution; // Save current screen resolution in case of errors, as MakeWindow invalidates it.
1440
1441 LoadWGLExtensions();
1442
1443 this->Initialize();
1444 this->MakeWindow(_fullscreen);
1445
1446 /* Create and initialize OpenGL context. */
1447 auto err = this->AllocateContext();
1448 if (err) {
1449 this->Stop();
1450 _cur_resolution = old_res;
1451 return err;
1452 }
1453
1454 this->driver_info = GetName();
1455 this->driver_info += " (";
1456 this->driver_info += OpenGLBackend::Get()->GetDriverName();
1457 this->driver_info += ")";
1458
1459 this->ClientSizeChanged(this->width, this->height, true);
1460 /* We should have a valid screen buffer now. If not, something went wrong and we should abort. */
1461 if (_screen.dst_ptr == nullptr) {
1462 this->Stop();
1463 _cur_resolution = old_res;
1464 return "Can't get pointer to screen buffer";
1465 }
1466 /* Main loop expects to start with the buffer unmapped. */
1467 this->ReleaseVideoPointer();
1468
1470
1471 this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
1472
1473 return std::nullopt;
1474}
1475
1476void VideoDriver_Win32OpenGL::Stop()
1477{
1478 this->DestroyContext();
1480}
1481
1482void VideoDriver_Win32OpenGL::DestroyContext()
1483{
1485
1486 wglMakeCurrent(nullptr, nullptr);
1487 if (this->gl_rc != nullptr) {
1488 wglDeleteContext(this->gl_rc);
1489 this->gl_rc = nullptr;
1490 }
1491 if (this->dc != nullptr) {
1492 ReleaseDC(this->main_wnd, this->dc);
1493 this->dc = nullptr;
1494 }
1495}
1496
1497void VideoDriver_Win32OpenGL::ToggleVsync(bool vsync)
1498{
1499 if (_wglSwapIntervalEXT != nullptr) {
1500 _wglSwapIntervalEXT(vsync);
1501 } else if (vsync) {
1502 Debug(driver, 0, "OpenGL: Vsync requested, but not supported by driver");
1503 }
1504}
1505
1506std::optional<std::string_view> VideoDriver_Win32OpenGL::AllocateContext()
1507{
1508 this->dc = GetDC(this->main_wnd);
1509
1510 auto err = SelectPixelFormat(this->dc);
1511 if (err) return err;
1512
1513 HGLRC rc = nullptr;
1514
1515 /* Create OpenGL device context. Try to get an 3.2+ context if possible. */
1516 if (_wglCreateContextAttribsARB != nullptr) {
1517 /* Try for OpenGL 4.5 first. */
1518 int attribs[] = {
1519 WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
1520 WGL_CONTEXT_MINOR_VERSION_ARB, 5,
1521 WGL_CONTEXT_FLAGS_ARB, _debug_driver_level >= 8 ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
1522 _hasWGLARBCreateContextProfile ? WGL_CONTEXT_PROFILE_MASK_ARB : 0, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, // Terminate list if WGL_ARB_create_context_profile isn't supported.
1523 0
1524 };
1525 rc = _wglCreateContextAttribsARB(this->dc, nullptr, attribs);
1526
1527 if (rc == nullptr) {
1528 /* Try again for a 3.2 context. */
1529 attribs[1] = 3;
1530 attribs[3] = 2;
1531 rc = _wglCreateContextAttribsARB(this->dc, nullptr, attribs);
1532 }
1533 }
1534
1535 if (rc == nullptr) {
1536 /* Old OpenGL or old driver, let's hope for the best. */
1537 rc = wglCreateContext(this->dc);
1538 if (rc == nullptr) return "Can't create OpenGL context";
1539 }
1540 if (!wglMakeCurrent(this->dc, rc)) return "Can't activate GL context";
1541
1542 this->ToggleVsync(_video_vsync);
1543
1544 this->gl_rc = rc;
1545 return OpenGLBackend::Create(&GetOGLProcAddressCallback, this->GetScreenSize());
1546}
1547
1548bool VideoDriver_Win32OpenGL::ToggleFullscreen(bool full_screen)
1549{
1550 if (_screen.dst_ptr != nullptr) this->ReleaseVideoPointer();
1551 this->DestroyContext();
1552 bool res = this->VideoDriver_Win32Base::ToggleFullscreen(full_screen);
1553 res &= this->AllocateContext() == std::nullopt;
1554 this->ClientSizeChanged(this->width, this->height, true);
1555 return res;
1556}
1557
1558bool VideoDriver_Win32OpenGL::AfterBlitterChange()
1559{
1560 assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
1561 this->ClientSizeChanged(this->width, this->height, true);
1562 return true;
1563}
1564
1565void VideoDriver_Win32OpenGL::PopulateSystemSprites()
1566{
1567 OpenGLBackend::Get()->PopulateCursorCache();
1568}
1569
1570void VideoDriver_Win32OpenGL::ClearSystemSprites()
1571{
1573}
1574
1575bool VideoDriver_Win32OpenGL::AllocateBackingStore(int w, int h, bool force)
1576{
1577 if (!force && w == _screen.width && h == _screen.height) return false;
1578
1579 this->width = w = std::max(w, 64);
1580 this->height = h = std::max(h, 64);
1581
1582 if (this->gl_rc == nullptr) return false;
1583
1584 if (_screen.dst_ptr != nullptr) this->ReleaseVideoPointer();
1585
1586 this->dirty_rect = {};
1587 bool res = OpenGLBackend::Get()->Resize(w, h, force);
1588 SwapBuffers(this->dc);
1589 _screen.dst_ptr = this->GetVideoPointer();
1590
1591 return res;
1592}
1593
1594void *VideoDriver_Win32OpenGL::GetVideoPointer()
1595{
1596 if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
1597 this->anim_buffer = OpenGLBackend::Get()->GetAnimBuffer();
1598 }
1600}
1601
1602void VideoDriver_Win32OpenGL::ReleaseVideoPointer()
1603{
1604 if (this->anim_buffer != nullptr) OpenGLBackend::Get()->ReleaseAnimBuffer(this->dirty_rect);
1605 OpenGLBackend::Get()->ReleaseVideoBuffer(this->dirty_rect);
1606 this->dirty_rect = {};
1607 _screen.dst_ptr = nullptr;
1608 this->anim_buffer = nullptr;
1609}
1610
1611void VideoDriver_Win32OpenGL::Paint()
1612{
1613 PerformanceMeasurer framerate(PFE_VIDEO);
1614
1615 if (_local_palette.count_dirty != 0) {
1617
1618 /* Always push a changed palette to OpenGL. */
1619 OpenGLBackend::Get()->UpdatePalette(_local_palette.palette, _local_palette.first_dirty, _local_palette.count_dirty);
1622 }
1623
1624 _local_palette.count_dirty = 0;
1625 }
1626
1629
1630 SwapBuffers(this->dc);
1631}
1632
1633#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:139
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:1065
uint8_t * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
Definition opengl.cpp:1196
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
Definition opengl.cpp:1174
bool Resize(int w, int h, bool force=false)
Change the size of the drawing window and allocate matching resources.
Definition opengl.cpp:939
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:490
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
Definition opengl.cpp:1051
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
Definition opengl.cpp:1257
void ClearCursorCache()
Queue a request for cursor cache clear.
Definition opengl.cpp:1161
static OpenGLBackend * Get()
Get singleton instance of this class.
Definition opengl.h:86
void DrawMouseCursor()
Draw mouse cursor on screen.
Definition opengl.cpp:1097
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
Definition opengl.cpp:1219
static void Destroy()
Free resources and destroy singleton back-end class.
Definition opengl.cpp:503
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:1084
void CheckPaletteAnim() override
Process any pending palette animation.
Definition win32_v.cpp:990
void Stop() override
Stop this driver.
Definition win32_v.cpp:977
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:984
bool PollEvent() override
Process a single system event.
Definition win32_v.cpp:1021
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:1120
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition win32_v.cpp:1108
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:996
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:1115
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:1131
void ClientSizeChanged(int w, int h, bool force=false)
Indicate to the driver the client-size might have changed.
Definition win32_v.cpp:1054
void MainLoop() override
Perform the actual drawing.
Definition win32_v.cpp:1034
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:1076
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition win32_v.cpp:1066
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:1250
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition win32_v.cpp:1146
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:1171
void Paint() override
Paint the window.
Definition win32_v.cpp:1261
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition win32_v.cpp:1209
void Stop() override
Stop this driver.
Definition win32_v.cpp:1163
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:49
#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:2722
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition window.cpp:3147
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:2985
void HandleKeypress(uint keycode, char32_t key)
Handle keyboard input.
Definition window.cpp:2666
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:2758
@ 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:230
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:386
std::wstring OTTD2FS(std::string_view name)
Convert from OpenTTD's encoding to a wide string.
Definition win32.cpp:368
std::string FS2OTTD(std::wstring_view name)
Convert to OpenTTD's encoding from a wide string.
Definition win32.cpp:352
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:258
static bool DrawIMECompositionString()
Should we draw the composition string ourself, i.e is this a normal IME?
Definition win32_v.cpp:288
static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
Handle WM_IME_COMPOSITION messages.
Definition win32_v.cpp:362
static void SetCandidatePos(HWND hwnd)
Set the position of the candidate window.
Definition win32_v.cpp:322
static void CancelIMEComposition(HWND hwnd)
Clear the current composition string.
Definition win32_v.cpp:427
static void SetCompositionPos(HWND hwnd)
Set position of the composition window to the caret position.
Definition win32_v.cpp:297
Base of the Windows video driver.
Window * _focused_window
Window that currently has focus.
Definition window.cpp:85
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Definition window.cpp:3438
bool EditBoxInGlobalFocus()
Check if an edit box is in global focus.
Definition window.cpp:461
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:3339
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: