OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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