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