tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file win32_v.cpp Implementation of the Windows (GDI) video driver. */ rubidium@9111: tron@2189: #include "../stdafx.h" tron@2189: #include "../openttd.h" rubidium@8123: #include "../gfx_func.h" tron@2189: #include "../variables.h" tron@2207: #include "../win32.h" smatz@9085: #include "../rev.h" truelight@6939: #include "../blitter/factory.hpp" rubidium@8121: #include "../network/network.h" glx@8277: #include "../core/math_func.hpp" glx@8437: #include "../core/random_func.hpp" rubidium@9117: #include "../functions.h" rubidium@9117: #include "../texteff.hpp" tron@2189: #include "win32_v.h" tron@2174: #include Darkvater@5168: #include tron@2174: tron@2174: static struct { tron@2174: HWND main_wnd; tron@2174: HBITMAP dib_sect; truelight@6878: void *buffer_bits; tron@2174: HPALETTE gdi_palette; tron@4489: int width; tron@4489: int height; tron@4489: int width_org; tron@4489: int height_org; tron@2174: bool fullscreen; tron@2174: bool has_focus; tron@2174: bool running; tron@2174: } _wnd; tron@2174: Darkvater@3051: bool _force_full_redraw; Darkvater@4258: bool _window_maximize; Darkvater@3051: uint _display_hz; Darkvater@3051: uint _fullscreen_bpp; smatz@9533: static Dimension _bck_resolution; Darkvater@6251: #if !defined(UNICODE) Darkvater@6251: uint _codepage; Darkvater@6251: #endif Darkvater@3051: rubidium@6247: static void MakePalette() tron@2174: { tron@2174: LOGPALETTE *pal; tron@2174: uint i; tron@2174: rubidium@6491: pal = (LOGPALETTE*)alloca(sizeof(LOGPALETTE) + (256 - 1) * sizeof(PALETTEENTRY)); tron@2174: tron@2174: pal->palVersion = 0x300; tron@2174: pal->palNumEntries = 256; tron@2174: tron@2174: for (i = 0; i != 256; i++) { tron@2174: pal->palPalEntry[i].peRed = _cur_palette[i].r; tron@2174: pal->palPalEntry[i].peGreen = _cur_palette[i].g; tron@2174: pal->palPalEntry[i].peBlue = _cur_palette[i].b; tron@2174: pal->palPalEntry[i].peFlags = 0; tron@2174: tron@2174: } tron@2174: _wnd.gdi_palette = CreatePalette(pal); glx@9470: if (_wnd.gdi_palette == NULL) usererror("CreatePalette failed!\n"); tron@2174: } tron@2174: tron@2174: static void UpdatePalette(HDC dc, uint start, uint count) tron@2174: { tron@2174: RGBQUAD rgb[256]; tron@2174: uint i; tron@2174: tron@2174: for (i = 0; i != count; i++) { tron@2174: rgb[i].rgbRed = _cur_palette[start + i].r; tron@2174: rgb[i].rgbGreen = _cur_palette[start + i].g; tron@2174: rgb[i].rgbBlue = _cur_palette[start + i].b; tron@2174: rgb[i].rgbReserved = 0; tron@2174: } tron@2174: tron@2174: SetDIBColorTable(dc, start, count, rgb); tron@2174: } tron@2174: rubidium@6248: struct VkMapping { tron@2174: byte vk_from; tron@2174: byte vk_count; tron@2174: byte map_to; rubidium@6248: }; tron@2174: tron@2174: #define AS(x, z) {x, 0, z} tron@2174: #define AM(x, y, z, w) {x, y - x, z} tron@2174: tron@2174: static const VkMapping _vk_mapping[] = { truelight@7311: /* Pageup stuff + up/down */ tron@2174: AM(VK_PRIOR,VK_DOWN, WKC_PAGEUP, WKC_DOWN), truelight@7311: /* Map letters & digits */ tron@2174: AM('A','Z','A','Z'), tron@2174: AM('0','9','0','9'), tron@2174: rubidium@4434: AS(VK_ESCAPE, WKC_ESC), rubidium@4434: AS(VK_PAUSE, WKC_PAUSE), rubidium@4434: AS(VK_BACK, WKC_BACKSPACE), rubidium@4434: AM(VK_INSERT, VK_DELETE, WKC_INSERT, WKC_DELETE), tron@2174: rubidium@4434: AS(VK_SPACE, WKC_SPACE), rubidium@4434: AS(VK_RETURN, WKC_RETURN), rubidium@4434: AS(VK_TAB, WKC_TAB), tron@2174: truelight@7311: /* Function keys */ rubidium@4434: AM(VK_F1, VK_F12, WKC_F1, WKC_F12), tron@2174: truelight@7311: /* Numeric part */ rubidium@4434: AM(VK_NUMPAD0, VK_NUMPAD9, WKC_NUM_0, WKC_NUM_9), rubidium@4434: AS(VK_DIVIDE, WKC_NUM_DIV), rubidium@4434: AS(VK_MULTIPLY, WKC_NUM_MUL), rubidium@4434: AS(VK_SUBTRACT, WKC_NUM_MINUS), rubidium@4434: AS(VK_ADD, WKC_NUM_PLUS), truelight@7310: AS(VK_DECIMAL, WKC_NUM_DECIMAL), truelight@7310: truelight@7311: /* Other non-letter keys */ truelight@7310: AS(0xBF, WKC_SLASH), truelight@7310: AS(0xBA, WKC_SEMICOLON), truelight@7310: AS(0xBB, WKC_EQUALS), truelight@7310: AS(0xDB, WKC_L_BRACKET), truelight@7310: AS(0xDC, WKC_BACKSLASH), truelight@7310: AS(0xDD, WKC_R_BRACKET), truelight@7310: truelight@7310: AS(0xDE, WKC_SINGLEQUOTE), truelight@7310: AS(0xBC, WKC_COMMA), truelight@7310: AS(0xBD, WKC_MINUS), truelight@7310: AS(0xBE, WKC_PERIOD) tron@2174: }; tron@2174: tron@2174: static uint MapWindowsKey(uint sym) tron@2174: { tron@2174: const VkMapping *map; tron@2174: uint key = 0; tron@2174: tron@2174: for (map = _vk_mapping; map != endof(_vk_mapping); ++map) { tron@2174: if ((uint)(sym - map->vk_from) <= map->vk_count) { tron@2174: key = sym - map->vk_from + map->map_to; tron@2174: break; tron@2174: } tron@2174: } tron@2174: tron@2174: if (GetAsyncKeyState(VK_SHIFT) < 0) key |= WKC_SHIFT; tron@2174: if (GetAsyncKeyState(VK_CONTROL) < 0) key |= WKC_CTRL; tron@2174: if (GetAsyncKeyState(VK_MENU) < 0) key |= WKC_ALT; tron@2174: return key; tron@2174: } tron@2174: tron@2174: static bool AllocateDibSection(int w, int h); tron@2174: tron@2174: static void ClientSizeChanged(int w, int h) tron@2174: { tron@2174: // allocate new dib section of the new size tron@2174: if (AllocateDibSection(w, h)) { tron@2174: // mark all palette colors dirty tron@2174: _pal_first_dirty = 0; glx@7392: _pal_count_dirty = 256; tron@2174: GameSizeChanged(); tron@2174: tron@2174: // redraw screen tron@2174: if (_wnd.running) { tron@2174: _screen.dst_ptr = _wnd.buffer_bits; tron@2174: UpdateWindows(); tron@2174: } tron@2174: } tron@2174: } tron@2174: tron@2174: #ifdef _DEBUG tron@2174: // Keep this function here.. tron@2174: // It allows you to redraw the screen from within the MSVC debugger rubidium@6247: int RedrawScreenDebug() tron@2174: { tron@2174: HDC dc,dc2; tron@2174: static int _fooctr; tron@2174: HBITMAP old_bmp; tron@2174: HPALETTE old_palette; tron@2174: tron@2174: _screen.dst_ptr = _wnd.buffer_bits; tron@2174: UpdateWindows(); tron@2174: tron@2174: dc = GetDC(_wnd.main_wnd); tron@2174: dc2 = CreateCompatibleDC(dc); tron@2174: rubidium@5587: old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect); tron@2174: old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE); tron@2174: BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY); tron@2174: SelectPalette(dc, old_palette, TRUE); tron@2174: SelectObject(dc2, old_bmp); tron@2174: DeleteDC(dc2); tron@2174: ReleaseDC(_wnd.main_wnd, dc); tron@2174: tron@2174: return _fooctr++; tron@2174: } tron@2174: #endif tron@2174: Darkvater@5021: /* Windows 95 will not have a WM_MOUSELEAVE message, so define it if needed */ Darkvater@3312: #if !defined(WM_MOUSELEAVE) Darkvater@3312: #define WM_MOUSELEAVE 0x02A3 Darkvater@3312: #endif Darkvater@3312: #define TID_POLLMOUSE 1 Darkvater@3312: #define MOUSE_POLL_DELAY 75 Darkvater@3312: Darkvater@3312: static void CALLBACK TrackMouseTimerProc(HWND hwnd, UINT msg, UINT event, DWORD time) Darkvater@3312: { Darkvater@3312: RECT rc; Darkvater@3312: POINT pt; Darkvater@3312: Darkvater@3312: /* Get the rectangle of our window and translate it to screen coordinates. Darkvater@3312: * Compare this with the current screen coordinates of the mouse and if it Darkvater@3312: * falls outside of the area or our window we have left the window. */ Darkvater@3312: GetClientRect(hwnd, &rc); glx@3802: MapWindowPoints(hwnd, HWND_DESKTOP, (LPPOINT)(LPRECT)&rc, 2); Darkvater@3312: GetCursorPos(&pt); Darkvater@3312: Darkvater@3312: if (!PtInRect(&rc, pt) || (WindowFromPoint(pt) != hwnd)) { Darkvater@3312: KillTimer(hwnd, event); Darkvater@3312: PostMessage(hwnd, WM_MOUSELEAVE, 0, 0L); Darkvater@3312: } Darkvater@3312: } Darkvater@3312: belugas@8171: static bool MakeWindow(bool full_screen) glx@6995: { glx@6995: _fullscreen = full_screen; glx@6995: glx@6995: // recreate window? glx@6995: if ((full_screen || _wnd.fullscreen) && _wnd.main_wnd) { rubidium@7429: DestroyWindow(_wnd.main_wnd); glx@6995: _wnd.main_wnd = 0; glx@6995: } glx@6995: glx@6995: #if defined(WINCE) glx@6995: /* WinCE is always fullscreen */ glx@6995: #else glx@6995: if (full_screen) { glx@6995: DEVMODE settings; glx@6995: glx@6995: /* Make sure we are always at least the screen-depth of the blitter */ glx@6995: if (_fullscreen_bpp < BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth()) _fullscreen_bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(); glx@6995: glx@6995: memset(&settings, 0, sizeof(settings)); glx@6995: settings.dmSize = sizeof(settings); glx@6995: settings.dmFields = glx@6995: (_fullscreen_bpp != 0 ? DM_BITSPERPEL : 0) | glx@6995: DM_PELSWIDTH | glx@6995: DM_PELSHEIGHT | glx@6995: (_display_hz != 0 ? DM_DISPLAYFREQUENCY : 0); glx@6995: settings.dmBitsPerPel = _fullscreen_bpp; glx@6995: settings.dmPelsWidth = _wnd.width_org; glx@6995: settings.dmPelsHeight = _wnd.height_org; glx@6995: settings.dmDisplayFrequency = _display_hz; glx@6995: glx@6995: if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) { belugas@8171: MakeWindow(false); // don't care about the result belugas@8171: return false; // the request failed glx@6995: } glx@6995: } else if (_wnd.fullscreen) { glx@6995: // restore display? glx@6995: ChangeDisplaySettings(NULL, 0); glx@6995: } glx@6995: #endif glx@6995: glx@6995: { glx@6995: RECT r; glx@6995: DWORD style, showstyle; glx@6995: int x, y, w, h; glx@6995: glx@6995: showstyle = SW_SHOWNORMAL; glx@6995: _wnd.fullscreen = full_screen; glx@6995: if (_wnd.fullscreen) { glx@6995: style = WS_POPUP; glx@6995: SetRect(&r, 0, 0, _wnd.width_org, _wnd.height_org); glx@6995: } else { glx@6995: style = WS_OVERLAPPEDWINDOW; glx@6995: /* On window creation, check if we were in maximize mode before */ glx@6995: if (_window_maximize) showstyle = SW_SHOWMAXIMIZED; glx@6995: SetRect(&r, 0, 0, _wnd.width, _wnd.height); glx@6995: } glx@6995: glx@6995: #if !defined(WINCE) glx@6995: AdjustWindowRect(&r, style, FALSE); glx@6995: #endif glx@6995: w = r.right - r.left; glx@6995: h = r.bottom - r.top; glx@9939: x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2; glx@9939: y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2; glx@6995: glx@6995: if (_wnd.main_wnd) { glx@6995: ShowWindow(_wnd.main_wnd, SW_SHOWNORMAL); // remove maximize-flag glx@6995: SetWindowPos(_wnd.main_wnd, 0, x, y, w, h, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER); glx@6995: } else { glx@6995: TCHAR Windowtitle[50]; glx@6995: glx@6995: _sntprintf(Windowtitle, sizeof(Windowtitle), _T("OpenTTD %s"), MB_TO_WIDE(_openttd_revision)); glx@6995: glx@6995: _wnd.main_wnd = CreateWindow(_T("OTTD"), Windowtitle, style, x, y, w, h, 0, 0, GetModuleHandle(NULL), 0); glx@9470: if (_wnd.main_wnd == NULL) usererror("CreateWindow failed"); glx@6995: ShowWindow(_wnd.main_wnd, showstyle); glx@6995: } glx@6995: } glx@6995: GameSizeChanged(); // invalidate all windows, force redraw belugas@8171: return true; // the request succedded glx@6995: } glx@6995: tron@2174: static LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) tron@2174: { Darkvater@6277: static uint32 keycode = 0; glx@6539: static bool console = false; Darkvater@6277: tron@2174: switch (msg) { tron@4489: case WM_CREATE: tron@4489: SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc); tron@4489: break; tron@2174: tron@4489: case WM_PAINT: { tron@4489: PAINTSTRUCT ps; rubidium@8969: HDC dc, dc2; tron@4489: HBITMAP old_bmp; tron@4489: HPALETTE old_palette; tron@2174: tron@4489: BeginPaint(hwnd, &ps); tron@4489: dc = ps.hdc; tron@4489: dc2 = CreateCompatibleDC(dc); rubidium@5587: old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect); tron@4489: old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE); Darkvater@3312: truelight@6960: if (_pal_count_dirty != 0) { truelight@6961: Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter(); truelight@6961: truelight@6960: switch (blitter->UsePaletteAnimation()) { truelight@6960: case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND: glx@6962: UpdatePalette(dc2, _pal_first_dirty, _pal_count_dirty); truelight@6960: break; truelight@6960: truelight@6960: case Blitter::PALETTE_ANIMATION_BLITTER: truelight@6960: blitter->PaletteAnimate(_pal_first_dirty, _pal_count_dirty); truelight@6960: break; truelight@6960: truelight@6960: case Blitter::PALETTE_ANIMATION_NONE: truelight@6960: break; truelight@6960: truelight@6960: default: truelight@6960: NOT_REACHED(); truelight@6960: } truelight@6960: _pal_count_dirty = 0; tron@4489: } tron@2174: tron@4489: BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY); tron@4489: SelectPalette(dc, old_palette, TRUE); tron@4489: SelectObject(dc2, old_bmp); tron@4489: DeleteDC(dc2); tron@4489: EndPaint(hwnd, &ps); tron@4489: return 0; tron@2174: } tron@2174: tron@4489: case WM_PALETTECHANGED: tron@4489: if ((HWND)wParam == hwnd) return 0; tron@4489: /* FALLTHROUGH */ tron@2174: tron@4489: case WM_QUERYNEWPALETTE: { tron@4489: HDC hDC = GetWindowDC(hwnd); tron@4489: HPALETTE hOldPalette = SelectPalette(hDC, _wnd.gdi_palette, FALSE); tron@4489: UINT nChanged = RealizePalette(hDC); tron@2174: tron@4489: SelectPalette(hDC, hOldPalette, TRUE); tron@4489: ReleaseDC(hwnd, hDC); tron@4489: if (nChanged) InvalidateRect(hwnd, NULL, FALSE); tron@4489: return 0; tron@2174: } tron@2174: tron@4489: case WM_CLOSE: rubidium@4548: HandleExitGameRequest(); tron@2174: return 0; tron@4489: Darkvater@5020: case WM_DESTROY: smatz@9533: if (_window_maximize) _cur_resolution = _bck_resolution; Darkvater@5020: return 0; Darkvater@5020: tron@4489: case WM_LBUTTONDOWN: tron@4489: SetCapture(hwnd); tron@4489: _left_button_down = true; Darkvater@5090: HandleMouseEvents(); tron@2174: return 0; tron@4489: tron@4489: case WM_LBUTTONUP: tron@4489: ReleaseCapture(); tron@4489: _left_button_down = false; tron@4489: _left_button_clicked = false; Darkvater@5090: HandleMouseEvents(); tron@4489: return 0; tron@4489: tron@4489: case WM_RBUTTONDOWN: tron@4489: SetCapture(hwnd); tron@4489: _right_button_down = true; tron@4489: _right_button_clicked = true; Darkvater@5090: HandleMouseEvents(); tron@4489: return 0; tron@4489: tron@4489: case WM_RBUTTONUP: tron@4489: ReleaseCapture(); tron@4489: _right_button_down = false; Darkvater@5090: HandleMouseEvents(); tron@4489: return 0; tron@4489: tron@4489: case WM_MOUSELEAVE: tron@4489: UndrawMouseCursor(); tron@4489: _cursor.in_window = false; Darkvater@5021: Darkvater@5021: if (!_left_button_down && !_right_button_down) MyShowCursor(true); Darkvater@5090: HandleMouseEvents(); Darkvater@5021: return 0; tron@4489: tron@4489: case WM_MOUSEMOVE: { tron@4489: int x = (int16)LOWORD(lParam); tron@4489: int y = (int16)HIWORD(lParam); tron@4489: POINT pt; tron@4489: tron@4489: /* If the mouse was not in the window and it has moved it means it has Darkvater@5021: * come into the window, so start drawing the mouse. Also start tron@4489: * tracking the mouse for exiting the window */ tron@4489: if (!_cursor.in_window) { tron@4489: _cursor.in_window = true; tron@4489: SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc); tron@4489: Darkvater@5021: DrawMouseCursor(); tron@4489: } tron@4489: tron@4489: if (_cursor.fix_at) { tron@4489: int dx = x - _cursor.pos.x; tron@4489: int dy = y - _cursor.pos.y; tron@4489: if (dx != 0 || dy != 0) { tron@4489: _cursor.delta.x += dx; tron@4489: _cursor.delta.y += dy; tron@4489: tron@4489: pt.x = _cursor.pos.x; tron@4489: pt.y = _cursor.pos.y; tron@4489: tron@4489: ClientToScreen(hwnd, &pt); tron@4489: SetCursorPos(pt.x, pt.y); tron@4489: } tron@4489: } else { tron@4489: _cursor.delta.x += x - _cursor.pos.x; tron@4489: _cursor.delta.y += y - _cursor.pos.y; tron@4489: _cursor.pos.x = x; tron@4489: _cursor.pos.y = y; tron@4489: _cursor.dirty = true; tron@4489: } tron@4489: MyShowCursor(false); Darkvater@5090: HandleMouseEvents(); tron@4489: return 0; tron@4489: } tron@4489: Darkvater@6251: #if !defined(UNICODE) Darkvater@6251: case WM_INPUTLANGCHANGE: { Darkvater@6251: TCHAR locale[6]; Darkvater@6251: LCID lcid = GB(lParam, 0, 16); Darkvater@6251: Darkvater@6251: int len = GetLocaleInfo(lcid, LOCALE_IDEFAULTANSICODEPAGE, locale, lengthof(locale)); Darkvater@6251: if (len != 0) _codepage = _ttoi(locale); Darkvater@6251: return 1; Darkvater@6251: } Darkvater@6251: #endif /* UNICODE */ Darkvater@6251: glx@6539: case WM_DEADCHAR: glx@6539: console = GB(lParam, 16, 8) == 41; glx@6539: return 0; glx@6539: Darkvater@6277: case WM_CHAR: { Darkvater@6277: /* Silently drop all non-text messages as those were handled by WM_KEYDOWN */ Darkvater@6277: if (wParam < VK_SPACE) return 0; Darkvater@6277: uint scancode = GB(lParam, 16, 8); Darkvater@6277: uint charcode = wParam; tron@4489: glx@6539: /* If the console key is a dead-key, we need to press it twice to get a WM_CHAR message. glx@6539: * But we then get two WM_CHAR messages, so ignore the first one */ glx@6539: if (console && scancode == 41) { glx@6539: console = false; glx@6539: return 0; glx@6539: } glx@6539: Darkvater@6277: #if !defined(UNICODE) Darkvater@6277: wchar_t w; Darkvater@6277: int len = MultiByteToWideChar(_codepage, 0, (char*)&charcode, 1, &w, 1); Darkvater@6277: charcode = len == 1 ? w : 0; Darkvater@6277: #endif /* UNICODE */ tron@4489: Darkvater@6277: /* No matter the keyboard layout, we will map the '~' to the console */ Darkvater@6277: scancode = scancode == 41 ? (int)WKC_BACKQUOTE : keycode; Darkvater@6277: HandleKeypress(GB(charcode, 0, 16) | (scancode << 16)); Darkvater@6277: return 0; Darkvater@6277: } tron@4489: Darkvater@6277: case WM_KEYDOWN: { Darkvater@6277: keycode = MapWindowsKey(wParam); tron@4489: Darkvater@6277: /* Silently drop all text messages as those will be handled by WM_CHAR Darkvater@6277: * WM_KEYDOWN only handles CTRL+ commands and special keys like VK_LEFT, etc. */ Darkvater@6277: if (keycode == 0 || (keycode > WKC_PAUSE && GB(keycode, 13, 4) == 0)) return 0; Darkvater@6277: glx@7541: /* Keys handled in WM_CHAR */ glx@7541: if ((uint)(GB(keycode, 0, 12) - WKC_NUM_DIV) <= WKC_MINUS - WKC_NUM_DIV) return 0; glx@7541: Darkvater@6277: HandleKeypress(0 | (keycode << 16)); Darkvater@6277: return 0; tron@2174: } tron@2174: tron@4489: case WM_SYSKEYDOWN: /* user presses F10 or Alt, both activating the title-menu */ tron@4489: switch (wParam) { tron@4489: case VK_RETURN: tron@4489: case 'F': /* Full Screen on ALT + ENTER/F */ tron@4489: ToggleFullScreen(!_wnd.fullscreen); tron@4489: return 0; tron@2174: tron@4489: case VK_MENU: /* Just ALT */ tron@4489: return 0; // do nothing tron@4489: tron@4489: case VK_F10: /* F10, ignore activation of menu */ Darkvater@5086: HandleKeypress(MapWindowsKey(wParam) << 16); tron@4489: return 0; tron@4489: tron@4489: default: /* ALT in combination with something else */ Darkvater@5086: HandleKeypress(MapWindowsKey(wParam) << 16); tron@4489: break; tron@4489: } tron@2174: break; tron@4489: tron@4489: case WM_SIZE: glx@7126: if (wParam != SIZE_MINIMIZED) { Darkvater@5019: /* Set maximized flag when we maximize (obviously), but also when we Darkvater@5019: * switched to fullscreen from a maximized state */ Darkvater@5019: _window_maximize = (wParam == SIZE_MAXIMIZED || (_window_maximize && _fullscreen)); smatz@9533: if (_window_maximize) _bck_resolution = _cur_resolution; tron@4489: ClientSizeChanged(LOWORD(lParam), HIWORD(lParam)); tron@4489: } tron@4489: return 0; tron@4489: truelight@5758: #if !defined(WINCE) tron@4489: case WM_SIZING: { tron@4489: RECT* r = (RECT*)lParam; tron@4489: RECT r2; tron@4489: int w, h; tron@4489: tron@4489: SetRect(&r2, 0, 0, 0, 0); tron@4489: AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE); tron@4489: tron@4489: w = r->right - r->left - (r2.right - r2.left); tron@4489: h = r->bottom - r->top - (r2.bottom - r2.top); rubidium@8985: w = max(w, 64); rubidium@8985: h = max(h, 64); tron@4489: SetRect(&r2, 0, 0, w, h); tron@4489: tron@4489: AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE); tron@4489: w = r2.right - r2.left; tron@4489: h = r2.bottom - r2.top; tron@4489: tron@4489: switch (wParam) { tron@4489: case WMSZ_BOTTOM: tron@4489: r->bottom = r->top + h; tron@4489: break; tron@4489: tron@4489: case WMSZ_BOTTOMLEFT: tron@4489: r->bottom = r->top + h; tron@4489: r->left = r->right - w; tron@4489: break; tron@4489: tron@4489: case WMSZ_BOTTOMRIGHT: tron@4489: r->bottom = r->top + h; tron@4489: r->right = r->left + w; tron@4489: break; tron@4489: tron@4489: case WMSZ_LEFT: tron@4489: r->left = r->right - w; tron@4489: break; tron@4489: tron@4489: case WMSZ_RIGHT: tron@4489: r->right = r->left + w; tron@4489: break; tron@4489: tron@4489: case WMSZ_TOP: tron@4489: r->top = r->bottom - h; tron@4489: break; tron@4489: tron@4489: case WMSZ_TOPLEFT: tron@4489: r->top = r->bottom - h; tron@4489: r->left = r->right - w; tron@4489: break; tron@4489: tron@4489: case WMSZ_TOPRIGHT: tron@4489: r->top = r->bottom - h; tron@4489: r->right = r->left + w; tron@4489: break; tron@4489: } tron@4489: return TRUE; tron@2174: } truelight@5759: #endif tron@2174: tron@2174: // needed for wheel tron@2174: #if !defined(WM_MOUSEWHEEL) tron@4489: # define WM_MOUSEWHEEL 0x020A tron@2174: #endif //WM_MOUSEWHEEL tron@2174: #if !defined(GET_WHEEL_DELTA_WPARAM) tron@2174: # define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam)) tron@2174: #endif //GET_WHEEL_DELTA_WPARAM tron@2174: tron@4489: case WM_MOUSEWHEEL: { tron@4489: int delta = GET_WHEEL_DELTA_WPARAM(wParam); tron@2174: tron@4489: if (delta < 0) { tron@4489: _cursor.wheel++; tron@4489: } else if (delta > 0) { tron@4489: _cursor.wheel--; tron@4489: } Darkvater@5090: HandleMouseEvents(); tron@4489: return 0; tron@2174: } tron@2174: glx@7126: case WM_SETFOCUS: glx@7126: _wnd.has_focus = true; glx@7126: break; glx@7126: glx@7126: case WM_KILLFOCUS: glx@7126: _wnd.has_focus = false; glx@7126: break; glx@7126: glx@6933: #if !defined(WINCE) glx@7126: case WM_ACTIVATE: { glx@7430: /* Don't do anything if we are closing openttd */ glx@7430: if (_exit_game) break; glx@7430: glx@7126: bool active = (LOWORD(wParam) != WA_INACTIVE); glx@7126: bool minimized = (HIWORD(wParam) != 0); glx@6933: if (_wnd.fullscreen) { glx@7126: if (active && minimized) { glx@6933: /* Restore the game window */ glx@6933: ShowWindow(hwnd, SW_RESTORE); glx@6995: MakeWindow(true); glx@7126: } else if (!active && !minimized) { glx@6933: /* Minimise the window and restore desktop */ glx@6933: ShowWindow(hwnd, SW_MINIMIZE); glx@6933: ChangeDisplaySettings(NULL, 0); glx@6933: } glx@6933: } truelight@7409: } break; truelight@7409: #endif glx@7126: } glx@7126: tron@2174: return DefWindowProc(hwnd, msg, wParam, lParam); tron@2174: } tron@2174: rubidium@6247: static void RegisterWndClass() tron@2174: { tron@4489: static bool registered = false; tron@4489: tron@2174: if (!registered) { tron@2174: HINSTANCE hinst = GetModuleHandle(NULL); tron@2174: WNDCLASS wnd = { tron@2174: 0, tron@2174: WndProcGdi, tron@2174: 0, tron@2174: 0, tron@2174: hinst, tron@2174: LoadIcon(hinst, MAKEINTRESOURCE(100)), tron@2174: LoadCursor(NULL, IDC_ARROW), tron@2174: 0, tron@2174: 0, Darkvater@5168: _T("OTTD") tron@2174: }; tron@4489: tron@2174: registered = true; glx@9470: if (!RegisterClass(&wnd)) usererror("RegisterClass failed"); tron@2174: } tron@2174: } tron@2174: tron@2174: static bool AllocateDibSection(int w, int h) tron@2174: { tron@2174: BITMAPINFO *bi; tron@2174: HDC dc; truelight@6878: int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(); tron@2174: rubidium@8985: w = max(w, 64); rubidium@8985: h = max(h, 64); tron@2174: glx@9470: if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals"); truelight@6878: tron@2174: if (w == _screen.width && h == _screen.height) tron@2174: return false; tron@2174: tron@2174: _screen.width = w; skidd13@7927: _screen.pitch = (bpp == 8) ? Align(w, 4) : w; tron@2174: _screen.height = h; rubidium@6491: bi = (BITMAPINFO*)alloca(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256); rubidium@6491: memset(bi, 0, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256); tron@2174: bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); tron@2174: tron@2174: bi->bmiHeader.biWidth = _wnd.width = w; tron@2174: bi->bmiHeader.biHeight = -(_wnd.height = h); tron@2174: tron@2174: bi->bmiHeader.biPlanes = 1; truelight@6878: bi->bmiHeader.biBitCount = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(); tron@2174: bi->bmiHeader.biCompression = BI_RGB; tron@2174: tron@4489: if (_wnd.dib_sect) DeleteObject(_wnd.dib_sect); tron@2174: tron@2174: dc = GetDC(0); truelight@6878: _wnd.dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID**)&_wnd.buffer_bits, NULL, 0); glx@9470: if (_wnd.dib_sect == NULL) usererror("CreateDIBSection failed"); tron@2174: ReleaseDC(0, dc); tron@2174: tron@2174: return true; tron@2174: } tron@2174: smatz@9533: static const Dimension default_resolutions[] = { tron@4489: { 640, 480 }, tron@4489: { 800, 600 }, tron@4489: { 1024, 768 }, tron@4489: { 1152, 864 }, tron@4489: { 1280, 800 }, tron@4489: { 1280, 960 }, tron@4489: { 1280, 1024 }, tron@4489: { 1400, 1050 }, tron@4489: { 1600, 1200 }, tron@4489: { 1680, 1050 }, tron@4489: { 1920, 1200 } tron@2174: }; tron@2174: rubidium@6247: static void FindResolutions() tron@2174: { tron@4000: uint n = 0; truelight@5758: #if defined(WINCE) truelight@5758: /* EnumDisplaySettingsW is only supported in CE 4.2+ */ truelight@5758: /* XXX -- One might argue that we assume 4.2+ on every system. Then we can use this function safely */ truelight@5758: #else tron@4000: uint i; Darkvater@5169: DEVMODEA dm; tron@2174: Darkvater@5169: /* XXX - EnumDisplaySettingsW crashes with unicows.dll on Windows95 Darkvater@5169: * Doesn't really matter since we don't pass a string anyways, but still Darkvater@5169: * a letdown */ Darkvater@5169: for (i = 0; EnumDisplaySettingsA(NULL, i, &dm) != 0; i++) { rubidium@8985: if (dm.dmBitsPerPel == BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() && rubidium@8985: dm.dmPelsWidth >= 640 && dm.dmPelsHeight >= 480) { tron@4000: uint j; tron@4000: tron@2174: for (j = 0; j < n; j++) { glx@9535: if (_resolutions[j].width == (int)dm.dmPelsWidth && _resolutions[j].height == (int)dm.dmPelsHeight) break; tron@2174: } tron@2174: tron@2174: /* In the previous loop we have checked already existing/added resolutions if tron@2174: * they are the same as the new ones. If this is not the case (j == n); we have tron@2174: * looped all and found none, add the new one to the list. If we have reached the tron@2174: * maximum amount of resolutions, then quit querying the display */ tron@2174: if (j == n) { smatz@9533: _resolutions[j].width = dm.dmPelsWidth; smatz@9533: _resolutions[j].height = dm.dmPelsHeight; tron@2174: if (++n == lengthof(_resolutions)) break; tron@2174: } tron@2174: } tron@2174: } truelight@5758: #endif tron@2174: tron@2174: /* We have found no resolutions, show the default list */ tron@2174: if (n == 0) { tron@2174: memcpy(_resolutions, default_resolutions, sizeof(default_resolutions)); tron@2174: n = lengthof(default_resolutions); tron@2174: } tron@2174: tron@2174: _num_resolutions = n; tron@2174: SortResolutions(_num_resolutions); tron@2174: } tron@2174: peter1138@7170: static FVideoDriver_Win32 iFVideoDriver_Win32; tron@2174: peter1138@7170: const char *VideoDriver_Win32::Start(const char * const *parm) tron@2174: { tron@2174: memset(&_wnd, 0, sizeof(_wnd)); tron@2174: tron@2174: RegisterWndClass(); tron@2174: tron@2174: MakePalette(); tron@2174: tron@2174: FindResolutions(); tron@2174: smatz@9533: DEBUG(driver, 2, "Resolution for display: %dx%d", _cur_resolution.width, _cur_resolution.height); truelight@7408: tron@2174: // fullscreen uses those smatz@9533: _wnd.width_org = _cur_resolution.width; smatz@9533: _wnd.height_org = _cur_resolution.height; tron@2174: smatz@9533: AllocateDibSection(_cur_resolution.width, _cur_resolution.height); rubidium@8985: MakeWindow(_fullscreen); rubidium@8985: tron@2174: MarkWholeScreenDirty(); tron@2174: tron@2174: return NULL; tron@2174: } tron@2174: peter1138@7170: void VideoDriver_Win32::Stop() tron@2174: { Darkvater@3285: DeleteObject(_wnd.gdi_palette); Darkvater@3285: DeleteObject(_wnd.dib_sect); rubidium@7429: DestroyWindow(_wnd.main_wnd); Darkvater@3285: truelight@5758: #if !defined(WINCE) tron@2174: if (_wnd.fullscreen) ChangeDisplaySettings(NULL, 0); truelight@5758: #endif tron@2174: MyShowCursor(true); tron@2174: } tron@2174: peter1138@7170: void VideoDriver_Win32::MakeDirty(int left, int top, int width, int height) tron@2174: { tron@2174: RECT r = { left, top, left + width, top + height }; tron@2174: tron@2174: InvalidateRect(_wnd.main_wnd, &r, FALSE); tron@2174: } tron@2174: rubidium@6247: static void CheckPaletteAnim() tron@2174: { glx@6962: if (_pal_count_dirty == 0) tron@2174: return; tron@2174: InvalidateRect(_wnd.main_wnd, NULL, FALSE); tron@2174: } tron@2174: peter1138@7170: void VideoDriver_Win32::MainLoop() tron@2174: { tron@2174: MSG mesg; rubidium@5581: uint32 cur_ticks = GetTickCount(); truelight@7018: uint32 last_cur_ticks = cur_ticks; rubidium@5581: uint32 next_tick = cur_ticks + 30; tron@2174: tron@2174: _wnd.running = true; tron@2174: tron@2952: for (;;) { rubidium@5581: uint32 prev_cur_ticks = cur_ticks; // to check for wrapping rubidium@5581: tron@2174: while (PeekMessage(&mesg, NULL, 0, 0, PM_REMOVE)) { tron@2174: InteractiveRandom(); // randomness Darkvater@6277: TranslateMessage(&mesg); tron@2174: DispatchMessage(&mesg); tron@2174: } tron@2228: if (_exit_game) return; tron@2174: tron@2174: #if defined(_DEBUG) Darkvater@5089: if (_wnd.has_focus && GetAsyncKeyState(VK_SHIFT) < 0 && tron@2174: #else Darkvater@5089: /* Speed up using TAB, but disable for ALT+TAB of course */ Darkvater@5089: if (_wnd.has_focus && GetAsyncKeyState(VK_TAB) < 0 && GetAsyncKeyState(VK_MENU) >= 0 && tron@2174: #endif Darkvater@5089: !_networking && _game_mode != GM_MENU) { Darkvater@5089: _fast_forward |= 2; tron@4077: } else if (_fast_forward & 2) { tron@2174: _fast_forward = 0; tron@4077: } tron@2174: tron@2174: cur_ticks = GetTickCount(); truelight@6231: if (cur_ticks >= next_tick || (_fast_forward && !_pause_game) || cur_ticks < prev_cur_ticks) { truelight@7019: _realtime_tick += cur_ticks - last_cur_ticks; truelight@7019: last_cur_ticks = cur_ticks; rubidium@5581: next_tick = cur_ticks + 30; smatz@8586: smatz@8586: bool old_ctrl_pressed = _ctrl_pressed; smatz@8586: tron@2174: _ctrl_pressed = _wnd.has_focus && GetAsyncKeyState(VK_CONTROL)<0; tron@2174: _shift_pressed = _wnd.has_focus && GetAsyncKeyState(VK_SHIFT)<0; tron@2174: tron@2174: // determine which directional keys are down tron@2174: if (_wnd.has_focus) { tron@2174: _dirkeys = tron@2174: (GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) + tron@2174: (GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) + tron@2174: (GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) + tron@2174: (GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0); tron@4077: } else { tron@2174: _dirkeys = 0; tron@4077: } tron@2174: smatz@8586: if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged(); smatz@8586: tron@2174: GameLoop(); tron@2174: _cursor.delta.x = _cursor.delta.y = 0; tron@2174: tron@4077: if (_force_full_redraw) MarkWholeScreenDirty(); tron@2174: truelight@5758: #if !defined(WINCE) tron@2174: GdiFlush(); truelight@5758: #endif tron@2174: _screen.dst_ptr = _wnd.buffer_bits; tron@2174: UpdateWindows(); tron@2174: CheckPaletteAnim(); tron@2174: } else { tron@2174: Sleep(1); truelight@5758: #if !defined(WINCE) tron@2174: GdiFlush(); truelight@5758: #endif tron@2174: _screen.dst_ptr = _wnd.buffer_bits; rubidium@9898: NetworkDrawChatMessage(); tron@2174: DrawMouseCursor(); tron@2174: } tron@2174: } tron@2174: } tron@2174: peter1138@7170: bool VideoDriver_Win32::ChangeResolution(int w, int h) tron@2174: { tron@2174: _wnd.width = _wnd.width_org = w; tron@2174: _wnd.height = _wnd.height_org = h; tron@2174: belugas@8171: return MakeWindow(_fullscreen); // _wnd.fullscreen screws up ingame resolution switching tron@2174: } tron@2174: belugas@8171: bool VideoDriver_Win32::ToggleFullscreen(bool full_screen) tron@4489: { belugas@8171: return MakeWindow(full_screen); tron@4489: }