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