win32_v.cpp

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

Generated on Thu Jan 20 22:57:43 2011 for OpenTTD by  doxygen 1.6.1