OpenTTD Source  1.11.0-beta1
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 "../network/network.h"
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 <mutex>
29 #include <condition_variable>
30 
31 #include "../safeguards.h"
32 
33 /* Missing define in MinGW headers. */
34 #ifndef MAPVK_VK_TO_CHAR
35 #define MAPVK_VK_TO_CHAR (2)
36 #endif
37 
38 #ifndef PM_QS_INPUT
39 #define PM_QS_INPUT 0x20000
40 #endif
41 
42 static struct {
43  HWND main_wnd;
44  HBITMAP dib_sect;
45  void *buffer_bits;
46  HPALETTE gdi_palette;
47  RECT update_rect;
48  int width;
49  int height;
50  int width_org;
51  int height_org;
52  bool fullscreen;
53  bool has_focus;
54  bool running;
55 } _wnd;
56 
57 bool _force_full_redraw;
58 bool _window_maximize;
59 uint _display_hz;
60 static Dimension _bck_resolution;
61 DWORD _imm_props;
62 
64 static bool _draw_threaded;
66 static std::recursive_mutex *_draw_mutex = nullptr;
68 static std::condition_variable_any *_draw_signal = nullptr;
70 static volatile bool _draw_continue;
73 
74 static void MakePalette()
75 {
79 
80  LOGPALETTE *pal = (LOGPALETTE*)alloca(sizeof(LOGPALETTE) + (256 - 1) * sizeof(PALETTEENTRY));
81 
82  pal->palVersion = 0x300;
83  pal->palNumEntries = 256;
84 
85  for (uint i = 0; i != 256; i++) {
86  pal->palPalEntry[i].peRed = _local_palette.palette[i].r;
87  pal->palPalEntry[i].peGreen = _local_palette.palette[i].g;
88  pal->palPalEntry[i].peBlue = _local_palette.palette[i].b;
89  pal->palPalEntry[i].peFlags = 0;
90 
91  }
92  _wnd.gdi_palette = CreatePalette(pal);
93  if (_wnd.gdi_palette == nullptr) usererror("CreatePalette failed!\n");
94 }
95 
96 static void UpdatePalette(HDC dc, uint start, uint count)
97 {
98  RGBQUAD rgb[256];
99  uint i;
100 
101  for (i = 0; i != count; i++) {
102  rgb[i].rgbRed = _local_palette.palette[start + i].r;
103  rgb[i].rgbGreen = _local_palette.palette[start + i].g;
104  rgb[i].rgbBlue = _local_palette.palette[start + i].b;
105  rgb[i].rgbReserved = 0;
106  }
107 
108  SetDIBColorTable(dc, start, count, rgb);
109 }
110 
111 bool VideoDriver_Win32::ClaimMousePointer()
112 {
113  MyShowCursor(false, true);
114  return true;
115 }
116 
117 struct VkMapping {
118  byte vk_from;
119  byte vk_count;
120  byte map_to;
121 };
122 
123 #define AS(x, z) {x, 0, z}
124 #define AM(x, y, z, w) {x, y - x, z}
125 
126 static const VkMapping _vk_mapping[] = {
127  /* Pageup stuff + up/down */
128  AM(VK_PRIOR, VK_DOWN, WKC_PAGEUP, WKC_DOWN),
129  /* Map letters & digits */
130  AM('A', 'Z', 'A', 'Z'),
131  AM('0', '9', '0', '9'),
132 
133  AS(VK_ESCAPE, WKC_ESC),
134  AS(VK_PAUSE, WKC_PAUSE),
135  AS(VK_BACK, WKC_BACKSPACE),
136  AM(VK_INSERT, VK_DELETE, WKC_INSERT, WKC_DELETE),
137 
138  AS(VK_SPACE, WKC_SPACE),
139  AS(VK_RETURN, WKC_RETURN),
140  AS(VK_TAB, WKC_TAB),
141 
142  /* Function keys */
143  AM(VK_F1, VK_F12, WKC_F1, WKC_F12),
144 
145  /* Numeric part */
146  AM(VK_NUMPAD0, VK_NUMPAD9, '0', '9'),
147  AS(VK_DIVIDE, WKC_NUM_DIV),
148  AS(VK_MULTIPLY, WKC_NUM_MUL),
149  AS(VK_SUBTRACT, WKC_NUM_MINUS),
150  AS(VK_ADD, WKC_NUM_PLUS),
151  AS(VK_DECIMAL, WKC_NUM_DECIMAL),
152 
153  /* Other non-letter keys */
154  AS(0xBF, WKC_SLASH),
155  AS(0xBA, WKC_SEMICOLON),
156  AS(0xBB, WKC_EQUALS),
157  AS(0xDB, WKC_L_BRACKET),
158  AS(0xDC, WKC_BACKSLASH),
159  AS(0xDD, WKC_R_BRACKET),
160 
161  AS(0xDE, WKC_SINGLEQUOTE),
162  AS(0xBC, WKC_COMMA),
163  AS(0xBD, WKC_MINUS),
164  AS(0xBE, WKC_PERIOD)
165 };
166 
167 static uint MapWindowsKey(uint sym)
168 {
169  const VkMapping *map;
170  uint key = 0;
171 
172  for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
173  if ((uint)(sym - map->vk_from) <= map->vk_count) {
174  key = sym - map->vk_from + map->map_to;
175  break;
176  }
177  }
178 
179  if (GetAsyncKeyState(VK_SHIFT) < 0) key |= WKC_SHIFT;
180  if (GetAsyncKeyState(VK_CONTROL) < 0) key |= WKC_CTRL;
181  if (GetAsyncKeyState(VK_MENU) < 0) key |= WKC_ALT;
182  return key;
183 }
184 
185 static bool AllocateDibSection(int w, int h, bool force = false);
186 
187 static void ClientSizeChanged(int w, int h)
188 {
189  /* allocate new dib section of the new size */
190  if (AllocateDibSection(w, h)) {
191  /* mark all palette colours dirty */
195 
197 
198  GameSizeChanged();
199  }
200 }
201 
202 #ifdef _DEBUG
203 /* Keep this function here..
204  * It allows you to redraw the screen from within the MSVC debugger */
205 int RedrawScreenDebug()
206 {
207  HDC dc, dc2;
208  static int _fooctr;
209  HBITMAP old_bmp;
210  HPALETTE old_palette;
211 
212  UpdateWindows();
213 
214  dc = GetDC(_wnd.main_wnd);
215  dc2 = CreateCompatibleDC(dc);
216 
217  old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect);
218  old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE);
219  BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY);
220  SelectPalette(dc, old_palette, TRUE);
221  SelectObject(dc2, old_bmp);
222  DeleteDC(dc2);
223  ReleaseDC(_wnd.main_wnd, dc);
224 
225  return _fooctr++;
226 }
227 #endif
228 
234 bool VideoDriver_Win32::MakeWindow(bool full_screen)
235 {
236  /* full_screen is whether the new window should be fullscreen,
237  * _wnd.fullscreen is whether the current window is. */
238  _fullscreen = full_screen;
239 
240  /* recreate window? */
241  if ((full_screen || _wnd.fullscreen) && _wnd.main_wnd) {
242  DestroyWindow(_wnd.main_wnd);
243  _wnd.main_wnd = 0;
244  }
245 
246  if (full_screen) {
247  DEVMODE settings;
248 
249  memset(&settings, 0, sizeof(settings));
250  settings.dmSize = sizeof(settings);
251  settings.dmFields =
252  DM_BITSPERPEL |
253  DM_PELSWIDTH |
254  DM_PELSHEIGHT |
255  (_display_hz != 0 ? DM_DISPLAYFREQUENCY : 0);
257  settings.dmPelsWidth = _wnd.width_org;
258  settings.dmPelsHeight = _wnd.height_org;
259  settings.dmDisplayFrequency = _display_hz;
260 
261  /* Check for 8 bpp support. */
262  if (settings.dmBitsPerPel == 8 &&
263  (_support8bpp != S8BPP_HARDWARE || ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL)) {
264  settings.dmBitsPerPel = 32;
265  }
266 
267  /* Test fullscreen with current resolution, if it fails use desktop resolution. */
268  if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
269  RECT r;
270  GetWindowRect(GetDesktopWindow(), &r);
271  /* Guard against recursion. If we already failed here once, just fall through to
272  * the next ChangeDisplaySettings call which will fail and error out appropriately. */
273  if ((int)settings.dmPelsWidth != r.right - r.left || (int)settings.dmPelsHeight != r.bottom - r.top) {
274  return this->ChangeResolution(r.right - r.left, r.bottom - r.top);
275  }
276  }
277 
278  if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
279  this->MakeWindow(false); // don't care about the result
280  return false; // the request failed
281  }
282  } else if (_wnd.fullscreen) {
283  /* restore display? */
284  ChangeDisplaySettings(nullptr, 0);
285  /* restore the resolution */
286  _wnd.width = _bck_resolution.width;
287  _wnd.height = _bck_resolution.height;
288  }
289 
290  {
291  RECT r;
292  DWORD style, showstyle;
293  int w, h;
294 
295  showstyle = SW_SHOWNORMAL;
296  _wnd.fullscreen = full_screen;
297  if (_wnd.fullscreen) {
298  style = WS_POPUP;
299  SetRect(&r, 0, 0, _wnd.width_org, _wnd.height_org);
300  } else {
301  style = WS_OVERLAPPEDWINDOW;
302  /* On window creation, check if we were in maximize mode before */
303  if (_window_maximize) showstyle = SW_SHOWMAXIMIZED;
304  SetRect(&r, 0, 0, _wnd.width, _wnd.height);
305  }
306 
307  AdjustWindowRect(&r, style, FALSE);
308  w = r.right - r.left;
309  h = r.bottom - r.top;
310 
311  if (_wnd.main_wnd != nullptr) {
312  if (!_window_maximize) SetWindowPos(_wnd.main_wnd, 0, 0, 0, w, h, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
313  } else {
314  int x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
315  int y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
316 
317  char window_title[64];
318  seprintf(window_title, lastof(window_title), "OpenTTD %s", _openttd_revision);
319 
320  _wnd.main_wnd = CreateWindow(_T("OTTD"), MB_TO_WIDE(window_title), style, x, y, w, h, 0, 0, GetModuleHandle(nullptr), 0);
321  if (_wnd.main_wnd == nullptr) usererror("CreateWindow failed");
322  ShowWindow(_wnd.main_wnd, showstyle);
323  }
324  }
325 
327 
328  GameSizeChanged(); // invalidate all windows, force redraw
329  return true; // the request succeeded
330 }
331 
333 static void PaintWindow(HDC dc)
334 {
335  PerformanceMeasurer framerate(PFE_VIDEO);
336 
337  HDC dc2 = CreateCompatibleDC(dc);
338  HBITMAP old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect);
339  HPALETTE old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE);
340 
341  if (_cur_palette.count_dirty != 0) {
343 
344  switch (blitter->UsePaletteAnimation()) {
347  break;
348 
350  blitter->PaletteAnimate(_local_palette);
351  break;
352 
354  break;
355 
356  default:
357  NOT_REACHED();
358  }
360  }
361 
362  BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY);
363  SelectPalette(dc, old_palette, TRUE);
364  SelectObject(dc2, old_bmp);
365  DeleteDC(dc2);
366 }
367 
368 static void PaintWindowThread()
369 {
370  /* First tell the main thread we're started */
371  std::unique_lock<std::recursive_mutex> lock(*_draw_mutex);
372  _draw_signal->notify_one();
373 
374  /* Now wait for the first thing to draw! */
375  _draw_signal->wait(*_draw_mutex);
376 
377  while (_draw_continue) {
378  /* Convert update region from logical to device coordinates. */
379  POINT pt = {0, 0};
380  ClientToScreen(_wnd.main_wnd, &pt);
381  OffsetRect(&_wnd.update_rect, pt.x, pt.y);
382 
383  /* Create a device context that is clipped to the region we need to draw.
384  * GetDCEx 'consumes' the update region, so we may not destroy it ourself. */
385  HRGN rgn = CreateRectRgnIndirect(&_wnd.update_rect);
386  HDC dc = GetDCEx(_wnd.main_wnd, rgn, DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_INTERSECTRGN);
387 
388  PaintWindow(dc);
389 
390  /* Clear update rect. */
391  SetRectEmpty(&_wnd.update_rect);
392  ReleaseDC(_wnd.main_wnd, dc);
393 
394  /* Flush GDI buffer to ensure drawing here doesn't conflict with any GDI usage in the main WndProc. */
395  GdiFlush();
396 
397  _draw_signal->wait(*_draw_mutex);
398  }
399 }
400 
402 static LRESULT HandleCharMsg(uint keycode, WChar charcode)
403 {
404 #if !defined(UNICODE)
405  static char prev_char = 0;
406 
407  char input[2] = {(char)charcode, 0};
408  int input_len = 1;
409 
410  if (prev_char != 0) {
411  /* We stored a lead byte previously, combine it with this byte. */
412  input[0] = prev_char;
413  input[1] = (char)charcode;
414  input_len = 2;
415  } else if (IsDBCSLeadByte(charcode)) {
416  /* We got a lead byte, store and exit. */
417  prev_char = charcode;
418  return 0;
419  }
420  prev_char = 0;
421 
422  wchar_t w[2]; // Can get up to two code points as a result.
423  int len = MultiByteToWideChar(CP_ACP, 0, input, input_len, w, 2);
424  switch (len) {
425  case 1: // Normal unicode character.
426  charcode = w[0];
427  break;
428 
429  case 2: // Got an UTF-16 surrogate pair back.
430  charcode = Utf16DecodeSurrogate(w[0], w[1]);
431  break;
432 
433  default: // Some kind of error.
434  DEBUG(driver, 1, "Invalid DBCS character sequence encountered, dropping input");
435  charcode = 0;
436  break;
437  }
438 #else
439  static WChar prev_char = 0;
440 
441  /* Did we get a lead surrogate? If yes, store and exit. */
442  if (Utf16IsLeadSurrogate(charcode)) {
443  if (prev_char != 0) DEBUG(driver, 1, "Got two UTF-16 lead surrogates, dropping the first one");
444  prev_char = charcode;
445  return 0;
446  }
447 
448  /* Stored lead surrogate and incoming trail surrogate? Combine and forward to input handling. */
449  if (prev_char != 0) {
450  if (Utf16IsTrailSurrogate(charcode)) {
451  charcode = Utf16DecodeSurrogate(prev_char, charcode);
452  } else {
453  DEBUG(driver, 1, "Got an UTF-16 lead surrogate without a trail surrogate, dropping the lead surrogate");
454  }
455  }
456  prev_char = 0;
457 #endif /* UNICODE */
458 
459  HandleKeypress(keycode, charcode);
460 
461  return 0;
462 }
463 
466 {
467  return (_imm_props & IME_PROP_AT_CARET) && !(_imm_props & IME_PROP_SPECIAL_UI);
468 }
469 
471 static void SetCompositionPos(HWND hwnd)
472 {
473  HIMC hIMC = ImmGetContext(hwnd);
474  if (hIMC != NULL) {
475  COMPOSITIONFORM cf;
476  cf.dwStyle = CFS_POINT;
477 
478  if (EditBoxInGlobalFocus()) {
479  /* Get caret position. */
480  Point pt = _focused_window->GetCaretPosition();
481  cf.ptCurrentPos.x = _focused_window->left + pt.x;
482  cf.ptCurrentPos.y = _focused_window->top + pt.y;
483  } else {
484  cf.ptCurrentPos.x = 0;
485  cf.ptCurrentPos.y = 0;
486  }
487  ImmSetCompositionWindow(hIMC, &cf);
488  }
489  ImmReleaseContext(hwnd, hIMC);
490 }
491 
493 static void SetCandidatePos(HWND hwnd)
494 {
495  HIMC hIMC = ImmGetContext(hwnd);
496  if (hIMC != NULL) {
497  CANDIDATEFORM cf;
498  cf.dwIndex = 0;
499  cf.dwStyle = CFS_EXCLUDE;
500 
501  if (EditBoxInGlobalFocus()) {
502  Point pt = _focused_window->GetCaretPosition();
503  cf.ptCurrentPos.x = _focused_window->left + pt.x;
504  cf.ptCurrentPos.y = _focused_window->top + pt.y;
505  if (_focused_window->window_class == WC_CONSOLE) {
506  cf.rcArea.left = _focused_window->left;
507  cf.rcArea.top = _focused_window->top;
508  cf.rcArea.right = _focused_window->left + _focused_window->width;
509  cf.rcArea.bottom = _focused_window->top + _focused_window->height;
510  } else {
511  cf.rcArea.left = _focused_window->left + _focused_window->nested_focus->pos_x;
512  cf.rcArea.top = _focused_window->top + _focused_window->nested_focus->pos_y;
513  cf.rcArea.right = cf.rcArea.left + _focused_window->nested_focus->current_x;
514  cf.rcArea.bottom = cf.rcArea.top + _focused_window->nested_focus->current_y;
515  }
516  } else {
517  cf.ptCurrentPos.x = 0;
518  cf.ptCurrentPos.y = 0;
519  SetRectEmpty(&cf.rcArea);
520  }
521  ImmSetCandidateWindow(hIMC, &cf);
522  }
523  ImmReleaseContext(hwnd, hIMC);
524 }
525 
527 static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
528 {
529  HIMC hIMC = ImmGetContext(hwnd);
530 
531  if (hIMC != NULL) {
532  if (lParam & GCS_RESULTSTR) {
533  /* Read result string from the IME. */
534  LONG len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, nullptr, 0); // Length is always in bytes, even in UNICODE build.
535  TCHAR *str = (TCHAR *)_alloca(len + sizeof(TCHAR));
536  len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, str, len);
537  str[len / sizeof(TCHAR)] = '\0';
538 
539  /* Transmit text to windowing system. */
540  if (len > 0) {
541  HandleTextInput(nullptr, true); // Clear marked string.
542  HandleTextInput(FS2OTTD(str));
543  }
544  SetCompositionPos(hwnd);
545 
546  /* Don't pass the result string on to the default window proc. */
547  lParam &= ~(GCS_RESULTSTR | GCS_RESULTCLAUSE | GCS_RESULTREADCLAUSE | GCS_RESULTREADSTR);
548  }
549 
550  if ((lParam & GCS_COMPSTR) && DrawIMECompositionString()) {
551  /* Read composition string from the IME. */
552  LONG len = ImmGetCompositionString(hIMC, GCS_COMPSTR, nullptr, 0); // Length is always in bytes, even in UNICODE build.
553  TCHAR *str = (TCHAR *)_alloca(len + sizeof(TCHAR));
554  len = ImmGetCompositionString(hIMC, GCS_COMPSTR, str, len);
555  str[len / sizeof(TCHAR)] = '\0';
556 
557  if (len > 0) {
558  static char utf8_buf[1024];
559  convert_from_fs(str, utf8_buf, lengthof(utf8_buf));
560 
561  /* Convert caret position from bytes in the input string to a position in the UTF-8 encoded string. */
562  LONG caret_bytes = ImmGetCompositionString(hIMC, GCS_CURSORPOS, nullptr, 0);
563  const char *caret = utf8_buf;
564  for (const TCHAR *c = str; *c != '\0' && *caret != '\0' && caret_bytes > 0; c++, caret_bytes--) {
565  /* Skip DBCS lead bytes or leading surrogates. */
566 #ifdef UNICODE
567  if (Utf16IsLeadSurrogate(*c)) {
568 #else
569  if (IsDBCSLeadByte(*c)) {
570 #endif
571  c++;
572  caret_bytes--;
573  }
574  Utf8Consume(&caret);
575  }
576 
577  HandleTextInput(utf8_buf, true, caret);
578  } else {
579  HandleTextInput(nullptr, true);
580  }
581 
582  lParam &= ~(GCS_COMPSTR | GCS_COMPATTR | GCS_COMPCLAUSE | GCS_CURSORPOS | GCS_DELTASTART);
583  }
584  }
585  ImmReleaseContext(hwnd, hIMC);
586 
587  return lParam != 0 ? DefWindowProc(hwnd, WM_IME_COMPOSITION, wParam, lParam) : 0;
588 }
589 
591 static void CancelIMEComposition(HWND hwnd)
592 {
593  HIMC hIMC = ImmGetContext(hwnd);
594  if (hIMC != NULL) ImmNotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
595  ImmReleaseContext(hwnd, hIMC);
596  /* Clear any marked string from the current edit box. */
597  HandleTextInput(nullptr, true);
598 }
599 
600 static LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
601 {
602  static uint32 keycode = 0;
603  static bool console = false;
604  static bool in_sizemove = false;
605 
606  switch (msg) {
607  case WM_CREATE:
608  _cursor.in_window = false; // Win32 has mouse tracking.
609  SetCompositionPos(hwnd);
610  _imm_props = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
611  break;
612 
613  case WM_ENTERSIZEMOVE:
614  in_sizemove = true;
615  break;
616 
617  case WM_EXITSIZEMOVE:
618  in_sizemove = false;
619  break;
620 
621  case WM_PAINT:
622  if (!in_sizemove && _draw_mutex != nullptr && !HasModalProgress()) {
623  /* Get the union of the old update rect and the new update rect. */
624  RECT r;
625  GetUpdateRect(hwnd, &r, FALSE);
626  UnionRect(&_wnd.update_rect, &_wnd.update_rect, &r);
627 
628  /* Mark the window as updated, otherwise Windows would send more WM_PAINT messages. */
629  ValidateRect(hwnd, nullptr);
630  _draw_signal->notify_one();
631  } else {
632  PAINTSTRUCT ps;
633 
634  BeginPaint(hwnd, &ps);
635  PaintWindow(ps.hdc);
636  EndPaint(hwnd, &ps);
637  }
638  return 0;
639 
640  case WM_PALETTECHANGED:
641  if ((HWND)wParam == hwnd) return 0;
642  FALLTHROUGH;
643 
644  case WM_QUERYNEWPALETTE: {
645  HDC hDC = GetWindowDC(hwnd);
646  HPALETTE hOldPalette = SelectPalette(hDC, _wnd.gdi_palette, FALSE);
647  UINT nChanged = RealizePalette(hDC);
648 
649  SelectPalette(hDC, hOldPalette, TRUE);
650  ReleaseDC(hwnd, hDC);
651  if (nChanged != 0) InvalidateRect(hwnd, nullptr, FALSE);
652  return 0;
653  }
654 
655  case WM_CLOSE:
656  HandleExitGameRequest();
657  return 0;
658 
659  case WM_DESTROY:
660  if (_window_maximize) _cur_resolution = _bck_resolution;
661  return 0;
662 
663  case WM_LBUTTONDOWN:
664  SetCapture(hwnd);
665  _left_button_down = true;
667  return 0;
668 
669  case WM_LBUTTONUP:
670  ReleaseCapture();
671  _left_button_down = false;
672  _left_button_clicked = false;
674  return 0;
675 
676  case WM_RBUTTONDOWN:
677  SetCapture(hwnd);
678  _right_button_down = true;
679  _right_button_clicked = true;
681  return 0;
682 
683  case WM_RBUTTONUP:
684  ReleaseCapture();
685  _right_button_down = false;
687  return 0;
688 
689  case WM_MOUSELEAVE:
690  UndrawMouseCursor();
691  _cursor.in_window = false;
692 
693  if (!_left_button_down && !_right_button_down) MyShowCursor(true);
694  return 0;
695 
696  case WM_MOUSEMOVE: {
697  int x = (int16)LOWORD(lParam);
698  int y = (int16)HIWORD(lParam);
699 
700  /* If the mouse was not in the window and it has moved it means it has
701  * come into the window, so start drawing the mouse. Also start
702  * tracking the mouse for exiting the window */
703  if (!_cursor.in_window) {
704  _cursor.in_window = true;
705  TRACKMOUSEEVENT tme;
706  tme.cbSize = sizeof(tme);
707  tme.dwFlags = TME_LEAVE;
708  tme.hwndTrack = hwnd;
709 
710  TrackMouseEvent(&tme);
711  }
712 
713  if (_cursor.fix_at) {
714  /* Get all queued mouse events now in case we have to warp the cursor. In the
715  * end, we only care about the current mouse position and not bygone events. */
716  MSG m;
717  while (PeekMessage(&m, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE | PM_NOYIELD | PM_QS_INPUT)) {
718  x = (int16)LOWORD(m.lParam);
719  y = (int16)HIWORD(m.lParam);
720  }
721  }
722 
723  if (_cursor.UpdateCursorPosition(x, y, false)) {
724  POINT pt;
725  pt.x = _cursor.pos.x;
726  pt.y = _cursor.pos.y;
727  ClientToScreen(hwnd, &pt);
728  SetCursorPos(pt.x, pt.y);
729  }
730  MyShowCursor(false);
732  return 0;
733  }
734 
735  case WM_INPUTLANGCHANGE:
736  _imm_props = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
737  break;
738 
739  case WM_IME_SETCONTEXT:
740  /* Don't show the composition window if we draw the string ourself. */
741  if (DrawIMECompositionString()) lParam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
742  break;
743 
744  case WM_IME_STARTCOMPOSITION:
745  SetCompositionPos(hwnd);
746  if (DrawIMECompositionString()) return 0;
747  break;
748 
749  case WM_IME_COMPOSITION:
750  return HandleIMEComposition(hwnd, wParam, lParam);
751 
752  case WM_IME_ENDCOMPOSITION:
753  /* Clear any pending composition string. */
754  HandleTextInput(nullptr, true);
755  if (DrawIMECompositionString()) return 0;
756  break;
757 
758  case WM_IME_NOTIFY:
759  if (wParam == IMN_OPENCANDIDATE) SetCandidatePos(hwnd);
760  break;
761 
762 #if !defined(UNICODE)
763  case WM_IME_CHAR:
764  if (GB(wParam, 8, 8) != 0) {
765  /* DBCS character, send lead byte first. */
766  HandleCharMsg(0, GB(wParam, 8, 8));
767  }
768  HandleCharMsg(0, GB(wParam, 0, 8));
769  return 0;
770 #endif
771 
772  case WM_DEADCHAR:
773  console = GB(lParam, 16, 8) == 41;
774  return 0;
775 
776  case WM_CHAR: {
777  uint scancode = GB(lParam, 16, 8);
778  uint charcode = wParam;
779 
780  /* If the console key is a dead-key, we need to press it twice to get a WM_CHAR message.
781  * But we then get two WM_CHAR messages, so ignore the first one */
782  if (console && scancode == 41) {
783  console = false;
784  return 0;
785  }
786 
787  /* IMEs and other input methods sometimes send a WM_CHAR without a WM_KEYDOWN,
788  * clear the keycode so a previous WM_KEYDOWN doesn't become 'stuck'. */
789  uint cur_keycode = keycode;
790  keycode = 0;
791 
792  return HandleCharMsg(cur_keycode, charcode);
793  }
794 
795  case WM_KEYDOWN: {
796  /* No matter the keyboard layout, we will map the '~' to the console. */
797  uint scancode = GB(lParam, 16, 8);
798  keycode = scancode == 41 ? (uint)WKC_BACKQUOTE : MapWindowsKey(wParam);
799 
800  /* Silently drop all messages handled by WM_CHAR. */
801  MSG msg;
802  if (PeekMessage(&msg, nullptr, 0, 0, PM_NOREMOVE)) {
803  if ((msg.message == WM_CHAR || msg.message == WM_DEADCHAR) && GB(lParam, 16, 8) == GB(msg.lParam, 16, 8)) {
804  return 0;
805  }
806  }
807 
808  uint charcode = MapVirtualKey(wParam, MAPVK_VK_TO_CHAR);
809 
810  /* No character translation? */
811  if (charcode == 0) {
812  HandleKeypress(keycode, 0);
813  return 0;
814  }
815 
816  /* Is the console key a dead key? If yes, ignore the first key down event. */
817  if (HasBit(charcode, 31) && !console) {
818  if (scancode == 41) {
819  console = true;
820  return 0;
821  }
822  }
823  console = false;
824 
825  /* IMEs and other input methods sometimes send a WM_CHAR without a WM_KEYDOWN,
826  * clear the keycode so a previous WM_KEYDOWN doesn't become 'stuck'. */
827  uint cur_keycode = keycode;
828  keycode = 0;
829 
830  return HandleCharMsg(cur_keycode, LOWORD(charcode));
831  }
832 
833  case WM_SYSKEYDOWN: // user presses F10 or Alt, both activating the title-menu
834  switch (wParam) {
835  case VK_RETURN:
836  case 'F': // Full Screen on ALT + ENTER/F
837  ToggleFullScreen(!_wnd.fullscreen);
838  return 0;
839 
840  case VK_MENU: // Just ALT
841  return 0; // do nothing
842 
843  case VK_F10: // F10, ignore activation of menu
844  HandleKeypress(MapWindowsKey(wParam), 0);
845  return 0;
846 
847  default: // ALT in combination with something else
848  HandleKeypress(MapWindowsKey(wParam), 0);
849  break;
850  }
851  break;
852 
853  case WM_SIZE:
854  if (wParam != SIZE_MINIMIZED) {
855  /* Set maximized flag when we maximize (obviously), but also when we
856  * switched to fullscreen from a maximized state */
857  _window_maximize = (wParam == SIZE_MAXIMIZED || (_window_maximize && _fullscreen));
858  if (_window_maximize || _fullscreen) _bck_resolution = _cur_resolution;
859  ClientSizeChanged(LOWORD(lParam), HIWORD(lParam));
860  }
861  return 0;
862 
863  case WM_SIZING: {
864  RECT *r = (RECT*)lParam;
865  RECT r2;
866  int w, h;
867 
868  SetRect(&r2, 0, 0, 0, 0);
869  AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
870 
871  w = r->right - r->left - (r2.right - r2.left);
872  h = r->bottom - r->top - (r2.bottom - r2.top);
873  w = std::max(w, 64);
874  h = std::max(h, 64);
875  SetRect(&r2, 0, 0, w, h);
876 
877  AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
878  w = r2.right - r2.left;
879  h = r2.bottom - r2.top;
880 
881  switch (wParam) {
882  case WMSZ_BOTTOM:
883  r->bottom = r->top + h;
884  break;
885 
886  case WMSZ_BOTTOMLEFT:
887  r->bottom = r->top + h;
888  r->left = r->right - w;
889  break;
890 
891  case WMSZ_BOTTOMRIGHT:
892  r->bottom = r->top + h;
893  r->right = r->left + w;
894  break;
895 
896  case WMSZ_LEFT:
897  r->left = r->right - w;
898  break;
899 
900  case WMSZ_RIGHT:
901  r->right = r->left + w;
902  break;
903 
904  case WMSZ_TOP:
905  r->top = r->bottom - h;
906  break;
907 
908  case WMSZ_TOPLEFT:
909  r->top = r->bottom - h;
910  r->left = r->right - w;
911  break;
912 
913  case WMSZ_TOPRIGHT:
914  r->top = r->bottom - h;
915  r->right = r->left + w;
916  break;
917  }
918  return TRUE;
919  }
920 
921 /* needed for wheel */
922 #if !defined(WM_MOUSEWHEEL)
923 # define WM_MOUSEWHEEL 0x020A
924 #endif /* WM_MOUSEWHEEL */
925 #if !defined(GET_WHEEL_DELTA_WPARAM)
926 # define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam))
927 #endif /* GET_WHEEL_DELTA_WPARAM */
928 
929  case WM_MOUSEWHEEL: {
930  int delta = GET_WHEEL_DELTA_WPARAM(wParam);
931 
932  if (delta < 0) {
933  _cursor.wheel++;
934  } else if (delta > 0) {
935  _cursor.wheel--;
936  }
938  return 0;
939  }
940 
941  case WM_SETFOCUS:
942  _wnd.has_focus = true;
943  SetCompositionPos(hwnd);
944  break;
945 
946  case WM_KILLFOCUS:
947  _wnd.has_focus = false;
948  break;
949 
950  case WM_ACTIVATE: {
951  /* Don't do anything if we are closing openttd */
952  if (_exit_game) break;
953 
954  bool active = (LOWORD(wParam) != WA_INACTIVE);
955  bool minimized = (HIWORD(wParam) != 0);
956  if (_wnd.fullscreen) {
957  if (active && minimized) {
958  /* Restore the game window */
959  ShowWindow(hwnd, SW_RESTORE);
960  static_cast<VideoDriver_Win32 *>(VideoDriver::GetInstance())->MakeWindow(true);
961  } else if (!active && !minimized) {
962  /* Minimise the window and restore desktop */
963  ShowWindow(hwnd, SW_MINIMIZE);
964  ChangeDisplaySettings(nullptr, 0);
965  }
966  }
967  break;
968  }
969  }
970 
971  return DefWindowProc(hwnd, msg, wParam, lParam);
972 }
973 
974 static void RegisterWndClass()
975 {
976  static bool registered = false;
977 
978  if (registered) return;
979 
980  HINSTANCE hinst = GetModuleHandle(nullptr);
981  WNDCLASS wnd = {
982  CS_OWNDC,
983  WndProcGdi,
984  0,
985  0,
986  hinst,
987  LoadIcon(hinst, MAKEINTRESOURCE(100)),
988  LoadCursor(nullptr, IDC_ARROW),
989  0,
990  0,
991  _T("OTTD")
992  };
993 
994  registered = true;
995  if (!RegisterClass(&wnd)) usererror("RegisterClass failed");
996 }
997 
998 static bool AllocateDibSection(int w, int h, bool force)
999 {
1000  BITMAPINFO *bi;
1001  HDC dc;
1003 
1004  w = std::max(w, 64);
1005  h = std::max(h, 64);
1006 
1007  if (!force && w == _screen.width && h == _screen.height) return false;
1008 
1009  bi = (BITMAPINFO*)alloca(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
1010  memset(bi, 0, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
1011  bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1012 
1013  bi->bmiHeader.biWidth = _wnd.width = w;
1014  bi->bmiHeader.biHeight = -(_wnd.height = h);
1015 
1016  bi->bmiHeader.biPlanes = 1;
1017  bi->bmiHeader.biBitCount = bpp;
1018  bi->bmiHeader.biCompression = BI_RGB;
1019 
1020  if (_wnd.dib_sect) DeleteObject(_wnd.dib_sect);
1021 
1022  dc = GetDC(0);
1023  _wnd.dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID**)&_wnd.buffer_bits, nullptr, 0);
1024  if (_wnd.dib_sect == nullptr) usererror("CreateDIBSection failed");
1025  ReleaseDC(0, dc);
1026 
1027  _screen.width = w;
1028  _screen.pitch = (bpp == 8) ? Align(w, 4) : w;
1029  _screen.height = h;
1030  _screen.dst_ptr = _wnd.buffer_bits;
1031 
1032  return true;
1033 }
1034 
1035 static const Dimension default_resolutions[] = {
1036  { 640, 480 },
1037  { 800, 600 },
1038  { 1024, 768 },
1039  { 1152, 864 },
1040  { 1280, 800 },
1041  { 1280, 960 },
1042  { 1280, 1024 },
1043  { 1400, 1050 },
1044  { 1600, 1200 },
1045  { 1680, 1050 },
1046  { 1920, 1200 }
1047 };
1048 
1049 static void FindResolutions()
1050 {
1051  uint i;
1052  DEVMODEA dm;
1053 
1054  /* Check modes for the relevant fullscreen bpp */
1055  uint bpp = _support8bpp != S8BPP_HARDWARE ? 32 : BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
1056 
1057  _resolutions.clear();
1058 
1059  /* XXX - EnumDisplaySettingsW crashes with unicows.dll on Windows95
1060  * Doesn't really matter since we don't pass a string anyways, but still
1061  * a letdown */
1062  for (i = 0; EnumDisplaySettingsA(nullptr, i, &dm) != 0; i++) {
1063  if (dm.dmBitsPerPel != bpp || dm.dmPelsWidth < 640 || dm.dmPelsHeight < 480) continue;
1064  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(dm.dmPelsWidth, dm.dmPelsHeight)) != _resolutions.end()) continue;
1065  _resolutions.emplace_back(dm.dmPelsWidth, dm.dmPelsHeight);
1066  }
1067 
1068  /* We have found no resolutions, show the default list */
1069  if (_resolutions.empty()) {
1070  _resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
1071  }
1072 
1073  SortResolutions();
1074 }
1075 
1076 static FVideoDriver_Win32 iFVideoDriver_Win32;
1077 
1078 const char *VideoDriver_Win32::Start(const StringList &parm)
1079 {
1080  if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
1081 
1082  this->UpdateAutoResolution();
1083 
1084  memset(&_wnd, 0, sizeof(_wnd));
1085 
1086  RegisterWndClass();
1087 
1088  MakePalette();
1089 
1090  FindResolutions();
1091 
1092  DEBUG(driver, 2, "Resolution for display: %ux%u", _cur_resolution.width, _cur_resolution.height);
1093 
1094  /* fullscreen uses those */
1095  _wnd.width_org = _cur_resolution.width;
1096  _wnd.height_org = _cur_resolution.height;
1097 
1098  AllocateDibSection(_cur_resolution.width, _cur_resolution.height);
1099  this->MakeWindow(_fullscreen);
1100 
1102 
1103  _draw_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread") && std::thread::hardware_concurrency() > 1;
1104 
1105  return nullptr;
1106 }
1107 
1109 {
1110  DeleteObject(_wnd.gdi_palette);
1111  DeleteObject(_wnd.dib_sect);
1112  DestroyWindow(_wnd.main_wnd);
1113 
1114  if (_wnd.fullscreen) ChangeDisplaySettings(nullptr, 0);
1115  MyShowCursor(true);
1116 }
1117 
1118 void VideoDriver_Win32::MakeDirty(int left, int top, int width, int height)
1119 {
1120  RECT r = { left, top, left + width, top + height };
1121 
1122  InvalidateRect(_wnd.main_wnd, &r, FALSE);
1123 }
1124 
1125 static void CheckPaletteAnim()
1126 {
1127  if (_cur_palette.count_dirty == 0) return;
1128 
1130  InvalidateRect(_wnd.main_wnd, nullptr, FALSE);
1131 }
1132 
1134 {
1135  MSG mesg;
1136  uint32 cur_ticks = GetTickCount();
1137  uint32 last_cur_ticks = cur_ticks;
1138  uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
1139 
1140  std::thread draw_thread;
1141  std::unique_lock<std::recursive_mutex> draw_lock;
1142 
1143  if (_draw_threaded) {
1144  /* Initialise the mutex first, because that's the thing we *need*
1145  * directly in the newly created thread. */
1146  try {
1147  _draw_signal = new std::condition_variable_any();
1148  _draw_mutex = new std::recursive_mutex();
1149  } catch (...) {
1150  _draw_threaded = false;
1151  }
1152 
1153  if (_draw_threaded) {
1154  draw_lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
1155 
1156  _draw_continue = true;
1157  _draw_threaded = StartNewThread(&draw_thread, "ottd:draw-win32", &PaintWindowThread);
1158 
1159  /* Free the mutex if we won't be able to use it. */
1160  if (!_draw_threaded) {
1161  draw_lock.unlock();
1162  draw_lock.release();
1163  delete _draw_mutex;
1164  delete _draw_signal;
1165  _draw_mutex = nullptr;
1166  _draw_signal = nullptr;
1167  } else {
1168  DEBUG(driver, 1, "Threaded drawing enabled");
1169  /* Wait till the draw thread has started itself. */
1170  _draw_signal->wait(*_draw_mutex);
1171  }
1172  }
1173  }
1174 
1175  _wnd.running = true;
1176 
1177  CheckPaletteAnim();
1178  for (;;) {
1179  uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
1180 
1181  while (PeekMessage(&mesg, nullptr, 0, 0, PM_REMOVE)) {
1182  InteractiveRandom(); // randomness
1183  /* Convert key messages to char messages if we want text input. */
1184  if (EditBoxInGlobalFocus()) TranslateMessage(&mesg);
1185  DispatchMessage(&mesg);
1186  }
1187  if (_exit_game) break;
1188 
1189 #if defined(_DEBUG)
1190  if (_wnd.has_focus && GetAsyncKeyState(VK_SHIFT) < 0 &&
1191 #else
1192  /* Speed up using TAB, but disable for ALT+TAB of course */
1193  if (_wnd.has_focus && GetAsyncKeyState(VK_TAB) < 0 && GetAsyncKeyState(VK_MENU) >= 0 &&
1194 #endif
1195  !_networking && _game_mode != GM_MENU) {
1196  _fast_forward |= 2;
1197  } else if (_fast_forward & 2) {
1198  _fast_forward = 0;
1199  }
1200 
1201  cur_ticks = GetTickCount();
1202  if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
1203  _realtime_tick += cur_ticks - last_cur_ticks;
1204  last_cur_ticks = cur_ticks;
1205  next_tick = cur_ticks + MILLISECONDS_PER_TICK;
1206 
1207  bool old_ctrl_pressed = _ctrl_pressed;
1208 
1209  _ctrl_pressed = _wnd.has_focus && GetAsyncKeyState(VK_CONTROL)<0;
1210  _shift_pressed = _wnd.has_focus && GetAsyncKeyState(VK_SHIFT)<0;
1211 
1212  /* determine which directional keys are down */
1213  if (_wnd.has_focus) {
1214  _dirkeys =
1215  (GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
1216  (GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
1217  (GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
1218  (GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
1219  } else {
1220  _dirkeys = 0;
1221  }
1222 
1223  if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
1224 
1225  /* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
1226  GdiFlush();
1227 
1228  /* The game loop is the part that can run asynchronously.
1229  * The rest except sleeping can't. */
1230  if (_draw_threaded) draw_lock.unlock();
1231  GameLoop();
1232  if (_draw_threaded) draw_lock.lock();
1233 
1234  if (_force_full_redraw) MarkWholeScreenDirty();
1235 
1236  UpdateWindows();
1237  CheckPaletteAnim();
1238  } else {
1239  /* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
1240  GdiFlush();
1241 
1242  /* Release the thread while sleeping */
1243  if (_draw_threaded) draw_lock.unlock();
1244  CSleep(1);
1245  if (_draw_threaded) draw_lock.lock();
1246 
1248  DrawMouseCursor();
1249  }
1250  }
1251 
1252  if (_draw_threaded) {
1253  _draw_continue = false;
1254  /* Sending signal if there is no thread blocked
1255  * is very valid and results in noop */
1256  _draw_signal->notify_all();
1257  if (draw_lock.owns_lock()) draw_lock.unlock();
1258  draw_lock.release();
1259  draw_thread.join();
1260 
1261  delete _draw_mutex;
1262  delete _draw_signal;
1263 
1264  _draw_mutex = nullptr;
1265  }
1266 }
1267 
1269 {
1270  std::unique_lock<std::recursive_mutex> lock;
1271  if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
1272 
1273  if (_window_maximize) ShowWindow(_wnd.main_wnd, SW_SHOWNORMAL);
1274 
1275  _wnd.width = _wnd.width_org = w;
1276  _wnd.height = _wnd.height_org = h;
1277 
1278  return this->MakeWindow(_fullscreen); // _wnd.fullscreen screws up ingame resolution switching
1279 }
1280 
1282 {
1283  std::unique_lock<std::recursive_mutex> lock;
1284  if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
1285 
1286  return this->MakeWindow(full_screen);
1287 }
1288 
1290 {
1291  assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
1292  return AllocateDibSection(_screen.width, _screen.height, true) && this->MakeWindow(_fullscreen);
1293 }
1294 
1296 {
1297  if (_draw_mutex != nullptr) _draw_mutex->lock();
1298 }
1299 
1301 {
1302  if (_draw_mutex != nullptr) _draw_mutex->unlock();
1303 }
1304 
1306 {
1307  std::unique_lock<std::recursive_mutex> lock;
1308  if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
1309 
1310  CancelIMEComposition(_wnd.main_wnd);
1311  SetCompositionPos(_wnd.main_wnd);
1312  SetCandidatePos(_wnd.main_wnd);
1313 }
1314 
1315 Dimension VideoDriver_Win32::GetScreenSize() const
1316 {
1317  return { static_cast<uint>(GetSystemMetrics(SM_CXSCREEN)), static_cast<uint>(GetSystemMetrics(SM_CYSCREEN)) };
1318 }
_dirkeys
byte _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition: gfx.cpp:31
fullscreen
bool fullscreen
Whether to use (true) fullscreen mode.
Definition: win32_v.cpp:52
has_focus
bool has_focus
Does our window have system focus?
Definition: win32_v.cpp:53
WKC_SINGLEQUOTE
@ WKC_SINGLEQUOTE
' Single quote
Definition: gfx_type.h:101
VideoDriver_Win32::ToggleFullscreen
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition: win32_v.cpp:1281
running
bool running
Is the main loop running?
Definition: win32_v.cpp:54
Palette::first_dirty
int first_dirty
The first dirty element.
Definition: gfx_type.h:315
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
CSleep
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:25
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
_left_button_down
bool _left_button_down
Is left mouse button pressed?
Definition: gfx.cpp:38
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:471
HandleKeypress
void HandleKeypress(uint keycode, WChar key)
Handle keyboard input.
Definition: window.cpp:2681
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
Blitter::GetScreenDepth
virtual uint8 GetScreenDepth()=0
Get the screen depth this blitter works for.
_local_palette
static Palette _local_palette
Local copy of the palette for use in the drawing thread.
Definition: win32_v.cpp:72
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
PerformanceMeasurer
RAII class for measuring simple elements of performance.
Definition: framerate_type.h:92
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
_draw_mutex
static std::recursive_mutex * _draw_mutex
Mutex to keep the access to the shared memory controlled.
Definition: win32_v.cpp:66
HandleIMEComposition
static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
Handle WM_IME_COMPOSITION messages.
Definition: win32_v.cpp:527
FVideoDriver_Win32
The factory for Windows' video driver.
Definition: win32_v.h:49
main_wnd
HWND main_wnd
Handle to system window.
Definition: win32_v.cpp:43
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
_draw_threaded
static bool _draw_threaded
Whether the drawing is/may be done in a separate thread.
Definition: win32_v.cpp:64
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
Utf16DecodeSurrogate
static WChar Utf16DecodeSurrogate(uint lead, uint trail)
Convert an UTF-16 surrogate pair to the corresponding Unicode character.
Definition: string_func.h:195
height_org
int height_org
Original monitor resolution height, before we changed it.
Definition: win32_v.cpp:51
UpdateWindows
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3139
height
int height
Height in pixels of our display surface.
Definition: win32_v.cpp:49
VkMapping
Definition: sdl_v.cpp:406
VideoDriver_Win32::MainLoop
void MainLoop() override
Perform the actual drawing.
Definition: win32_v.cpp:1133
win32_v.h
WKC_EQUALS
@ WKC_EQUALS
= Equals
Definition: gfx_type.h:97
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_Win32::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: win32_v.cpp:1118
HandleMouseEvents
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition: window.cpp:2988
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:1819
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
VideoDriver_Win32::Stop
void Stop() override
Stop this driver.
Definition: win32_v.cpp:1108
Blitter::PostResize
virtual void PostResize()
Post resize event.
Definition: base.hpp:201
VideoDriver_Win32::EditBoxLostFocus
void EditBoxLostFocus() override
An edit box lost the input focus.
Definition: win32_v.cpp:1305
VideoDriver_Win32
The video driver for windows.
Definition: win32_v.h:16
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
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:131
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
VideoDriver_Win32::ReleaseBlitterLock
void ReleaseBlitterLock() override
Release any lock(s) required to be held when changing blitters.
Definition: win32_v.cpp:1300
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
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
_shift_pressed
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:36
CursorVars::wheel
int wheel
mouse wheel movement
Definition: gfx_type.h:119
convert_from_fs
char * convert_from_fs(const TCHAR *name, char *utf8_buf, size_t buflen)
Convert to OpenTTD's encoding from that of the environment in UNICODE.
Definition: win32.cpp:616
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
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:111
NWidgetBase::current_y
uint current_y
Current vertical size (after resizing).
Definition: widget_type.h:173
_draw_signal
static std::condition_variable_any * _draw_signal
Signal to draw the next frame.
Definition: win32_v.cpp:68
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
width_org
int width_org
Original monitor resolution width, before we changed it.
Definition: win32_v.cpp:50
SetCandidatePos
static void SetCandidatePos(HWND hwnd)
Set the position of the candidate window.
Definition: win32_v.cpp:493
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:124
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:385
Blitter::PALETTE_ANIMATION_VIDEO_BACKEND
@ PALETTE_ANIMATION_VIDEO_BACKEND
Palette animation should be done by video backend (8bpp only!)
Definition: base.hpp:51
buffer_bits
void * buffer_bits
Internal rendering buffer.
Definition: win32_v.cpp:45
DrawIMECompositionString
static bool DrawIMECompositionString()
Should we draw the composition string ourself, i.e is this a normal IME?
Definition: win32_v.cpp:465
width
int width
Width in pixels of our display surface.
Definition: win32_v.cpp:48
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
Blitter::PALETTE_ANIMATION_NONE
@ PALETTE_ANIMATION_NONE
No palette animation.
Definition: base.hpp:50
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:402
Window::top
int top
y position of top edge of the window
Definition: window_gui.h:318
_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
MILLISECONDS_PER_TICK
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Definition: gfx_type.h:310
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:377
Window::width
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:319
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:1610
_draw_continue
static volatile bool _draw_continue
Should we keep continue drawing?
Definition: win32_v.cpp:70
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
NetworkDrawChatMessage
void NetworkDrawChatMessage()
Draw the chat message-box.
Definition: network_chat_gui.cpp:198
VideoDriver_Win32::ChangeResolution
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition: win32_v.cpp:1268
dib_sect
HBITMAP dib_sect
System bitmap object referencing our rendering buffer.
Definition: win32_v.cpp:44
HasModalProgress
static bool HasModalProgress()
Check if we are currently in a modal progress state.
Definition: progress.h:21
update_rect
RECT update_rect
Current dirty rect.
Definition: win32_v.cpp:47
gdi_palette
HPALETTE gdi_palette
Palette object for 8bpp blitter.
Definition: win32_v.cpp:46
WKC_MINUS
@ WKC_MINUS
Definition: gfx_type.h:104
VideoDriver_Win32::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: win32_v.cpp:1078
FS2OTTD
const char * FS2OTTD(const TCHAR *name)
Convert to OpenTTD's encoding from that of the local environment.
Definition: win32.cpp:583
_realtime_tick
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:48
CursorVars::pos
Point pos
logical mouse position
Definition: gfx_type.h:117
_right_button_clicked
bool _right_button_clicked
Is right mouse button clicked?
Definition: gfx.cpp:41
VideoDriver_Win32::AcquireBlitterLock
void AcquireBlitterLock() override
Acquire any lock(s) required to be held when changing blitters.
Definition: win32_v.cpp:1295
VideoDriver_Win32::MakeWindow
bool MakeWindow(bool full_screen)
Instantiate a new window.
Definition: win32_v.cpp:234
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:393
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
PaintWindow
static void PaintWindow(HDC dc)
Do palette animation and blit to the window.
Definition: win32_v.cpp:333
_right_button_down
bool _right_button_down
Is right mouse button pressed?
Definition: gfx.cpp:40
CancelIMEComposition
static void CancelIMEComposition(HWND hwnd)
Clear the current composition string.
Definition: win32_v.cpp:591
VideoDriver_Win32::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: win32_v.cpp:1289