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 (ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
00287 RECT r;
00288 GetWindowRect(GetDesktopWindow(), &r);
00289 return this->ChangeResolution(r.right - r.left, r.bottom - r.top);
00290 }
00291
00292 if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
00293 this->MakeWindow(false);
00294 return false;
00295 }
00296 } else if (_wnd.fullscreen) {
00297
00298 ChangeDisplaySettings(NULL, 0);
00299
00300 _wnd.width = _bck_resolution.width;
00301 _wnd.height = _bck_resolution.height;
00302 }
00303 #endif
00304
00305 {
00306 RECT r;
00307 DWORD style, showstyle;
00308 int w, h;
00309
00310 showstyle = SW_SHOWNORMAL;
00311 _wnd.fullscreen = full_screen;
00312 if (_wnd.fullscreen) {
00313 style = WS_POPUP;
00314 SetRect(&r, 0, 0, _wnd.width_org, _wnd.height_org);
00315 } else {
00316 style = WS_OVERLAPPEDWINDOW;
00317
00318 if (_window_maximize) showstyle = SW_SHOWMAXIMIZED;
00319 SetRect(&r, 0, 0, _wnd.width, _wnd.height);
00320 }
00321
00322 #if !defined(WINCE)
00323 AdjustWindowRect(&r, style, FALSE);
00324 #endif
00325 w = r.right - r.left;
00326 h = r.bottom - r.top;
00327
00328 if (_wnd.main_wnd != NULL) {
00329 if (!_window_maximize) SetWindowPos(_wnd.main_wnd, 0, 0, 0, w, h, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
00330 } else {
00331 TCHAR Windowtitle[50];
00332 int x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
00333 int y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
00334
00335 _sntprintf(Windowtitle, lengthof(Windowtitle), _T("OpenTTD %s"), MB_TO_WIDE(_openttd_revision));
00336
00337 _wnd.main_wnd = CreateWindow(_T("OTTD"), Windowtitle, style, x, y, w, h, 0, 0, GetModuleHandle(NULL), 0);
00338 if (_wnd.main_wnd == NULL) usererror("CreateWindow failed");
00339 ShowWindow(_wnd.main_wnd, showstyle);
00340 }
00341 }
00342
00343 BlitterFactoryBase::GetCurrentBlitter()->PostResize();
00344
00345 GameSizeChanged();
00346 return true;
00347 }
00348
00350 static void PaintWindow(HDC dc)
00351 {
00352 HDC dc2 = CreateCompatibleDC(dc);
00353 HBITMAP old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect);
00354 HPALETTE old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE);
00355
00356 if (_cur_palette.count_dirty != 0) {
00357 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00358
00359 switch (blitter->UsePaletteAnimation()) {
00360 case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
00361 UpdatePalette(dc2, _local_palette.first_dirty, _local_palette.count_dirty);
00362 break;
00363
00364 case Blitter::PALETTE_ANIMATION_BLITTER:
00365 blitter->PaletteAnimate(_local_palette);
00366 break;
00367
00368 case Blitter::PALETTE_ANIMATION_NONE:
00369 break;
00370
00371 default:
00372 NOT_REACHED();
00373 }
00374 _cur_palette.count_dirty = 0;
00375 }
00376
00377 BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY);
00378 SelectPalette(dc, old_palette, TRUE);
00379 SelectObject(dc2, old_bmp);
00380 DeleteDC(dc2);
00381 }
00382
00383 static void PaintWindowThread(void *)
00384 {
00385
00386 _draw_mutex->BeginCritical();
00387 _draw_mutex->SendSignal();
00388
00389
00390
00391 Sleep(0);
00392
00393
00394 _draw_mutex->WaitForSignal();
00395
00396 while (_draw_continue) {
00397
00398 POINT pt = {0, 0};
00399 ClientToScreen(_wnd.main_wnd, &pt);
00400 OffsetRect(&_wnd.update_rect, pt.x, pt.y);
00401
00402
00403
00404 HRGN rgn = CreateRectRgnIndirect(&_wnd.update_rect);
00405 HDC dc = GetDCEx(_wnd.main_wnd, rgn, DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_INTERSECTRGN);
00406
00407 PaintWindow(dc);
00408
00409
00410 SetRectEmpty(&_wnd.update_rect);
00411 ReleaseDC(_wnd.main_wnd, dc);
00412
00413
00414 GdiFlush();
00415
00416 _draw_mutex->WaitForSignal();
00417 }
00418
00419 _draw_mutex->EndCritical();
00420 _draw_thread->Exit();
00421 }
00422
00423 static LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
00424 {
00425 static uint32 keycode = 0;
00426 static bool console = false;
00427 static bool in_sizemove = false;
00428
00429 switch (msg) {
00430 case WM_CREATE:
00431 SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc);
00432 break;
00433
00434 case WM_ENTERSIZEMOVE:
00435 in_sizemove = true;
00436 break;
00437
00438 case WM_EXITSIZEMOVE:
00439 in_sizemove = false;
00440 break;
00441
00442 case WM_PAINT:
00443 if (!in_sizemove && _draw_mutex != NULL && !HasModalProgress()) {
00444
00445 RECT r;
00446 GetUpdateRect(hwnd, &r, FALSE);
00447 UnionRect(&_wnd.update_rect, &_wnd.update_rect, &r);
00448
00449
00450 ValidateRect(hwnd, NULL);
00451 _draw_mutex->SendSignal();
00452 } else {
00453 PAINTSTRUCT ps;
00454
00455 BeginPaint(hwnd, &ps);
00456 PaintWindow(ps.hdc);
00457 EndPaint(hwnd, &ps);
00458 }
00459 return 0;
00460
00461 case WM_PALETTECHANGED:
00462 if ((HWND)wParam == hwnd) return 0;
00463
00464
00465 case WM_QUERYNEWPALETTE: {
00466 HDC hDC = GetWindowDC(hwnd);
00467 HPALETTE hOldPalette = SelectPalette(hDC, _wnd.gdi_palette, FALSE);
00468 UINT nChanged = RealizePalette(hDC);
00469
00470 SelectPalette(hDC, hOldPalette, TRUE);
00471 ReleaseDC(hwnd, hDC);
00472 if (nChanged != 0) InvalidateRect(hwnd, NULL, FALSE);
00473 return 0;
00474 }
00475
00476 case WM_CLOSE:
00477 HandleExitGameRequest();
00478 return 0;
00479
00480 case WM_DESTROY:
00481 if (_window_maximize) _cur_resolution = _bck_resolution;
00482 return 0;
00483
00484 case WM_LBUTTONDOWN:
00485 SetCapture(hwnd);
00486 _left_button_down = true;
00487 HandleMouseEvents();
00488 return 0;
00489
00490 case WM_LBUTTONUP:
00491 ReleaseCapture();
00492 _left_button_down = false;
00493 _left_button_clicked = false;
00494 HandleMouseEvents();
00495 return 0;
00496
00497 case WM_RBUTTONDOWN:
00498 SetCapture(hwnd);
00499 _right_button_down = true;
00500 _right_button_clicked = true;
00501 HandleMouseEvents();
00502 return 0;
00503
00504 case WM_RBUTTONUP:
00505 ReleaseCapture();
00506 _right_button_down = false;
00507 HandleMouseEvents();
00508 return 0;
00509
00510 case WM_MOUSELEAVE:
00511 UndrawMouseCursor();
00512 _cursor.in_window = false;
00513
00514 if (!_left_button_down && !_right_button_down) MyShowCursor(true);
00515 return 0;
00516
00517 case WM_MOUSEMOVE: {
00518 int x = (int16)LOWORD(lParam);
00519 int y = (int16)HIWORD(lParam);
00520 POINT pt;
00521
00522
00523
00524
00525 if (!_cursor.in_window) {
00526 _cursor.in_window = true;
00527 SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc);
00528
00529 DrawMouseCursor();
00530 }
00531
00532 if (_cursor.fix_at) {
00533 int dx = x - _cursor.pos.x;
00534 int dy = y - _cursor.pos.y;
00535 if (dx != 0 || dy != 0) {
00536 _cursor.delta.x = dx;
00537 _cursor.delta.y = dy;
00538
00539 pt.x = _cursor.pos.x;
00540 pt.y = _cursor.pos.y;
00541
00542 ClientToScreen(hwnd, &pt);
00543 SetCursorPos(pt.x, pt.y);
00544 }
00545 } else {
00546 _cursor.delta.x = x - _cursor.pos.x;
00547 _cursor.delta.y = y - _cursor.pos.y;
00548 _cursor.pos.x = x;
00549 _cursor.pos.y = y;
00550 _cursor.dirty = true;
00551 }
00552 MyShowCursor(false);
00553 HandleMouseEvents();
00554 return 0;
00555 }
00556
00557 #if !defined(UNICODE)
00558 case WM_INPUTLANGCHANGE: {
00559 TCHAR locale[6];
00560 LCID lcid = GB(lParam, 0, 16);
00561
00562 int len = GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, locale, lengthof(locale));
00563 if (len != 0) _codepage = _ttoi(locale);
00564 return 1;
00565 }
00566 #endif
00567
00568 case WM_DEADCHAR:
00569 console = GB(lParam, 16, 8) == 41;
00570 return 0;
00571
00572 case WM_CHAR: {
00573 uint scancode = GB(lParam, 16, 8);
00574 uint charcode = wParam;
00575
00576
00577
00578 if (console && scancode == 41) {
00579 console = false;
00580 return 0;
00581 }
00582
00583 #if !defined(UNICODE)
00584 wchar_t w;
00585 int len = MultiByteToWideChar(_codepage, 0, (char*)&charcode, 1, &w, 1);
00586 charcode = len == 1 ? w : 0;
00587 #endif
00588
00589
00590 scancode = scancode == 41 ? (int)WKC_BACKQUOTE : keycode;
00591 HandleKeypress(GB(charcode, 0, 16) | (scancode << 16));
00592 return 0;
00593 }
00594
00595 case WM_KEYDOWN: {
00596 keycode = MapWindowsKey(wParam);
00597
00598
00599 MSG msg;
00600 if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
00601 if (msg.message == WM_CHAR && GB(lParam, 16, 8) == GB(msg.lParam, 16, 8)) {
00602 return 0;
00603 }
00604 }
00605
00606 HandleKeypress(0 | (keycode << 16));
00607 return 0;
00608 }
00609
00610 case WM_SYSKEYDOWN:
00611 switch (wParam) {
00612 case VK_RETURN:
00613 case 'F':
00614 ToggleFullScreen(!_wnd.fullscreen);
00615 return 0;
00616
00617 case VK_MENU:
00618 return 0;
00619
00620 case VK_F10:
00621 HandleKeypress(MapWindowsKey(wParam) << 16);
00622 return 0;
00623
00624 default:
00625 HandleKeypress(MapWindowsKey(wParam) << 16);
00626 break;
00627 }
00628 break;
00629
00630 case WM_SIZE:
00631 if (wParam != SIZE_MINIMIZED) {
00632
00633
00634 _window_maximize = (wParam == SIZE_MAXIMIZED || (_window_maximize && _fullscreen));
00635 if (_window_maximize || _fullscreen) _bck_resolution = _cur_resolution;
00636 ClientSizeChanged(LOWORD(lParam), HIWORD(lParam));
00637 }
00638 return 0;
00639
00640 #if !defined(WINCE)
00641 case WM_SIZING: {
00642 RECT *r = (RECT*)lParam;
00643 RECT r2;
00644 int w, h;
00645
00646 SetRect(&r2, 0, 0, 0, 0);
00647 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
00648
00649 w = r->right - r->left - (r2.right - r2.left);
00650 h = r->bottom - r->top - (r2.bottom - r2.top);
00651 w = max(w, 64);
00652 h = max(h, 64);
00653 SetRect(&r2, 0, 0, w, h);
00654
00655 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
00656 w = r2.right - r2.left;
00657 h = r2.bottom - r2.top;
00658
00659 switch (wParam) {
00660 case WMSZ_BOTTOM:
00661 r->bottom = r->top + h;
00662 break;
00663
00664 case WMSZ_BOTTOMLEFT:
00665 r->bottom = r->top + h;
00666 r->left = r->right - w;
00667 break;
00668
00669 case WMSZ_BOTTOMRIGHT:
00670 r->bottom = r->top + h;
00671 r->right = r->left + w;
00672 break;
00673
00674 case WMSZ_LEFT:
00675 r->left = r->right - w;
00676 break;
00677
00678 case WMSZ_RIGHT:
00679 r->right = r->left + w;
00680 break;
00681
00682 case WMSZ_TOP:
00683 r->top = r->bottom - h;
00684 break;
00685
00686 case WMSZ_TOPLEFT:
00687 r->top = r->bottom - h;
00688 r->left = r->right - w;
00689 break;
00690
00691 case WMSZ_TOPRIGHT:
00692 r->top = r->bottom - h;
00693 r->right = r->left + w;
00694 break;
00695 }
00696 return TRUE;
00697 }
00698 #endif
00699
00700
00701 #if !defined(WM_MOUSEWHEEL)
00702 # define WM_MOUSEWHEEL 0x020A
00703 #endif
00704 #if !defined(GET_WHEEL_DELTA_WPARAM)
00705 # define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam))
00706 #endif
00707
00708 case WM_MOUSEWHEEL: {
00709 int delta = GET_WHEEL_DELTA_WPARAM(wParam);
00710
00711 if (delta < 0) {
00712 _cursor.wheel++;
00713 } else if (delta > 0) {
00714 _cursor.wheel--;
00715 }
00716 HandleMouseEvents();
00717 return 0;
00718 }
00719
00720 case WM_SETFOCUS:
00721 _wnd.has_focus = true;
00722 break;
00723
00724 case WM_KILLFOCUS:
00725 _wnd.has_focus = false;
00726 break;
00727
00728 #if !defined(WINCE)
00729 case WM_ACTIVATE: {
00730
00731 if (_exit_game) break;
00732
00733 bool active = (LOWORD(wParam) != WA_INACTIVE);
00734 bool minimized = (HIWORD(wParam) != 0);
00735 if (_wnd.fullscreen) {
00736 if (active && minimized) {
00737
00738 ShowWindow(hwnd, SW_RESTORE);
00739 static_cast<VideoDriver_Win32 *>(_video_driver)->MakeWindow(true);
00740 } else if (!active && !minimized) {
00741
00742 ShowWindow(hwnd, SW_MINIMIZE);
00743 ChangeDisplaySettings(NULL, 0);
00744 }
00745 }
00746 break;
00747 }
00748 #endif
00749 }
00750
00751 return DefWindowProc(hwnd, msg, wParam, lParam);
00752 }
00753
00754 static void RegisterWndClass()
00755 {
00756 static bool registered = false;
00757
00758 if (!registered) {
00759 HINSTANCE hinst = GetModuleHandle(NULL);
00760 WNDCLASS wnd = {
00761 CS_OWNDC,
00762 WndProcGdi,
00763 0,
00764 0,
00765 hinst,
00766 LoadIcon(hinst, MAKEINTRESOURCE(100)),
00767 LoadCursor(NULL, IDC_ARROW),
00768 0,
00769 0,
00770 _T("OTTD")
00771 };
00772
00773 registered = true;
00774 if (!RegisterClass(&wnd)) usererror("RegisterClass failed");
00775 }
00776 }
00777
00778 static bool AllocateDibSection(int w, int h, bool force)
00779 {
00780 BITMAPINFO *bi;
00781 HDC dc;
00782 int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00783
00784 w = max(w, 64);
00785 h = max(h, 64);
00786
00787 if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
00788
00789 if (!force && w == _screen.width && h == _screen.height) return false;
00790
00791 _screen.width = w;
00792 _screen.pitch = (bpp == 8) ? Align(w, 4) : w;
00793 _screen.height = h;
00794 bi = (BITMAPINFO*)alloca(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
00795 memset(bi, 0, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
00796 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
00797
00798 bi->bmiHeader.biWidth = _wnd.width = w;
00799 bi->bmiHeader.biHeight = -(_wnd.height = h);
00800
00801 bi->bmiHeader.biPlanes = 1;
00802 bi->bmiHeader.biBitCount = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00803 bi->bmiHeader.biCompression = BI_RGB;
00804
00805 if (_wnd.dib_sect) DeleteObject(_wnd.dib_sect);
00806
00807 dc = GetDC(0);
00808 _wnd.dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID**)&_wnd.buffer_bits, NULL, 0);
00809 if (_wnd.dib_sect == NULL) usererror("CreateDIBSection failed");
00810 ReleaseDC(0, dc);
00811
00812 return true;
00813 }
00814
00815 static const Dimension default_resolutions[] = {
00816 { 640, 480 },
00817 { 800, 600 },
00818 { 1024, 768 },
00819 { 1152, 864 },
00820 { 1280, 800 },
00821 { 1280, 960 },
00822 { 1280, 1024 },
00823 { 1400, 1050 },
00824 { 1600, 1200 },
00825 { 1680, 1050 },
00826 { 1920, 1200 }
00827 };
00828
00829 static void FindResolutions()
00830 {
00831 uint n = 0;
00832 #if defined(WINCE)
00833
00834
00835 #else
00836 uint i;
00837 DEVMODEA dm;
00838
00839
00840
00841
00842 for (i = 0; EnumDisplaySettingsA(NULL, i, &dm) != 0; i++) {
00843 if (dm.dmBitsPerPel == BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() &&
00844 dm.dmPelsWidth >= 640 && dm.dmPelsHeight >= 480) {
00845 uint j;
00846
00847 for (j = 0; j < n; j++) {
00848 if (_resolutions[j].width == dm.dmPelsWidth && _resolutions[j].height == dm.dmPelsHeight) break;
00849 }
00850
00851
00852
00853
00854
00855 if (j == n) {
00856 _resolutions[j].width = dm.dmPelsWidth;
00857 _resolutions[j].height = dm.dmPelsHeight;
00858 if (++n == lengthof(_resolutions)) break;
00859 }
00860 }
00861 }
00862 #endif
00863
00864
00865 if (n == 0) {
00866 memcpy(_resolutions, default_resolutions, sizeof(default_resolutions));
00867 n = lengthof(default_resolutions);
00868 }
00869
00870 _num_resolutions = n;
00871 SortResolutions(_num_resolutions);
00872 }
00873
00874 static FVideoDriver_Win32 iFVideoDriver_Win32;
00875
00876 const char *VideoDriver_Win32::Start(const char * const *parm)
00877 {
00878 memset(&_wnd, 0, sizeof(_wnd));
00879
00880 RegisterWndClass();
00881
00882 MakePalette();
00883
00884 FindResolutions();
00885
00886 DEBUG(driver, 2, "Resolution for display: %ux%u", _cur_resolution.width, _cur_resolution.height);
00887
00888
00889 _wnd.width_org = _cur_resolution.width;
00890 _wnd.height_org = _cur_resolution.height;
00891
00892 AllocateDibSection(_cur_resolution.width, _cur_resolution.height);
00893 this->MakeWindow(_fullscreen);
00894
00895 MarkWholeScreenDirty();
00896
00897 _draw_threaded = GetDriverParam(parm, "no_threads") == NULL && GetDriverParam(parm, "no_thread") == NULL && GetCPUCoreCount() > 1;
00898
00899 return NULL;
00900 }
00901
00902 void VideoDriver_Win32::Stop()
00903 {
00904 DeleteObject(_wnd.gdi_palette);
00905 DeleteObject(_wnd.dib_sect);
00906 DestroyWindow(_wnd.main_wnd);
00907
00908 #if !defined(WINCE)
00909 if (_wnd.fullscreen) ChangeDisplaySettings(NULL, 0);
00910 #endif
00911 MyShowCursor(true);
00912 }
00913
00914 void VideoDriver_Win32::MakeDirty(int left, int top, int width, int height)
00915 {
00916 RECT r = { left, top, left + width, top + height };
00917
00918 InvalidateRect(_wnd.main_wnd, &r, FALSE);
00919 }
00920
00921 static void CheckPaletteAnim()
00922 {
00923 if (_cur_palette.count_dirty == 0) return;
00924
00925 _local_palette = _cur_palette;
00926 InvalidateRect(_wnd.main_wnd, NULL, FALSE);
00927 }
00928
00929 void VideoDriver_Win32::MainLoop()
00930 {
00931 MSG mesg;
00932 uint32 cur_ticks = GetTickCount();
00933 uint32 last_cur_ticks = cur_ticks;
00934 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00935
00936 if (_draw_threaded) {
00937
00938
00939 _draw_mutex = ThreadMutex::New();
00940 if (_draw_mutex == NULL) {
00941 _draw_threaded = false;
00942 } else {
00943 _draw_mutex->BeginCritical();
00944 _draw_continue = true;
00945
00946 _draw_threaded = ThreadObject::New(&PaintWindowThread, NULL, &_draw_thread);
00947
00948
00949 if (!_draw_threaded) {
00950 _draw_mutex->EndCritical();
00951 delete _draw_mutex;
00952 _draw_mutex = NULL;
00953 } else {
00954 DEBUG(driver, 1, "Threaded drawing enabled");
00955
00956
00957 _draw_mutex->WaitForSignal();
00958 }
00959 }
00960 }
00961
00962 _wnd.running = true;
00963
00964 CheckPaletteAnim();
00965 for (;;) {
00966 uint32 prev_cur_ticks = cur_ticks;
00967
00968 while (PeekMessage(&mesg, NULL, 0, 0, PM_REMOVE)) {
00969 InteractiveRandom();
00970 TranslateMessage(&mesg);
00971 DispatchMessage(&mesg);
00972 }
00973 if (_exit_game) return;
00974
00975 #if defined(_DEBUG)
00976 if (_wnd.has_focus && GetAsyncKeyState(VK_SHIFT) < 0 &&
00977 #else
00978
00979 if (_wnd.has_focus && GetAsyncKeyState(VK_TAB) < 0 && GetAsyncKeyState(VK_MENU) >= 0 &&
00980 #endif
00981 !_networking && _game_mode != GM_MENU) {
00982 _fast_forward |= 2;
00983 } else if (_fast_forward & 2) {
00984 _fast_forward = 0;
00985 }
00986
00987 cur_ticks = GetTickCount();
00988 if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
00989 _realtime_tick += cur_ticks - last_cur_ticks;
00990 last_cur_ticks = cur_ticks;
00991 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00992
00993 bool old_ctrl_pressed = _ctrl_pressed;
00994
00995 _ctrl_pressed = _wnd.has_focus && GetAsyncKeyState(VK_CONTROL)<0;
00996 _shift_pressed = _wnd.has_focus && GetAsyncKeyState(VK_SHIFT)<0;
00997
00998
00999 if (_wnd.has_focus) {
01000 _dirkeys =
01001 (GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
01002 (GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
01003 (GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
01004 (GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
01005 } else {
01006 _dirkeys = 0;
01007 }
01008
01009 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
01010
01011 #if !defined(WINCE)
01012
01013 GdiFlush();
01014 #endif
01015
01016
01017
01018 if (_draw_threaded) _draw_mutex->EndCritical();
01019 GameLoop();
01020 if (_draw_threaded) _draw_mutex->BeginCritical();
01021
01022 if (_force_full_redraw) MarkWholeScreenDirty();
01023
01024 _screen.dst_ptr = _wnd.buffer_bits;
01025 UpdateWindows();
01026 CheckPaletteAnim();
01027 } else {
01028 #if !defined(WINCE)
01029
01030 GdiFlush();
01031 #endif
01032
01033
01034 if (_draw_threaded) _draw_mutex->EndCritical();
01035 Sleep(1);
01036 if (_draw_threaded) _draw_mutex->BeginCritical();
01037
01038 _screen.dst_ptr = _wnd.buffer_bits;
01039 NetworkDrawChatMessage();
01040 DrawMouseCursor();
01041 }
01042 }
01043
01044 if (_draw_threaded) {
01045 _draw_continue = false;
01046
01047
01048 _draw_mutex->SendSignal();
01049 _draw_mutex->EndCritical();
01050 _draw_thread->Join();
01051
01052 delete _draw_mutex;
01053 delete _draw_thread;
01054 }
01055 }
01056
01057 bool VideoDriver_Win32::ChangeResolution(int w, int h)
01058 {
01059 if (_window_maximize) ShowWindow(_wnd.main_wnd, SW_SHOWNORMAL);
01060
01061 _wnd.width = _wnd.width_org = w;
01062 _wnd.height = _wnd.height_org = h;
01063
01064 return this->MakeWindow(_fullscreen);
01065 }
01066
01067 bool VideoDriver_Win32::ToggleFullscreen(bool full_screen)
01068 {
01069 return this->MakeWindow(full_screen);
01070 }
01071
01072 bool VideoDriver_Win32::AfterBlitterChange()
01073 {
01074 return AllocateDibSection(_screen.width, _screen.height, true) && this->MakeWindow(_fullscreen);
01075 }