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