# win32_v.cpp

Go to the documentation of this file.

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