00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../openttd.h"
00014 #include "../gfx_func.h"
00015 #include "../os/windows/win32.h"
00016 #include "../rev.h"
00017 #include "../blitter/factory.hpp"
00018 #include "../network/network.h"
00019 #include "../core/math_func.hpp"
00020 #include "../core/random_func.hpp"
00021 #include "../texteff.hpp"
00022 #include "../thread/thread.h"
00023 #include "../progress.h"
00024 #include "win32_v.h"
00025 #include <windows.h>
00026
00027 static struct {
00028 HWND main_wnd;
00029 HBITMAP dib_sect;
00030 void *buffer_bits;
00031 HPALETTE gdi_palette;
00032 RECT update_rect;
00033 int width;
00034 int height;
00035 int width_org;
00036 int height_org;
00037 bool fullscreen;
00038 bool has_focus;
00039 bool running;
00040 } _wnd;
00041
00042 bool _force_full_redraw;
00043 bool _window_maximize;
00044 uint _display_hz;
00045 uint _fullscreen_bpp;
00046 static Dimension _bck_resolution;
00047 #if !defined(UNICODE)
00048 uint _codepage;
00049 #endif
00050
00052 static bool _draw_threaded;
00054 static ThreadObject *_draw_thread = NULL;
00056 static ThreadMutex *_draw_mutex = NULL;
00058 static volatile bool _draw_continue;
00060 static Palette _local_palette;
00061
00062 static void MakePalette()
00063 {
00064 LOGPALETTE *pal = (LOGPALETTE*)alloca(sizeof(LOGPALETTE) + (256 - 1) * sizeof(PALETTEENTRY));
00065
00066 pal->palVersion = 0x300;
00067 pal->palNumEntries = 256;
00068
00069 for (uint i = 0; i != 256; i++) {
00070 pal->palPalEntry[i].peRed = _cur_palette.palette[i].r;
00071 pal->palPalEntry[i].peGreen = _cur_palette.palette[i].g;
00072 pal->palPalEntry[i].peBlue = _cur_palette.palette[i].b;
00073 pal->palPalEntry[i].peFlags = 0;
00074
00075 }
00076 _wnd.gdi_palette = CreatePalette(pal);
00077 if (_wnd.gdi_palette == NULL) usererror("CreatePalette failed!\n");
00078
00079 _cur_palette.first_dirty = 0;
00080 _cur_palette.count_dirty = 256;
00081 _local_palette = _cur_palette;
00082 }
00083
00084 static void UpdatePalette(HDC dc, uint start, uint count)
00085 {
00086 RGBQUAD rgb[256];
00087 uint i;
00088
00089 for (i = 0; i != count; i++) {
00090 rgb[i].rgbRed = _local_palette.palette[start + i].r;
00091 rgb[i].rgbGreen = _local_palette.palette[start + i].g;
00092 rgb[i].rgbBlue = _local_palette.palette[start + i].b;
00093 rgb[i].rgbReserved = 0;
00094 }
00095
00096 SetDIBColorTable(dc, start, count, rgb);
00097 }
00098
00099 bool VideoDriver_Win32::ClaimMousePointer()
00100 {
00101 MyShowCursor(false, true);
00102 return true;
00103 }
00104
00105 struct VkMapping {
00106 byte vk_from;
00107 byte vk_count;
00108 byte map_to;
00109 };
00110
00111 #define AS(x, z) {x, 0, z}
00112 #define AM(x, y, z, w) {x, y - x, z}
00113
00114 static const VkMapping _vk_mapping[] = {
00115
00116 AM(VK_PRIOR, VK_DOWN, WKC_PAGEUP, WKC_DOWN),
00117
00118 AM('A', 'Z', 'A', 'Z'),
00119 AM('0', '9', '0', '9'),
00120
00121 AS(VK_ESCAPE, WKC_ESC),
00122 AS(VK_PAUSE, WKC_PAUSE),
00123 AS(VK_BACK, WKC_BACKSPACE),
00124 AM(VK_INSERT, VK_DELETE, WKC_INSERT, WKC_DELETE),
00125
00126 AS(VK_SPACE, WKC_SPACE),
00127 AS(VK_RETURN, WKC_RETURN),
00128 AS(VK_TAB, WKC_TAB),
00129
00130
00131 AM(VK_F1, VK_F12, WKC_F1, WKC_F12),
00132
00133
00134 AM(VK_NUMPAD0, VK_NUMPAD9, '0', '9'),
00135 AS(VK_DIVIDE, WKC_NUM_DIV),
00136 AS(VK_MULTIPLY, WKC_NUM_MUL),
00137 AS(VK_SUBTRACT, WKC_NUM_MINUS),
00138 AS(VK_ADD, WKC_NUM_PLUS),
00139 AS(VK_DECIMAL, WKC_NUM_DECIMAL),
00140
00141
00142 AS(0xBF, WKC_SLASH),
00143 AS(0xBA, WKC_SEMICOLON),
00144 AS(0xBB, WKC_EQUALS),
00145 AS(0xDB, WKC_L_BRACKET),
00146 AS(0xDC, WKC_BACKSLASH),
00147 AS(0xDD, WKC_R_BRACKET),
00148
00149 AS(0xDE, WKC_SINGLEQUOTE),
00150 AS(0xBC, WKC_COMMA),
00151 AS(0xBD, WKC_MINUS),
00152 AS(0xBE, WKC_PERIOD)
00153 };
00154
00155 static uint MapWindowsKey(uint sym)
00156 {
00157 const VkMapping *map;
00158 uint key = 0;
00159
00160 for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
00161 if ((uint)(sym - map->vk_from) <= map->vk_count) {
00162 key = sym - map->vk_from + map->map_to;
00163 break;
00164 }
00165 }
00166
00167 if (GetAsyncKeyState(VK_SHIFT) < 0) key |= WKC_SHIFT;
00168 if (GetAsyncKeyState(VK_CONTROL) < 0) key |= WKC_CTRL;
00169 if (GetAsyncKeyState(VK_MENU) < 0) key |= WKC_ALT;
00170 return key;
00171 }
00172
00173 static bool AllocateDibSection(int w, int h, bool force = false);
00174
00175 static void ClientSizeChanged(int w, int h)
00176 {
00177
00178 if (AllocateDibSection(w, h)) {
00179
00180 _cur_palette.first_dirty = 0;
00181 _cur_palette.count_dirty = 256;
00182 _local_palette = _cur_palette;
00183
00184 BlitterFactoryBase::GetCurrentBlitter()->PostResize();
00185
00186 GameSizeChanged();
00187
00188
00189 if (_wnd.running) {
00190 _screen.dst_ptr = _wnd.buffer_bits;
00191 UpdateWindows();
00192 }
00193 }
00194 }
00195
00196 #ifdef _DEBUG
00197
00198
00199 int RedrawScreenDebug()
00200 {
00201 HDC dc, dc2;
00202 static int _fooctr;
00203 HBITMAP old_bmp;
00204 HPALETTE old_palette;
00205
00206 _screen.dst_ptr = _wnd.buffer_bits;
00207 UpdateWindows();
00208
00209 dc = GetDC(_wnd.main_wnd);
00210 dc2 = CreateCompatibleDC(dc);
00211
00212 old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect);
00213 old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE);
00214 BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY);
00215 SelectPalette(dc, old_palette, TRUE);
00216 SelectObject(dc2, old_bmp);
00217 DeleteDC(dc2);
00218 ReleaseDC(_wnd.main_wnd, dc);
00219
00220 return _fooctr++;
00221 }
00222 #endif
00223
00224
00225 #if !defined(WM_MOUSELEAVE)
00226 #define WM_MOUSELEAVE 0x02A3
00227 #endif
00228 #define TID_POLLMOUSE 1
00229 #define MOUSE_POLL_DELAY 75
00230
00231 static void CALLBACK TrackMouseTimerProc(HWND hwnd, UINT msg, UINT event, DWORD time)
00232 {
00233 RECT rc;
00234 POINT pt;
00235
00236
00237
00238
00239 GetClientRect(hwnd, &rc);
00240 MapWindowPoints(hwnd, HWND_DESKTOP, (LPPOINT)(LPRECT)&rc, 2);
00241 GetCursorPos(&pt);
00242
00243 if (!PtInRect(&rc, pt) || (WindowFromPoint(pt) != hwnd)) {
00244 KillTimer(hwnd, event);
00245 PostMessage(hwnd, WM_MOUSELEAVE, 0, 0L);
00246 }
00247 }
00248
00254 bool VideoDriver_Win32::MakeWindow(bool full_screen)
00255 {
00256 _fullscreen = full_screen;
00257
00258
00259 if ((full_screen || _wnd.fullscreen) && _wnd.main_wnd) {
00260 DestroyWindow(_wnd.main_wnd);
00261 _wnd.main_wnd = 0;
00262 }
00263
00264 #if defined(WINCE)
00265
00266 #else
00267 if (full_screen) {
00268 DEVMODE settings;
00269
00270
00271 if (_fullscreen_bpp < BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth()) _fullscreen_bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00272
00273 memset(&settings, 0, sizeof(settings));
00274 settings.dmSize = sizeof(settings);
00275 settings.dmFields =
00276 (_fullscreen_bpp != 0 ? DM_BITSPERPEL : 0) |
00277 DM_PELSWIDTH |
00278 DM_PELSHEIGHT |
00279 (_display_hz != 0 ? DM_DISPLAYFREQUENCY : 0);
00280 settings.dmBitsPerPel = _fullscreen_bpp;
00281 settings.dmPelsWidth = _wnd.width_org;
00282 settings.dmPelsHeight = _wnd.height_org;
00283 settings.dmDisplayFrequency = _display_hz;
00284
00285
00286 if (settings.dmBitsPerPel != 32 && ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
00287 settings.dmBitsPerPel = 32;
00288 }
00289
00290
00291 if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
00292 RECT r;
00293 GetWindowRect(GetDesktopWindow(), &r);
00294
00295
00296 if (settings.dmPelsWidth != r.right - r.left || settings.dmPelsHeight != r.bottom - r.top) {
00297 return this->ChangeResolution(r.right - r.left, r.bottom - r.top);
00298 }
00299 }
00300
00301 if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
00302 this->MakeWindow(false);
00303 return false;
00304 }
00305 } else if (_wnd.fullscreen) {
00306
00307 ChangeDisplaySettings(NULL, 0);
00308
00309 _wnd.width = _bck_resolution.width;
00310 _wnd.height = _bck_resolution.height;
00311 }
00312 #endif
00313
00314 {
00315 RECT r;
00316 DWORD style, showstyle;
00317 int w, h;
00318
00319 showstyle = SW_SHOWNORMAL;
00320 _wnd.fullscreen = full_screen;
00321 if (_wnd.fullscreen) {
00322 style = WS_POPUP;
00323 SetRect(&r, 0, 0, _wnd.width_org, _wnd.height_org);
00324 } else {
00325 style = WS_OVERLAPPEDWINDOW;
00326
00327 if (_window_maximize) showstyle = SW_SHOWMAXIMIZED;
00328 SetRect(&r, 0, 0, _wnd.width, _wnd.height);
00329 }
00330
00331 #if !defined(WINCE)
00332 AdjustWindowRect(&r, style, FALSE);
00333 #endif
00334 w = r.right - r.left;
00335 h = r.bottom - r.top;
00336
00337 if (_wnd.main_wnd != NULL) {
00338 if (!_window_maximize) SetWindowPos(_wnd.main_wnd, 0, 0, 0, w, h, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
00339 } else {
00340 TCHAR Windowtitle[50];
00341 int x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
00342 int y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
00343
00344 _sntprintf(Windowtitle, lengthof(Windowtitle), _T("OpenTTD %s"), MB_TO_WIDE(_openttd_revision));
00345
00346 _wnd.main_wnd = CreateWindow(_T("OTTD"), Windowtitle, style, x, y, w, h, 0, 0, GetModuleHandle(NULL), 0);
00347 if (_wnd.main_wnd == NULL) usererror("CreateWindow failed");
00348 ShowWindow(_wnd.main_wnd, showstyle);
00349 }
00350 }
00351
00352 BlitterFactoryBase::GetCurrentBlitter()->PostResize();
00353
00354 GameSizeChanged();
00355 return true;
00356 }
00357
00359 static void PaintWindow(HDC dc)
00360 {
00361 HDC dc2 = CreateCompatibleDC(dc);
00362 HBITMAP old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect);
00363 HPALETTE old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE);
00364
00365 if (_cur_palette.count_dirty != 0) {
00366 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00367
00368 switch (blitter->UsePaletteAnimation()) {
00369 case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
00370 UpdatePalette(dc2, _local_palette.first_dirty, _local_palette.count_dirty);
00371 break;
00372
00373 case Blitter::PALETTE_ANIMATION_BLITTER:
00374 blitter->PaletteAnimate(_local_palette);
00375 break;
00376
00377 case Blitter::PALETTE_ANIMATION_NONE:
00378 break;
00379
00380 default:
00381 NOT_REACHED();
00382 }
00383 _cur_palette.count_dirty = 0;
00384 }
00385
00386 BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY);
00387 SelectPalette(dc, old_palette, TRUE);
00388 SelectObject(dc2, old_bmp);
00389 DeleteDC(dc2);
00390 }
00391
00392 static void PaintWindowThread(void *)
00393 {
00394
00395 _draw_mutex->BeginCritical();
00396 _draw_mutex->SendSignal();
00397
00398
00399
00400 Sleep(0);
00401
00402
00403 _draw_mutex->WaitForSignal();
00404
00405 while (_draw_continue) {
00406
00407 POINT pt = {0, 0};
00408 ClientToScreen(_wnd.main_wnd, &pt);
00409 OffsetRect(&_wnd.update_rect, pt.x, pt.y);
00410
00411
00412
00413 HRGN rgn = CreateRectRgnIndirect(&_wnd.update_rect);
00414 HDC dc = GetDCEx(_wnd.main_wnd, rgn, DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_INTERSECTRGN);
00415
00416 PaintWindow(dc);
00417
00418
00419 SetRectEmpty(&_wnd.update_rect);
00420 ReleaseDC(_wnd.main_wnd, dc);
00421
00422
00423 GdiFlush();
00424
00425 _draw_mutex->WaitForSignal();
00426 }
00427
00428 _draw_mutex->EndCritical();
00429 _draw_thread->Exit();
00430 }
00431
00432 static LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
00433 {
00434 static uint32 keycode = 0;
00435 static bool console = false;
00436 static bool in_sizemove = false;
00437
00438 switch (msg) {
00439 case WM_CREATE:
00440 SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc);
00441 break;
00442
00443 case WM_ENTERSIZEMOVE:
00444 in_sizemove = true;
00445 break;
00446
00447 case WM_EXITSIZEMOVE:
00448 in_sizemove = false;
00449 break;
00450
00451 case WM_PAINT:
00452 if (!in_sizemove && _draw_mutex != NULL && !HasModalProgress()) {
00453
00454 RECT r;
00455 GetUpdateRect(hwnd, &r, FALSE);
00456 UnionRect(&_wnd.update_rect, &_wnd.update_rect, &r);
00457
00458
00459 ValidateRect(hwnd, NULL);
00460 _draw_mutex->SendSignal();
00461 } else {
00462 PAINTSTRUCT ps;
00463
00464 BeginPaint(hwnd, &ps);
00465 PaintWindow(ps.hdc);
00466 EndPaint(hwnd, &ps);
00467 }
00468 return 0;
00469
00470 case WM_PALETTECHANGED:
00471 if ((HWND)wParam == hwnd) return 0;
00472
00473
00474 case WM_QUERYNEWPALETTE: {
00475 HDC hDC = GetWindowDC(hwnd);
00476 HPALETTE hOldPalette = SelectPalette(hDC, _wnd.gdi_palette, FALSE);
00477 UINT nChanged = RealizePalette(hDC);
00478
00479 SelectPalette(hDC, hOldPalette, TRUE);
00480 ReleaseDC(hwnd, hDC);
00481 if (nChanged != 0) InvalidateRect(hwnd, NULL, FALSE);
00482 return 0;
00483 }
00484
00485 case WM_CLOSE:
00486 HandleExitGameRequest();
00487 return 0;
00488
00489 case WM_DESTROY:
00490 if (_window_maximize) _cur_resolution = _bck_resolution;
00491 return 0;
00492
00493 case WM_LBUTTONDOWN:
00494 SetCapture(hwnd);
00495 _left_button_down = true;
00496 HandleMouseEvents();
00497 return 0;
00498
00499 case WM_LBUTTONUP:
00500 ReleaseCapture();
00501 _left_button_down = false;
00502 _left_button_clicked = false;
00503 HandleMouseEvents();
00504 return 0;
00505
00506 case WM_RBUTTONDOWN:
00507 SetCapture(hwnd);
00508 _right_button_down = true;
00509 _right_button_clicked = true;
00510 HandleMouseEvents();
00511 return 0;
00512
00513 case WM_RBUTTONUP:
00514 ReleaseCapture();
00515 _right_button_down = false;
00516 HandleMouseEvents();
00517 return 0;
00518
00519 case WM_MOUSELEAVE:
00520 UndrawMouseCursor();
00521 _cursor.in_window = false;
00522
00523 if (!_left_button_down && !_right_button_down) MyShowCursor(true);
00524 return 0;
00525
00526 case WM_MOUSEMOVE: {
00527 int x = (int16)LOWORD(lParam);
00528 int y = (int16)HIWORD(lParam);
00529 POINT pt;
00530
00531
00532
00533
00534 if (!_cursor.in_window) {
00535 _cursor.in_window = true;
00536 SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc);
00537
00538 DrawMouseCursor();
00539 }
00540
00541 if (_cursor.fix_at) {
00542 int dx = x - _cursor.pos.x;
00543 int dy = y - _cursor.pos.y;
00544 if (dx != 0 || dy != 0) {
00545 _cursor.delta.x = dx;
00546 _cursor.delta.y = dy;
00547
00548 pt.x = _cursor.pos.x;
00549 pt.y = _cursor.pos.y;
00550
00551 ClientToScreen(hwnd, &pt);
00552 SetCursorPos(pt.x, pt.y);
00553 }
00554 } else {
00555 _cursor.delta.x = x - _cursor.pos.x;
00556 _cursor.delta.y = y - _cursor.pos.y;
00557 _cursor.pos.x = x;
00558 _cursor.pos.y = y;
00559 _cursor.dirty = true;
00560 }
00561 MyShowCursor(false);
00562 HandleMouseEvents();
00563 return 0;
00564 }
00565
00566 #if !defined(UNICODE)
00567 case WM_INPUTLANGCHANGE: {
00568 TCHAR locale[6];
00569 LCID lcid = GB(lParam, 0, 16);
00570
00571 int len = GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, locale, lengthof(locale));
00572 if (len != 0) _codepage = _ttoi(locale);
00573 return 1;
00574 }
00575 #endif
00576
00577 case WM_DEADCHAR:
00578 console = GB(lParam, 16, 8) == 41;
00579 return 0;
00580
00581 case WM_CHAR: {
00582 uint scancode = GB(lParam, 16, 8);
00583 uint charcode = wParam;
00584
00585
00586
00587 if (console && scancode == 41) {
00588 console = false;
00589 return 0;
00590 }
00591
00592 #if !defined(UNICODE)
00593 wchar_t w;
00594 int len = MultiByteToWideChar(_codepage, 0, (char*)&charcode, 1, &w, 1);
00595 charcode = len == 1 ? w : 0;
00596 #endif
00597
00598
00599 scancode = scancode == 41 ? (int)WKC_BACKQUOTE : keycode;
00600 HandleKeypress(GB(charcode, 0, 16) | (scancode << 16));
00601 return 0;
00602 }
00603
00604 case WM_KEYDOWN: {
00605 keycode = MapWindowsKey(wParam);
00606
00607
00608 MSG msg;
00609 if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
00610 if (msg.message == WM_CHAR && GB(lParam, 16, 8) == GB(msg.lParam, 16, 8)) {
00611 return 0;
00612 }
00613 }
00614
00615 HandleKeypress(0 | (keycode << 16));
00616 return 0;
00617 }
00618
00619 case WM_SYSKEYDOWN:
00620 switch (wParam) {
00621 case VK_RETURN:
00622 case 'F':
00623 ToggleFullScreen(!_wnd.fullscreen);
00624 return 0;
00625
00626 case VK_MENU:
00627 return 0;
00628
00629 case VK_F10:
00630 HandleKeypress(MapWindowsKey(wParam) << 16);
00631 return 0;
00632
00633 default:
00634 HandleKeypress(MapWindowsKey(wParam) << 16);
00635 break;
00636 }
00637 break;
00638
00639 case WM_SIZE:
00640 if (wParam != SIZE_MINIMIZED) {
00641
00642
00643 _window_maximize = (wParam == SIZE_MAXIMIZED || (_window_maximize && _fullscreen));
00644 if (_window_maximize || _fullscreen) _bck_resolution = _cur_resolution;
00645 ClientSizeChanged(LOWORD(lParam), HIWORD(lParam));
00646 }
00647 return 0;
00648
00649 #if !defined(WINCE)
00650 case WM_SIZING: {
00651 RECT *r = (RECT*)lParam;
00652 RECT r2;
00653 int w, h;
00654
00655 SetRect(&r2, 0, 0, 0, 0);
00656 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
00657
00658 w = r->right - r->left - (r2.right - r2.left);
00659 h = r->bottom - r->top - (r2.bottom - r2.top);
00660 w = max(w, 64);
00661 h = max(h, 64);
00662 SetRect(&r2, 0, 0, w, h);
00663
00664 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
00665 w = r2.right - r2.left;
00666 h = r2.bottom - r2.top;
00667
00668 switch (wParam) {
00669 case WMSZ_BOTTOM:
00670 r->bottom = r->top + h;
00671 break;
00672
00673 case WMSZ_BOTTOMLEFT:
00674 r->bottom = r->top + h;
00675 r->left = r->right - w;
00676 break;
00677
00678 case WMSZ_BOTTOMRIGHT:
00679 r->bottom = r->top + h;
00680 r->right = r->left + w;
00681 break;
00682
00683 case WMSZ_LEFT:
00684 r->left = r->right - w;
00685 break;
00686
00687 case WMSZ_RIGHT:
00688 r->right = r->left + w;
00689 break;
00690
00691 case WMSZ_TOP:
00692 r->top = r->bottom - h;
00693 break;
00694
00695 case WMSZ_TOPLEFT:
00696 r->top = r->bottom - h;
00697 r->left = r->right - w;
00698 break;
00699
00700 case WMSZ_TOPRIGHT:
00701 r->top = r->bottom - h;
00702 r->right = r->left + w;
00703 break;
00704 }
00705 return TRUE;
00706 }
00707 #endif
00708
00709
00710 #if !defined(WM_MOUSEWHEEL)
00711 # define WM_MOUSEWHEEL 0x020A
00712 #endif
00713 #if !defined(GET_WHEEL_DELTA_WPARAM)
00714 # define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam))
00715 #endif
00716
00717 case WM_MOUSEWHEEL: {
00718 int delta = GET_WHEEL_DELTA_WPARAM(wParam);
00719
00720 if (delta < 0) {
00721 _cursor.wheel++;
00722 } else if (delta > 0) {
00723 _cursor.wheel--;
00724 }
00725 HandleMouseEvents();
00726 return 0;
00727 }
00728
00729 case WM_SETFOCUS:
00730 _wnd.has_focus = true;
00731 break;
00732
00733 case WM_KILLFOCUS:
00734 _wnd.has_focus = false;
00735 break;
00736
00737 #if !defined(WINCE)
00738 case WM_ACTIVATE: {
00739
00740 if (_exit_game) break;
00741
00742 bool active = (LOWORD(wParam) != WA_INACTIVE);
00743 bool minimized = (HIWORD(wParam) != 0);
00744 if (_wnd.fullscreen) {
00745 if (active && minimized) {
00746
00747 ShowWindow(hwnd, SW_RESTORE);
00748 static_cast<VideoDriver_Win32 *>(_video_driver)->MakeWindow(true);
00749 } else if (!active && !minimized) {
00750
00751 ShowWindow(hwnd, SW_MINIMIZE);
00752 ChangeDisplaySettings(NULL, 0);
00753 }
00754 }
00755 break;
00756 }
00757 #endif
00758 }
00759
00760 return DefWindowProc(hwnd, msg, wParam, lParam);
00761 }
00762
00763 static void RegisterWndClass()
00764 {
00765 static bool registered = false;
00766
00767 if (!registered) {
00768 HINSTANCE hinst = GetModuleHandle(NULL);
00769 WNDCLASS wnd = {
00770 CS_OWNDC,
00771 WndProcGdi,
00772 0,
00773 0,
00774 hinst,
00775 LoadIcon(hinst, MAKEINTRESOURCE(100)),
00776 LoadCursor(NULL, IDC_ARROW),
00777 0,
00778 0,
00779 _T("OTTD")
00780 };
00781
00782 registered = true;
00783 if (!RegisterClass(&wnd)) usererror("RegisterClass failed");
00784 }
00785 }
00786
00787 static bool AllocateDibSection(int w, int h, bool force)
00788 {
00789 BITMAPINFO *bi;
00790 HDC dc;
00791 int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00792
00793 w = max(w, 64);
00794 h = max(h, 64);
00795
00796 if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
00797
00798 if (!force && w == _screen.width && h == _screen.height) return false;
00799
00800 _screen.width = w;
00801 _screen.pitch = (bpp == 8) ? Align(w, 4) : w;
00802 _screen.height = h;
00803 bi = (BITMAPINFO*)alloca(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
00804 memset(bi, 0, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
00805 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
00806
00807 bi->bmiHeader.biWidth = _wnd.width = w;
00808 bi->bmiHeader.biHeight = -(_wnd.height = h);
00809
00810 bi->bmiHeader.biPlanes = 1;
00811 bi->bmiHeader.biBitCount = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00812 bi->bmiHeader.biCompression = BI_RGB;
00813
00814 if (_wnd.dib_sect) DeleteObject(_wnd.dib_sect);
00815
00816 dc = GetDC(0);
00817 _wnd.dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID**)&_wnd.buffer_bits, NULL, 0);
00818 if (_wnd.dib_sect == NULL) usererror("CreateDIBSection failed");
00819 ReleaseDC(0, dc);
00820
00821 return true;
00822 }
00823
00824 static const Dimension default_resolutions[] = {
00825 { 640, 480 },
00826 { 800, 600 },
00827 { 1024, 768 },
00828 { 1152, 864 },
00829 { 1280, 800 },
00830 { 1280, 960 },
00831 { 1280, 1024 },
00832 { 1400, 1050 },
00833 { 1600, 1200 },
00834 { 1680, 1050 },
00835 { 1920, 1200 }
00836 };
00837
00838 static void FindResolutions()
00839 {
00840 uint n = 0;
00841 #if defined(WINCE)
00842
00843
00844 #else
00845 uint i;
00846 DEVMODEA dm;
00847
00848
00849
00850
00851 for (i = 0; EnumDisplaySettingsA(NULL, i, &dm) != 0; i++) {
00852 if (dm.dmBitsPerPel == BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() &&
00853 dm.dmPelsWidth >= 640 && dm.dmPelsHeight >= 480) {
00854 uint j;
00855
00856 for (j = 0; j < n; j++) {
00857 if (_resolutions[j].width == dm.dmPelsWidth && _resolutions[j].height == dm.dmPelsHeight) break;
00858 }
00859
00860
00861
00862
00863
00864 if (j == n) {
00865 _resolutions[j].width = dm.dmPelsWidth;
00866 _resolutions[j].height = dm.dmPelsHeight;
00867 if (++n == lengthof(_resolutions)) break;
00868 }
00869 }
00870 }
00871 #endif
00872
00873
00874 if (n == 0) {
00875 memcpy(_resolutions, default_resolutions, sizeof(default_resolutions));
00876 n = lengthof(default_resolutions);
00877 }
00878
00879 _num_resolutions = n;
00880 SortResolutions(_num_resolutions);
00881 }
00882
00883 static FVideoDriver_Win32 iFVideoDriver_Win32;
00884
00885 const char *VideoDriver_Win32::Start(const char * const *parm)
00886 {
00887 memset(&_wnd, 0, sizeof(_wnd));
00888
00889 RegisterWndClass();
00890
00891 MakePalette();
00892
00893 FindResolutions();
00894
00895 DEBUG(driver, 2, "Resolution for display: %ux%u", _cur_resolution.width, _cur_resolution.height);
00896
00897
00898 _wnd.width_org = _cur_resolution.width;
00899 _wnd.height_org = _cur_resolution.height;
00900
00901 AllocateDibSection(_cur_resolution.width, _cur_resolution.height);
00902 this->MakeWindow(_fullscreen);
00903
00904 MarkWholeScreenDirty();
00905
00906 _draw_threaded = GetDriverParam(parm, "no_threads") == NULL && GetDriverParam(parm, "no_thread") == NULL && GetCPUCoreCount() > 1;
00907
00908 return NULL;
00909 }
00910
00911 void VideoDriver_Win32::Stop()
00912 {
00913 DeleteObject(_wnd.gdi_palette);
00914 DeleteObject(_wnd.dib_sect);
00915 DestroyWindow(_wnd.main_wnd);
00916
00917 #if !defined(WINCE)
00918 if (_wnd.fullscreen) ChangeDisplaySettings(NULL, 0);
00919 #endif
00920 MyShowCursor(true);
00921 }
00922
00923 void VideoDriver_Win32::MakeDirty(int left, int top, int width, int height)
00924 {
00925 RECT r = { left, top, left + width, top + height };
00926
00927 InvalidateRect(_wnd.main_wnd, &r, FALSE);
00928 }
00929
00930 static void CheckPaletteAnim()
00931 {
00932 if (_cur_palette.count_dirty == 0) return;
00933
00934 _local_palette = _cur_palette;
00935 InvalidateRect(_wnd.main_wnd, NULL, FALSE);
00936 }
00937
00938 void VideoDriver_Win32::MainLoop()
00939 {
00940 MSG mesg;
00941 uint32 cur_ticks = GetTickCount();
00942 uint32 last_cur_ticks = cur_ticks;
00943 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00944
00945 if (_draw_threaded) {
00946
00947
00948 _draw_mutex = ThreadMutex::New();
00949 if (_draw_mutex == NULL) {
00950 _draw_threaded = false;
00951 } else {
00952 _draw_mutex->BeginCritical();
00953 _draw_continue = true;
00954
00955 _draw_threaded = ThreadObject::New(&PaintWindowThread, NULL, &_draw_thread);
00956
00957
00958 if (!_draw_threaded) {
00959 _draw_mutex->EndCritical();
00960 delete _draw_mutex;
00961 _draw_mutex = NULL;
00962 } else {
00963 DEBUG(driver, 1, "Threaded drawing enabled");
00964
00965
00966 _draw_mutex->WaitForSignal();
00967 }
00968 }
00969 }
00970
00971 _wnd.running = true;
00972
00973 CheckPaletteAnim();
00974 for (;;) {
00975 uint32 prev_cur_ticks = cur_ticks;
00976
00977 while (PeekMessage(&mesg, NULL, 0, 0, PM_REMOVE)) {
00978 InteractiveRandom();
00979 TranslateMessage(&mesg);
00980 DispatchMessage(&mesg);
00981 }
00982 if (_exit_game) return;
00983
00984 #if defined(_DEBUG)
00985 if (_wnd.has_focus && GetAsyncKeyState(VK_SHIFT) < 0 &&
00986 #else
00987
00988 if (_wnd.has_focus && GetAsyncKeyState(VK_TAB) < 0 && GetAsyncKeyState(VK_MENU) >= 0 &&
00989 #endif
00990 !_networking && _game_mode != GM_MENU) {
00991 _fast_forward |= 2;
00992 } else if (_fast_forward & 2) {
00993 _fast_forward = 0;
00994 }
00995
00996 cur_ticks = GetTickCount();
00997 if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
00998 _realtime_tick += cur_ticks - last_cur_ticks;
00999 last_cur_ticks = cur_ticks;
01000 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
01001
01002 bool old_ctrl_pressed = _ctrl_pressed;
01003
01004 _ctrl_pressed = _wnd.has_focus && GetAsyncKeyState(VK_CONTROL)<0;
01005 _shift_pressed = _wnd.has_focus && GetAsyncKeyState(VK_SHIFT)<0;
01006
01007
01008 if (_wnd.has_focus) {
01009 _dirkeys =
01010 (GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
01011 (GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
01012 (GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
01013 (GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
01014 } else {
01015 _dirkeys = 0;
01016 }
01017
01018 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
01019
01020 #if !defined(WINCE)
01021
01022 GdiFlush();
01023 #endif
01024
01025
01026
01027 if (_draw_threaded) _draw_mutex->EndCritical();
01028 GameLoop();
01029 if (_draw_threaded) _draw_mutex->BeginCritical();
01030
01031 if (_force_full_redraw) MarkWholeScreenDirty();
01032
01033 _screen.dst_ptr = _wnd.buffer_bits;
01034 UpdateWindows();
01035 CheckPaletteAnim();
01036 } else {
01037 #if !defined(WINCE)
01038
01039 GdiFlush();
01040 #endif
01041
01042
01043 if (_draw_threaded) _draw_mutex->EndCritical();
01044 Sleep(1);
01045 if (_draw_threaded) _draw_mutex->BeginCritical();
01046
01047 _screen.dst_ptr = _wnd.buffer_bits;
01048 NetworkDrawChatMessage();
01049 DrawMouseCursor();
01050 }
01051 }
01052
01053 if (_draw_threaded) {
01054 _draw_continue = false;
01055
01056
01057 _draw_mutex->SendSignal();
01058 _draw_mutex->EndCritical();
01059 _draw_thread->Join();
01060
01061 delete _draw_mutex;
01062 delete _draw_thread;
01063 }
01064 }
01065
01066 bool VideoDriver_Win32::ChangeResolution(int w, int h)
01067 {
01068 if (_window_maximize) ShowWindow(_wnd.main_wnd, SW_SHOWNORMAL);
01069
01070 _wnd.width = _wnd.width_org = w;
01071 _wnd.height = _wnd.height_org = h;
01072
01073 return this->MakeWindow(_fullscreen);
01074 }
01075
01076 bool VideoDriver_Win32::ToggleFullscreen(bool full_screen)
01077 {
01078 return this->MakeWindow(full_screen);
01079 }
01080
01081 bool VideoDriver_Win32::AfterBlitterChange()
01082 {
01083 return AllocateDibSection(_screen.width, _screen.height, true) && this->MakeWindow(_fullscreen);
01084 }