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