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