# win32_v.cpp

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