OpenTTD Source: src/video/win32_v.cpp Source File

OpenTTD Source 13.2.1

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