win32_v.cpp

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

Generated on Mon May 11 15:48:09 2009 for OpenTTD by  doxygen 1.5.6