# win32_v.cpp

Go to the documentation of this file.

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