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