truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" tron@1302: #include "debug.h" tron@1317: #include "string.h" tron@507: #include "table/strings.h" truelight@0: #include "gfx.h" tron@1496: #include "mixer.h" truelight@0: #include "window.h" Darkvater@1390: #include "gui.h" truelight@0: #include truelight@0: #include truelight@0: #include "hal.h" truelight@0: #include truelight@0: #include truelight@0: #include truelight@0: #include truelight@543: #include "network.h" truelight@0: truelight@0: #define SMART_PALETTE_ANIM truelight@0: truelight@0: static struct { truelight@0: HWND main_wnd; truelight@0: HBITMAP dib_sect; truelight@0: void *bitmap_bits; truelight@0: void *buffer_bits; truelight@0: void *alloced_bits; truelight@0: HPALETTE gdi_palette; truelight@0: int width,height; truelight@0: int width_org, height_org; truelight@0: bool cursor_visible; truelight@0: bool switch_driver; truelight@0: bool fullscreen; truelight@0: bool double_size; truelight@0: bool has_focus; truelight@0: bool running; truelight@0: } _wnd; truelight@0: truelight@0: static HINSTANCE _inst; truelight@0: static bool _has_console; truelight@0: darkvater@796: #if defined(__MINGW32__) || defined(__CYGWIN__) truelight@0: #define __TIMESTAMP__ __DATE__ __TIME__ truelight@0: #endif truelight@0: truelight@0: #ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT truelight@0: extern const HalMusicDriver _dmusic_midi_driver; truelight@0: #endif truelight@0: darkvater@1102: static void MakePalette(void) truelight@0: { truelight@0: LOGPALETTE *pal; tron@1468: uint i; truelight@0: byte *b; truelight@0: truelight@0: pal = alloca(sizeof(LOGPALETTE) + (256-1) * sizeof(PALETTEENTRY)); truelight@0: truelight@0: pal->palVersion = 0x300; truelight@0: pal->palNumEntries = 256; truelight@193: tron@1468: for (i = 0, b = _cur_palette; i != 256; i++, b += 3) { truelight@0: pal->palPalEntry[i].peRed = b[0]; truelight@0: pal->palPalEntry[i].peGreen = b[1]; truelight@0: pal->palPalEntry[i].peBlue = b[2]; truelight@0: pal->palPalEntry[i].peFlags = 0; truelight@0: truelight@0: } truelight@0: _wnd.gdi_palette = CreatePalette(pal); truelight@193: if (_wnd.gdi_palette == NULL) truelight@0: error("CreatePalette failed!\n"); truelight@0: } truelight@0: truelight@0: static void UpdatePalette(HDC dc, uint start, uint count) truelight@0: { truelight@0: RGBQUAD rgb[256]; truelight@0: uint i; truelight@0: byte *b; truelight@0: tron@1468: for (i = 0, b = _cur_palette + start * 3; i != count; i++, b += 3) { truelight@0: rgb[i].rgbRed = b[0]; truelight@0: rgb[i].rgbGreen = b[1]; truelight@0: rgb[i].rgbBlue = b[2]; truelight@0: rgb[i].rgbReserved = 0; truelight@0: } truelight@0: truelight@0: SetDIBColorTable(dc, start, count, rgb); truelight@0: } truelight@0: truelight@0: static bool MyShowCursor(bool show) truelight@0: { truelight@0: if (_wnd.cursor_visible == show) truelight@0: return show; truelight@0: truelight@0: _wnd.cursor_visible = show; truelight@0: ShowCursor(show); truelight@0: truelight@0: return !show; truelight@0: } truelight@0: truelight@0: typedef struct { truelight@0: byte vk_from; truelight@0: byte vk_count; truelight@0: byte map_to; truelight@0: } VkMapping; truelight@0: tron@1468: #define AS(x, z) {x, 0, z} tron@1468: #define AM(x, y, z, w) {x, y - x, z} truelight@0: darkvater@153: #ifndef VK_OEM_3 darkvater@153: #define VK_OEM_3 0xC0 darkvater@153: #endif darkvater@153: truelight@0: static const VkMapping _vk_mapping[] = { truelight@0: // Pageup stuff + up/down truelight@0: AM(VK_PRIOR,VK_DOWN, WKC_PAGEUP, WKC_DOWN), truelight@0: // Map letters & digits truelight@0: AM('A','Z','A','Z'), truelight@0: AM('0','9','0','9'), truelight@0: truelight@0: AS(VK_ESCAPE, WKC_ESC), tron@424: AS(VK_PAUSE, WKC_PAUSE), truelight@0: AS(VK_BACK, WKC_BACKSPACE), truelight@0: AM(VK_INSERT,VK_DELETE,WKC_INSERT, WKC_DELETE), truelight@0: truelight@0: AS(VK_SPACE, WKC_SPACE), truelight@0: AS(VK_RETURN, WKC_RETURN), truelight@0: AS(VK_TAB, WKC_TAB), truelight@0: truelight@0: // Function keys truelight@0: AM(VK_F1, VK_F12, WKC_F1, WKC_F12), truelight@0: truelight@0: // Numeric part. truelight@0: // What is the virtual keycode for numeric enter?? truelight@0: AM(VK_NUMPAD0,VK_NUMPAD9, WKC_NUM_0, WKC_NUM_9), truelight@0: AS(VK_DIVIDE, WKC_NUM_DIV), truelight@0: AS(VK_MULTIPLY, WKC_NUM_MUL), truelight@0: AS(VK_SUBTRACT, WKC_NUM_MINUS), truelight@0: AS(VK_ADD, WKC_NUM_PLUS), tron@1466: AS(VK_DECIMAL, WKC_NUM_DECIMAL) truelight@0: }; truelight@0: tron@1466: static uint MapWindowsKey(uint sym) truelight@0: { tron@1466: const VkMapping *map; tron@1466: uint key = 0; truelight@0: tron@1466: for (map = _vk_mapping; map != endof(_vk_mapping); ++map) { tron@1466: if ((uint)(sym - map->vk_from) <= map->vk_count) { tron@1466: key = sym - map->vk_from + map->map_to; tron@1466: break; tron@1466: } tron@1466: } darkvater@135: truelight@0: if (GetAsyncKeyState(VK_SHIFT)<0) key |= WKC_SHIFT; truelight@0: if (GetAsyncKeyState(VK_CONTROL)<0) key |= WKC_CTRL; truelight@0: if (GetAsyncKeyState(VK_MENU)<0) key |= WKC_ALT; truelight@0: return key; truelight@0: } truelight@0: truelight@0: static void MakeWindow(bool full_screen); truelight@0: static bool AllocateDibSection(int w, int h); truelight@0: truelight@0: static void ClientSizeChanged(int w, int h) truelight@0: { tron@1468: if (_wnd.double_size) { tron@1468: w /= 2; tron@1468: h /= 2; tron@1468: } truelight@193: truelight@0: // allocate new dib section of the new size truelight@0: if (AllocateDibSection(w, h)) { truelight@0: // mark all palette colors dirty truelight@0: _pal_first_dirty = 0; truelight@0: _pal_last_dirty = 255; truelight@0: GameSizeChanged(); truelight@193: truelight@0: // redraw screen truelight@0: if (_wnd.running) { truelight@0: _screen.dst_ptr = _wnd.buffer_bits; truelight@0: UpdateWindows(); truelight@0: } truelight@0: } truelight@0: } truelight@0: Darkvater@1881: extern void DoExitSave(void); dominik@643: truelight@0: static LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) truelight@0: { tron@1468: switch (msg) { truelight@0: case WM_PAINT: { truelight@0: PAINTSTRUCT ps; truelight@0: HDC dc,dc2; truelight@0: HBITMAP old_bmp; truelight@0: HPALETTE old_palette; truelight@0: BeginPaint(hwnd, &ps); truelight@0: dc = ps.hdc; truelight@0: dc2 = CreateCompatibleDC(dc); truelight@0: old_bmp = SelectObject(dc2, _wnd.dib_sect); truelight@0: old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE); truelight@193: truelight@0: if (_pal_last_dirty != -1) { truelight@0: UpdatePalette(dc2, _pal_first_dirty, _pal_last_dirty - _pal_first_dirty + 1); truelight@0: _pal_last_dirty = -1; truelight@0: } truelight@193: truelight@0: BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY); truelight@0: SelectPalette(dc, old_palette, TRUE); truelight@0: SelectObject(dc2, old_bmp); truelight@0: DeleteDC(dc2); truelight@0: EndPaint(hwnd, &ps); truelight@0: } truelight@0: return 0; truelight@0: truelight@0: case WM_PALETTECHANGED: truelight@0: if ((HWND)wParam == hwnd) truelight@0: return 0; truelight@0: // FALL THROUGH truelight@0: case WM_QUERYNEWPALETTE: { truelight@0: HDC hDC = GetWindowDC(hwnd); truelight@0: HPALETTE hOldPalette = SelectPalette(hDC, _wnd.gdi_palette, FALSE); truelight@0: UINT nChanged = RealizePalette(hDC); truelight@0: SelectPalette(hDC, hOldPalette, TRUE); truelight@0: ReleaseDC(hwnd, hDC); truelight@0: if (nChanged) truelight@0: InvalidateRect(hwnd, NULL, FALSE); truelight@0: return 0; truelight@0: } truelight@193: truelight@0: case WM_CLOSE: darkvater@646: if (_game_mode == GM_MENU) { // do not ask to quit on the main screen darkvater@646: _exit_game = true; darkvater@646: } else if (_patches.autosave_on_exit) { tron@1468: DoExitSave(); tron@1468: _exit_game = true; dominik@643: } else darkvater@646: AskExitGame(); darkvater@646: truelight@0: return 0; truelight@0: truelight@0: case WM_LBUTTONDOWN: truelight@0: SetCapture(hwnd); truelight@0: _left_button_down = true; truelight@0: return 0; truelight@0: truelight@0: case WM_LBUTTONUP: truelight@0: ReleaseCapture(); truelight@0: _left_button_down = false; truelight@0: _left_button_clicked = false; truelight@0: return 0; truelight@0: truelight@0: case WM_RBUTTONDOWN: truelight@0: SetCapture(hwnd); truelight@0: _right_button_down = true; truelight@0: _right_button_clicked = true; truelight@0: return 0; truelight@0: truelight@0: case WM_RBUTTONUP: truelight@0: ReleaseCapture(); truelight@0: _right_button_down = false; truelight@0: return 0; truelight@0: truelight@0: case WM_MOUSEMOVE: { truelight@0: int x = (int16)LOWORD(lParam); truelight@0: int y = (int16)HIWORD(lParam); truelight@0: POINT pt; truelight@0: truelight@0: if (_wnd.double_size) { tron@1468: x /= 2; tron@1468: y /= 2; truelight@0: } truelight@193: truelight@0: if (_cursor.fix_at) { truelight@0: int dx = x - _cursor.pos.x; truelight@0: int dy = y - _cursor.pos.y; truelight@0: if (dx != 0 || dy != 0) { truelight@0: _cursor.delta.x += dx; truelight@0: _cursor.delta.y += dy; truelight@0: truelight@0: pt.x = _cursor.pos.x; truelight@0: pt.y = _cursor.pos.y; truelight@0: truelight@0: if (_wnd.double_size) { truelight@0: pt.x *= 2; truelight@0: pt.y *= 2; truelight@0: } truelight@0: ClientToScreen(hwnd, &pt); truelight@0: SetCursorPos(pt.x, pt.y); truelight@0: } truelight@0: } else { truelight@0: _cursor.delta.x += x - _cursor.pos.x; truelight@0: _cursor.delta.y += y - _cursor.pos.y; truelight@0: _cursor.pos.x = x; truelight@0: _cursor.pos.y = y; truelight@0: _cursor.dirty = true; truelight@0: } truelight@0: MyShowCursor(false); truelight@0: return 0; truelight@0: } truelight@0: darkvater@394: case WM_KEYDOWN: { darkvater@135: // this is the rewritten ascii input function darkvater@135: // it disables windows deadkey handling --> more linux like :D pasky@1582: unsigned short w = 0; darkvater@135: int r = 0; darkvater@135: byte ks[256]; darkvater@394: unsigned int scan = 0; dominik@719: uint16 scancode = (( lParam & 0xFF0000 ) >> 16 ); dominik@719: darkvater@135: GetKeyboardState(ks); darkvater@394: r = ToAscii(wParam, scan, ks, &w, 0); darkvater@394: if (r == 0) w = 0; // no translation was possible darkvater@135: darkvater@135: _pressed_key = w | MapWindowsKey(wParam) << 16; darkvater@394: tron@1468: if (scancode == 41) dominik@719: _pressed_key = w | WKC_BACKQUOTE << 16; dominik@719: tron@1468: if ((_pressed_key >> 16) == ('D' | WKC_CTRL) && !_wnd.fullscreen) { truelight@0: _double_size ^= 1; truelight@0: _wnd.double_size = _double_size; truelight@0: ClientSizeChanged(_wnd.width, _wnd.height); truelight@0: MarkWholeScreenDirty(); truelight@0: } darkvater@394: } break; truelight@0: darkvater@198: case WM_SYSKEYDOWN: /* user presses F10 or Alt, both activating the title-menu */ tron@1468: switch (wParam) { Darkvater@1806: case VK_RETURN: case 0x46: /* Full Screen on ALT + ENTER/F(VK_F) */ Darkvater@1806: ToggleFullScreen(!_wnd.fullscreen); truelight@0: return 0; darkvater@199: case VK_MENU: /* Just ALT */ darkvater@199: return 0; // do nothing darkvater@1548: case VK_F10: /* F10, ignore activation of menu */ darkvater@1548: _pressed_key = MapWindowsKey(wParam) << 16; darkvater@1548: return 0; darkvater@199: default: /* ALT in combination with something else */ truelight@0: _pressed_key = MapWindowsKey(wParam) << 16; darkvater@1548: break; truelight@0: } truelight@0: break; truelight@0: case WM_NCMOUSEMOVE: truelight@0: MyShowCursor(true); truelight@0: return 0; truelight@0: truelight@0: case WM_SIZE: { truelight@0: if (wParam != SIZE_MINIMIZED) { truelight@0: ClientSizeChanged(LOWORD(lParam), HIWORD(lParam)); truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: case WM_SIZING: { truelight@0: RECT* r = (RECT*)lParam; truelight@0: RECT r2; truelight@0: int w, h; truelight@0: truelight@0: SetRect(&r2, 0, 0, 0, 0); truelight@0: AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE); truelight@193: truelight@0: w = r->right - r->left - (r2.right - r2.left); truelight@0: h = r->bottom - r->top - (r2.bottom - r2.top); tron@1468: if (_wnd.double_size) { tron@1468: w /= 2; tron@1468: h /= 2; tron@1468: } darkvater@306: w = clamp(w, 64, MAX_SCREEN_WIDTH); darkvater@306: h = clamp(h, 64, MAX_SCREEN_HEIGHT); tron@1468: if (_wnd.double_size) { tron@1468: w *= 2; tron@1468: h *= 2; tron@1468: } truelight@0: SetRect(&r2, 0, 0, w, h); truelight@0: truelight@0: AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE); truelight@0: w = r2.right - r2.left; truelight@0: h = r2.bottom - r2.top; truelight@0: truelight@0: switch (wParam) { truelight@0: case WMSZ_BOTTOM: truelight@0: r->bottom = r->top + h; truelight@0: break; truelight@0: case WMSZ_BOTTOMLEFT: truelight@0: r->bottom = r->top + h; truelight@0: r->left = r->right - w; truelight@0: break; truelight@0: case WMSZ_BOTTOMRIGHT: truelight@0: r->bottom = r->top + h; truelight@0: r->right = r->left + w; truelight@0: break; truelight@0: case WMSZ_LEFT: truelight@0: r->left = r->right - w; truelight@0: break; truelight@0: case WMSZ_RIGHT: truelight@0: r->right = r->left + w; truelight@0: break; truelight@0: case WMSZ_TOP: truelight@0: r->top = r->bottom - h; truelight@0: break; truelight@0: case WMSZ_TOPLEFT: truelight@0: r->top = r->bottom - h; truelight@0: r->left = r->right - w; truelight@0: break; truelight@0: case WMSZ_TOPRIGHT: truelight@0: r->top = r->bottom - h; truelight@0: r->right = r->left + w; truelight@0: break; truelight@0: } truelight@0: return TRUE; truelight@0: } truelight@0: truelight@0: // needed for wheel truelight@0: #if !defined(WM_MOUSEWHEEL) truelight@0: # define WM_MOUSEWHEEL 0x020A truelight@0: #endif //WM_MOUSEWHEEL truelight@0: #if !defined(GET_WHEEL_DELTA_WPARAM) truelight@0: # define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam)) truelight@0: #endif //GET_WHEEL_DELTA_WPARAM truelight@0: truelight@0: case WM_MOUSEWHEEL: { truelight@193: int delta = GET_WHEEL_DELTA_WPARAM(wParam); tron@1468: truelight@0: if (delta < 0) { truelight@0: _cursor.wheel++; truelight@0: } else if (delta > 0) { truelight@0: _cursor.wheel--; truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: truelight@0: case WM_ACTIVATEAPP: truelight@0: _wnd.has_focus = (bool)wParam; truelight@0: break; truelight@0: } truelight@0: return DefWindowProc(hwnd, msg, wParam, lParam); truelight@0: } truelight@0: darkvater@1102: static void RegisterWndClass(void) truelight@0: { truelight@0: static bool registered; truelight@0: if (!registered) { truelight@0: HINSTANCE hinst = GetModuleHandle(NULL); truelight@0: WNDCLASS wnd = { truelight@0: 0, truelight@0: WndProcGdi, truelight@0: 0, truelight@0: 0, truelight@0: hinst, truelight@0: LoadIcon(hinst, MAKEINTRESOURCE(100)), truelight@0: LoadCursor(NULL, IDC_ARROW), truelight@0: 0, truelight@0: 0, truelight@0: "TTD" truelight@0: }; truelight@0: registered = true; truelight@0: if (!RegisterClass(&wnd)) truelight@0: error("RegisterClass failed"); truelight@0: } truelight@0: } truelight@0: darkvater@659: extern const char _openttd_revision[]; darkvater@659: truelight@0: static void MakeWindow(bool full_screen) truelight@0: { truelight@0: _fullscreen = full_screen; truelight@0: truelight@0: _wnd.double_size = _double_size && !full_screen; truelight@193: truelight@0: // recreate window? tron@1468: if ((full_screen || _wnd.fullscreen) && _wnd.main_wnd) { truelight@0: DestroyWindow(_wnd.main_wnd); truelight@0: _wnd.main_wnd = 0; truelight@0: } truelight@193: truelight@0: if (full_screen) { truelight@0: DEVMODE settings; truelight@0: memset(&settings, 0, sizeof(DEVMODE)); truelight@0: settings.dmSize = sizeof(DEVMODE); truelight@0: settings.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT; truelight@0: truelight@0: if (_fullscreen_bpp) { truelight@0: settings.dmBitsPerPel = _fullscreen_bpp; truelight@0: settings.dmFields |= DM_BITSPERPEL; truelight@0: } truelight@0: settings.dmPelsWidth = _wnd.width_org; truelight@0: settings.dmPelsHeight = _wnd.height_org; tron@1468: settings.dmDisplayFrequency = _display_hz; tron@1468: if (settings.dmDisplayFrequency != 0) truelight@0: settings.dmFields |= DM_DISPLAYFREQUENCY; tron@1468: if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) { truelight@0: MakeWindow(false); truelight@0: return; truelight@0: } truelight@0: } else if (_wnd.fullscreen) { truelight@0: // restore display? truelight@0: ChangeDisplaySettings(NULL, 0); truelight@0: } truelight@0: truelight@0: { truelight@0: RECT r; truelight@0: uint style; truelight@0: int x, y, w, h; truelight@193: tron@1468: _wnd.fullscreen = full_screen; tron@1468: if (_wnd.fullscreen) { truelight@0: style = WS_POPUP | WS_VISIBLE; truelight@0: SetRect(&r, 0, 0, _wnd.width_org, _wnd.height_org); truelight@0: } else { truelight@0: style = WS_OVERLAPPEDWINDOW | WS_VISIBLE; truelight@0: SetRect(&r, 0, 0, _wnd.width, _wnd.height); truelight@0: } truelight@0: truelight@0: AdjustWindowRect(&r, style, FALSE); truelight@0: w = r.right - r.left; truelight@0: h = r.bottom - r.top; tron@1468: x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2; tron@1468: y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2; truelight@0: truelight@0: if (_wnd.main_wnd) { truelight@0: SetWindowPos(_wnd.main_wnd, 0, x, y, w, h, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER); truelight@0: } else { darkvater@287: char Windowtitle[50] = "OpenTTD "; tron@1468: tron@1468: snprintf(Windowtitle, lengthof(Windowtitle), "OpenTTD %s", tron@1468: _openttd_revision); darkvater@659: darkvater@287: _wnd.main_wnd = CreateWindow("TTD", Windowtitle, style, x, y, w, h, 0, 0, _inst, 0); truelight@0: if (_wnd.main_wnd == NULL) truelight@0: error("CreateWindow failed"); truelight@0: } truelight@0: } darkvater@298: GameSizeChanged(); // invalidate all windows, force redraw truelight@0: } truelight@0: truelight@0: static bool AllocateDibSection(int w, int h) truelight@0: { truelight@0: BITMAPINFO *bi; truelight@0: HDC dc; truelight@0: darkvater@306: w = clamp(w, 64, MAX_SCREEN_WIDTH); darkvater@306: h = clamp(h, 64, MAX_SCREEN_HEIGHT); truelight@0: truelight@0: if (w == _screen.width && h == _screen.height) truelight@0: return false; truelight@0: darkvater@306: _screen.width = w; darkvater@306: _screen.pitch = (w + 3) & ~0x3; truelight@0: _screen.height = h; truelight@0: truelight@0: if (_wnd.alloced_bits) { truelight@0: free(_wnd.alloced_bits); truelight@0: _wnd.alloced_bits = NULL; truelight@0: } truelight@193: truelight@0: bi = alloca(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256); truelight@0: memset(bi, 0, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD)*256); truelight@0: bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); truelight@193: darkvater@306: if (_wnd.double_size) { darkvater@306: w = (w + 3) & ~0x3; tron@1468: _wnd.alloced_bits = _wnd.buffer_bits = malloc(w * h); darkvater@306: w *= 2; darkvater@306: h *= 2; darkvater@306: } truelight@0: darkvater@306: bi->bmiHeader.biWidth = _wnd.width = w; darkvater@306: bi->bmiHeader.biHeight = -(_wnd.height = h); truelight@193: truelight@0: bi->bmiHeader.biPlanes = 1; truelight@0: bi->bmiHeader.biBitCount = 8; truelight@0: bi->bmiHeader.biCompression = BI_RGB; truelight@0: truelight@0: if (_wnd.dib_sect) truelight@0: DeleteObject(_wnd.dib_sect); truelight@0: truelight@0: dc = GetDC(0); truelight@0: _wnd.dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, &_wnd.bitmap_bits, NULL, 0); truelight@0: if (_wnd.dib_sect == NULL) truelight@0: error("CreateDIBSection failed"); truelight@0: ReleaseDC(0, dc); truelight@0: truelight@193: if (!_wnd.double_size) truelight@0: _wnd.buffer_bits = _wnd.bitmap_bits; truelight@0: truelight@0: return true; truelight@0: } truelight@0: truelight@0: static const uint16 default_resolutions[][2] = { darkvater@1028: { 640, 480}, darkvater@1028: { 800, 600}, darkvater@1028: {1024, 768}, darkvater@1028: {1152, 864}, darkvater@1028: {1280, 800}, darkvater@1028: {1280, 960}, darkvater@1028: {1280, 1024}, darkvater@1028: {1400, 1050}, darkvater@1028: {1600, 1200}, darkvater@1028: {1680, 1050}, darkvater@1028: {1920, 1200} truelight@0: }; truelight@0: darkvater@1102: static void FindResolutions(void) truelight@0: { truelight@0: int i = 0, n = 0; truelight@0: DEVMODE dm; truelight@0: Darkvater@1806: while (EnumDisplaySettings(NULL, i++, &dm) != 0) { Darkvater@1806: if (dm.dmBitsPerPel == 8 && IS_INT_INSIDE(dm.dmPelsWidth, 640, MAX_SCREEN_WIDTH + 1) && Darkvater@1806: IS_INT_INSIDE(dm.dmPelsHeight, 480, MAX_SCREEN_HEIGHT + 1)){ Darkvater@1806: int j; Darkvater@1806: for (j = 0; j < n; j++) { Darkvater@1806: if (_resolutions[j][0] == dm.dmPelsWidth && _resolutions[j][1] == dm.dmPelsHeight) break; Darkvater@1806: } Darkvater@1806: Darkvater@1806: /* In the previous loop we have checked already existing/added resolutions if Darkvater@1806: * they are the same as the new ones. If this is not the case (j == n); we have Darkvater@1806: * looped all and found none, add the new one to the list. If we have reached the Darkvater@1806: * maximum amount of resolutions, then quit querying the display */ Darkvater@1806: if (j == n) { Darkvater@1806: _resolutions[j][0] = dm.dmPelsWidth; Darkvater@1806: _resolutions[j][1] = dm.dmPelsHeight; Darkvater@1806: if (++n == lengthof(_resolutions)) break; Darkvater@1806: } truelight@0: } truelight@0: } truelight@0: Darkvater@1806: /* We have found no resolutions, show the default list */ truelight@0: if (n == 0) { truelight@0: memcpy(_resolutions, default_resolutions, sizeof(default_resolutions)); tron@1468: n = lengthof(default_resolutions); truelight@0: } truelight@0: truelight@0: _num_resolutions = n; Darkvater@1806: SortResolutions(_num_resolutions); truelight@0: } truelight@0: truelight@0: tron@1301: static const char *Win32GdiStart(const char * const *parm) truelight@0: { truelight@0: memset(&_wnd, 0, sizeof(_wnd)); truelight@0: _wnd.cursor_visible = true; truelight@0: truelight@0: RegisterWndClass(); truelight@0: truelight@0: MakePalette(); truelight@0: truelight@0: FindResolutions(); truelight@0: truelight@0: // fullscreen uses those truelight@0: _wnd.width_org = _cur_resolution[0]; truelight@0: _wnd.height_org = _cur_resolution[1]; truelight@0: truelight@0: AllocateDibSection(_cur_resolution[0], _cur_resolution[1]); truelight@0: MarkWholeScreenDirty(); truelight@0: truelight@0: MakeWindow(_fullscreen); truelight@0: truelight@0: return NULL; truelight@0: } truelight@0: darkvater@1102: static void Win32GdiStop(void) truelight@0: { tron@1468: if (_wnd.fullscreen) ChangeDisplaySettings(NULL, 0); truelight@0: MyShowCursor(true); truelight@0: DeleteObject(_wnd.gdi_palette); truelight@0: DeleteObject(_wnd.dib_sect); truelight@0: DestroyWindow(_wnd.main_wnd); truelight@0: } truelight@0: truelight@0: // simple upscaler by 2 truelight@0: static void filter(int left, int top, int width, int height) truelight@0: { truelight@0: uint p = _screen.pitch; truelight@0: byte *s = (byte*)_wnd.buffer_bits + top * p + left; truelight@0: byte *d = (byte*)_wnd.bitmap_bits + top * p * 4 + left * 2; truelight@0: tron@1468: for (; height > 0; height--) { tron@1468: int i; tron@1468: tron@1468: for (i = 0; i != width; i++) { tron@1468: d[i * 2] = d[i * 2 + 1] = d[i * 2 + p * 2] = d[i * 2 + 1 + p * 2] = s[i]; truelight@0: } truelight@0: s += p; truelight@0: d += p * 4; truelight@0: } truelight@0: } truelight@0: truelight@0: static void Win32GdiMakeDirty(int left, int top, int width, int height) truelight@0: { tron@1468: RECT r = { left, top, left + width, top + height }; tron@1468: truelight@0: if (_wnd.double_size) { truelight@0: filter(left, top, width, height); tron@1468: r.left *= 2; tron@1468: r.top *= 2; tron@1468: r.right *= 2; tron@1468: r.bottom *= 2; truelight@0: } truelight@0: InvalidateRect(_wnd.main_wnd, &r, FALSE); truelight@0: } truelight@0: darkvater@1102: static void CheckPaletteAnim(void) truelight@0: { truelight@0: if (_pal_last_dirty == -1) truelight@0: return; truelight@0: InvalidateRect(_wnd.main_wnd, NULL, FALSE); truelight@0: } truelight@0: darkvater@1102: static int Win32GdiMainLoop(void) truelight@0: { truelight@0: MSG mesg; truelight@0: uint32 next_tick = GetTickCount() + 30, cur_ticks; truelight@0: truelight@0: _wnd.running = true; truelight@0: truelight@0: while(true) { truelight@0: while (PeekMessage(&mesg, NULL, 0, 0, PM_REMOVE)) { truelight@0: InteractiveRandom(); // randomness truelight@0: TranslateMessage(&mesg); truelight@0: DispatchMessage(&mesg); truelight@0: } truelight@0: if (_exit_game) return ML_QUIT; truelight@0: if (_wnd.switch_driver) return ML_SWITCHDRIVER; truelight@0: truelight@0: #if defined(_DEBUG) truelight@0: if (_wnd.has_focus && GetAsyncKeyState(VK_SHIFT) < 0) { darkvater@1548: if ( truelight@0: #else truelight@0: if (_wnd.has_focus && GetAsyncKeyState(VK_TAB) < 0) { darkvater@1403: /* Disable speeding up game with ALT+TAB (if syskey is pressed, the darkvater@1403: * real key is in the upper 16 bits (see WM_SYSKEYDOWN in WndProcGdi()) */ darkvater@1548: if ((_pressed_key >> 16) & WKC_TAB && darkvater@1548: #endif pasky@1582: !_networking && _game_mode != GM_MENU) tron@1468: _fast_forward |= 2; darkvater@1403: } else if (_fast_forward & 2) truelight@0: _fast_forward = 0; truelight@193: darkvater@1403: cur_ticks = GetTickCount(); truelight@0: if ((_fast_forward && !_pause) || cur_ticks > next_tick) truelight@0: next_tick = cur_ticks; truelight@0: truelight@0: if (cur_ticks == next_tick) { truelight@0: next_tick += 30; truelight@0: _ctrl_pressed = _wnd.has_focus && GetAsyncKeyState(VK_CONTROL)<0; truelight@0: _shift_pressed = _wnd.has_focus && GetAsyncKeyState(VK_SHIFT)<0; truelight@0: _dbg_screen_rect = _wnd.has_focus && GetAsyncKeyState(VK_CAPITAL)<0; truelight@193: truelight@0: // determine which directional keys are down darkvater@1230: if (_wnd.has_focus) { darkvater@1230: _dirkeys = darkvater@1230: (GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) + darkvater@1230: (GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) + darkvater@1230: (GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) + darkvater@1230: (GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0); darkvater@1230: } else darkvater@1230: _dirkeys = 0; truelight@0: truelight@193: GameLoop(); truelight@0: _cursor.delta.x = _cursor.delta.y = 0; truelight@0: truelight@0: if (_force_full_redraw) truelight@0: MarkWholeScreenDirty(); truelight@0: truelight@0: GdiFlush(); truelight@0: _screen.dst_ptr = _wnd.buffer_bits; truelight@0: UpdateWindows(); truelight@0: CheckPaletteAnim(); truelight@0: } else { truelight@0: Sleep(1); truelight@0: GdiFlush(); truelight@0: _screen.dst_ptr = _wnd.buffer_bits; truelight@543: DrawTextMessage(); truelight@0: DrawMouseCursor(); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static bool Win32GdiChangeRes(int w, int h) truelight@0: { truelight@0: _wnd.width = _wnd.width_org = w; truelight@0: _wnd.height = _wnd.height_org = h; truelight@193: darkvater@298: MakeWindow(_fullscreen); // _wnd.fullscreen screws up ingame resolution switching truelight@193: truelight@0: return true; truelight@0: } truelight@0: Darkvater@1829: static void Win32GdiFullScreen(bool full_screen) {MakeWindow(full_screen);} Darkvater@1806: truelight@0: const HalVideoDriver _win32_video_driver = { truelight@0: Win32GdiStart, truelight@0: Win32GdiStop, truelight@0: Win32GdiMakeDirty, truelight@0: Win32GdiMainLoop, truelight@0: Win32GdiChangeRes, Darkvater@1829: Win32GdiFullScreen, truelight@0: }; truelight@0: truelight@0: truelight@0: /********************** truelight@0: * WIN32 MIDI PLAYER truelight@0: **********************/ truelight@0: tron@1468: static struct { truelight@0: bool stop_song; truelight@0: bool terminate; truelight@0: bool playing; truelight@0: int new_vol; truelight@0: HANDLE wait_obj; truelight@0: char start_song[260]; truelight@0: } _midi; truelight@0: truelight@0: static void Win32MidiPlaySong(const char *filename) truelight@0: { truelight@0: strcpy(_midi.start_song, filename); truelight@0: _midi.playing = true; truelight@0: _midi.stop_song = false; truelight@0: SetEvent(_midi.wait_obj); truelight@0: } truelight@0: darkvater@1102: static void Win32MidiStopSong(void) truelight@0: { truelight@0: if (_midi.playing) { truelight@0: _midi.stop_song = true; tron@1468: _midi.start_song[0] = '\0'; truelight@0: SetEvent(_midi.wait_obj); truelight@0: } truelight@0: } truelight@0: darkvater@1102: static bool Win32MidiIsSongPlaying(void) truelight@0: { truelight@0: return _midi.playing; truelight@0: } truelight@0: truelight@0: static void Win32MidiSetVolume(byte vol) truelight@0: { truelight@0: _midi.new_vol = vol; truelight@0: SetEvent(_midi.wait_obj); truelight@0: } truelight@0: truelight@0: static long CDECL MidiSendCommand(const char *cmd, ...) { truelight@0: va_list va; truelight@0: char buf[512]; tron@1468: truelight@0: va_start(va, cmd); truelight@0: vsprintf(buf, cmd, va); truelight@0: va_end(va); truelight@0: return mciSendStringA(buf, NULL, 0, 0); truelight@0: } truelight@0: truelight@0: static bool MidiIntPlaySong(const char *filename) truelight@0: { truelight@0: MidiSendCommand("close all"); truelight@0: if (MidiSendCommand("open %s type sequencer alias song", filename) != 0) truelight@0: return false; truelight@0: truelight@0: if (MidiSendCommand("play song from 0") != 0) truelight@0: return false; truelight@0: return true; truelight@0: } truelight@0: darkvater@1102: static void MidiIntStopSong(void) truelight@0: { truelight@0: MidiSendCommand("close all"); truelight@0: } truelight@0: truelight@0: static void MidiIntSetVolume(int vol) truelight@0: { truelight@0: uint v = (vol * 65535 / 127); tron@1468: midiOutSetVolume((HMIDIOUT)-1, v + (v << 16)); truelight@0: } truelight@0: darkvater@1102: static bool MidiIntIsSongPlaying(void) truelight@0: { truelight@0: char buf[16]; truelight@0: mciSendStringA("status song mode", buf, sizeof(buf), 0); truelight@0: return strcmp(buf, "playing") == 0 || strcmp(buf, "seeking") == 0; truelight@0: } truelight@0: truelight@0: static DWORD WINAPI MidiThread(LPVOID arg) truelight@0: { truelight@0: _midi.wait_obj = CreateEvent(NULL, FALSE, FALSE, NULL); truelight@0: truelight@0: do { tron@1468: char *s; tron@1468: int vol; tron@1468: tron@1468: vol = _midi.new_vol; tron@1468: if (vol != -1) { truelight@0: _midi.new_vol = -1; truelight@0: MidiIntSetVolume(vol); truelight@0: } tron@1468: tron@1468: s = _midi.start_song; tron@1468: if (s[0] != '\0') { truelight@0: _midi.playing = MidiIntPlaySong(s); tron@1468: s[0] = '\0'; truelight@193: truelight@0: // Delay somewhat in case we don't manage to play. truelight@0: if (!_midi.playing) { truelight@193: Sleep(5000); truelight@0: } truelight@0: } tron@1468: tron@1468: if (_midi.stop_song && _midi.playing) { truelight@0: _midi.stop_song = false; truelight@0: _midi.playing = false; truelight@0: MidiIntStopSong(); truelight@0: } truelight@0: truelight@0: if (_midi.playing && !MidiIntIsSongPlaying()) truelight@0: _midi.playing = false; truelight@0: truelight@0: WaitForMultipleObjects(1, &_midi.wait_obj, FALSE, 1000); truelight@0: } while (!_midi.terminate); truelight@0: truelight@0: DeleteObject(_midi.wait_obj); truelight@0: return 0; truelight@0: } truelight@0: tron@1301: static const char *Win32MidiStart(const char * const *parm) truelight@0: { truelight@0: DWORD threadId; tron@1468: truelight@0: memset(&_midi, 0, sizeof(_midi)); truelight@0: _midi.new_vol = -1; truelight@0: CreateThread(NULL, 8192, MidiThread, 0, 0, &threadId); truelight@0: return 0; truelight@0: } truelight@0: darkvater@1102: static void Win32MidiStop(void) truelight@0: { truelight@0: _midi.terminate = true; truelight@0: SetEvent(_midi.wait_obj); truelight@0: } truelight@0: truelight@0: const HalMusicDriver _win32_music_driver = { truelight@0: Win32MidiStart, truelight@0: Win32MidiStop, truelight@0: Win32MidiPlaySong, truelight@0: Win32MidiStopSong, truelight@0: Win32MidiIsSongPlaying, truelight@0: Win32MidiSetVolume, truelight@0: }; truelight@0: truelight@0: // WIN32 Sound code. truelight@0: truelight@0: static HWAVEOUT _waveout; truelight@0: static WAVEHDR _wave_hdr[2]; truelight@0: static int _bufsize; truelight@0: static void PrepareHeader(WAVEHDR *hdr) truelight@0: { tron@1468: hdr->dwBufferLength = _bufsize * 4; truelight@0: hdr->dwFlags = 0; tron@1468: hdr->lpData = malloc(_bufsize * 4); tron@1468: if (hdr->lpData == NULL || tron@1468: waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) truelight@0: error("waveOutPrepareHeader failed"); truelight@0: } truelight@0: darkvater@1102: static void FillHeaders(void) truelight@0: { truelight@0: WAVEHDR *hdr; tron@1468: tron@1468: for (hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) { truelight@0: if (!(hdr->dwFlags & WHDR_INQUEUE)) { tron@1468: MxMixSamples(_mixer, hdr->lpData, hdr->dwBufferLength / 4); truelight@0: if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) truelight@0: error("waveOutWrite failed"); truelight@0: } truelight@0: } truelight@0: } truelight@0: tron@1468: static void CALLBACK waveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, tron@1468: DWORD dwParam1, DWORD dwParam2) truelight@0: { tron@1468: switch (uMsg) { tron@1468: case WOM_DONE: tron@1468: if (_waveout) FillHeaders(); tron@1468: break; tron@1468: tron@1468: default: tron@1468: break; truelight@0: } truelight@0: } truelight@0: tron@1301: static const char *Win32SoundStart(const char * const *parm) truelight@0: { truelight@0: WAVEFORMATEX wfex; truelight@0: int hz; truelight@193: truelight@0: _bufsize = GetDriverParamInt(parm, "bufsize", 1024); truelight@0: hz = GetDriverParamInt(parm, "hz", 11025); truelight@0: wfex.wFormatTag = WAVE_FORMAT_PCM; truelight@0: wfex.nChannels = 2; truelight@0: wfex.nSamplesPerSec = hz; truelight@0: wfex.nAvgBytesPerSec = hz*2*2; truelight@0: wfex.nBlockAlign = 4; truelight@0: wfex.wBitsPerSample = 16; truelight@0: if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD)&waveOutProc, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR) truelight@0: return "waveOutOpen failed"; truelight@0: PrepareHeader(&_wave_hdr[0]); truelight@0: PrepareHeader(&_wave_hdr[1]); truelight@0: FillHeaders(); truelight@0: return NULL; truelight@0: } truelight@0: darkvater@1102: static void Win32SoundStop(void) truelight@0: { truelight@0: HWAVEOUT waveout = _waveout; tron@1468: truelight@0: _waveout = NULL; truelight@0: waveOutReset(waveout); truelight@0: waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR)); truelight@0: waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR)); truelight@0: waveOutClose(waveout); truelight@0: } truelight@0: truelight@0: const HalSoundDriver _win32_sound_driver = { truelight@0: Win32SoundStart, truelight@0: Win32SoundStop, truelight@0: }; truelight@0: truelight@0: // Helper function needed by dynamically loading SDL truelight@0: bool LoadLibraryList(void **proc, const char *dll) truelight@0: { truelight@0: HMODULE lib; truelight@0: void *p; truelight@0: tron@1468: while (*dll != '\0') { truelight@0: lib = LoadLibrary(dll); truelight@0: if (lib == NULL) truelight@0: return false; truelight@0: while (true) { tron@1468: while(*dll++ != '\0'); tron@1468: if (*dll == '\0') truelight@0: break; truelight@0: p = GetProcAddress(lib, dll); truelight@0: if (p == NULL) truelight@0: return false; truelight@0: *proc++ = p; truelight@0: } truelight@0: dll++; truelight@0: } truelight@0: return true; truelight@0: } truelight@0: darkvater@796: #ifdef _MSC_VER darkvater@796: truelight@0: static const char *_exception_string; truelight@0: static void *_safe_esp; truelight@0: static char *_crash_msg; truelight@0: static bool _expanded; truelight@0: static bool _did_emerg_save; truelight@0: static int _ident; truelight@0: truelight@0: void ShowOSErrorBox(const char *buf) truelight@0: { truelight@0: MyShowCursor(true); truelight@0: MessageBoxA(GetActiveWindow(), buf, "Error!", MB_ICONSTOP); truelight@0: truelight@0: // if exception tracker is enabled, we crash here to let the exception handler handle it. truelight@0: #if defined(WIN32_EXCEPTION_TRACKER) && !defined(_DEBUG) truelight@0: if (*buf == '!') { truelight@0: _exception_string = buf; truelight@0: *(byte*)0 = 0; truelight@0: } truelight@0: #endif truelight@0: } truelight@0: truelight@0: typedef struct DebugFileInfo { truelight@0: uint32 size; truelight@0: uint32 crc32; truelight@0: SYSTEMTIME file_time; truelight@0: } DebugFileInfo; truelight@0: truelight@0: static uint32 *_crc_table; truelight@0: truelight@0: static void MakeCRCTable(uint32 *table) { truelight@0: uint32 crc, poly = 0xEDB88320L; truelight@0: int i, j; truelight@193: truelight@0: _crc_table = table; truelight@0: tron@1468: for (i = 0; i != 256; i++) { truelight@0: crc = i; tron@1468: for (j = 8; j != 0; j--) { truelight@0: if (crc & 1) truelight@0: crc = (crc >> 1) ^ poly; truelight@0: else tron@1468: crc >>= 1; truelight@0: } truelight@0: table[i] = crc; truelight@0: } truelight@0: } truelight@0: truelight@0: static uint32 CalcCRC(byte *data, uint size, uint32 crc) { tron@1468: for (; size > 0; size--) { tron@1468: crc = ((crc >> 8) & 0x00FFFFFF) ^ _crc_table[(crc ^ *data++) & 0xFF]; tron@1468: } truelight@0: return crc; truelight@0: } truelight@0: truelight@0: static void GetFileInfo(DebugFileInfo *dfi, const char *filename) truelight@0: { truelight@0: memset(dfi, 0, sizeof(dfi)); truelight@0: truelight@0: { truelight@0: HANDLE file; truelight@0: byte buffer[1024]; truelight@0: DWORD numread; truelight@0: uint32 filesize = 0; truelight@0: FILETIME write_time; truelight@0: uint32 crc = (uint32)-1; truelight@0: tron@1468: file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, tron@1468: OPEN_EXISTING, 0, 0); truelight@0: if (file != INVALID_HANDLE_VALUE) { truelight@0: while(true) { tron@1468: if (ReadFile(file, buffer, sizeof(buffer), &numread, NULL) == 0 || tron@1468: numread == 0) truelight@0: break; truelight@0: filesize += numread; truelight@0: crc = CalcCRC(buffer, numread, crc); truelight@0: } truelight@0: dfi->size = filesize; truelight@0: dfi->crc32 = crc ^ (uint32)-1; truelight@193: truelight@0: if (GetFileTime(file, NULL, NULL, &write_time)) { truelight@0: FileTimeToSystemTime(&write_time, &dfi->file_time); truelight@0: } truelight@0: CloseHandle(file); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: truelight@0: static char *PrintModuleInfo(char *output, HMODULE mod) truelight@0: { darkvater@318: char buffer[MAX_PATH]; truelight@0: DebugFileInfo dfi; tron@1468: truelight@0: GetModuleFileName(mod, buffer, MAX_PATH); truelight@0: GetFileInfo(&dfi, buffer); truelight@0: output += sprintf(output, " %-20s handle: %.8X size: %d crc: %.8X date: %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n", truelight@0: buffer, truelight@0: mod, truelight@0: dfi.size, truelight@0: dfi.crc32, truelight@0: dfi.file_time.wYear, truelight@0: dfi.file_time.wMonth, truelight@0: dfi.file_time.wDay, truelight@0: dfi.file_time.wHour, truelight@0: dfi.file_time.wMinute, truelight@0: dfi.file_time.wSecond truelight@0: ); truelight@0: return output; truelight@0: } truelight@0: truelight@0: static char *PrintModuleList(char *output) truelight@0: { truelight@0: BOOL (WINAPI *EnumProcessModules)(HANDLE,HMODULE*,DWORD,LPDWORD); truelight@0: HANDLE proc; truelight@0: HMODULE modules[100]; truelight@0: DWORD needed; truelight@0: BOOL res; truelight@0: int count,i; truelight@0: truelight@0: if (LoadLibraryList((void*)&EnumProcessModules, "psapi.dll\0EnumProcessModules\0")) { truelight@0: proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId()); truelight@0: if (proc) { truelight@0: res = EnumProcessModules(proc, modules, sizeof(modules), &needed); truelight@0: CloseHandle(proc); truelight@0: if (res) { tron@1468: count = tron@1468: min(needed / sizeof(HMODULE), lengthof(modules)); tron@1468: for (i = 0; i != count; i++) truelight@0: output = PrintModuleInfo(output, modules[i]); truelight@0: return output; truelight@0: } truelight@0: } truelight@0: } truelight@0: output = PrintModuleInfo(output, NULL); truelight@0: return output; truelight@0: } truelight@0: truelight@193: static const char _crash_desc[] = truelight@0: "A serious fault condition occured in the game. The game will shut down.\n" truelight@0: "Press \"Submit report\" to send crash information to the developers. " tron@1468: "This will greatly help debugging. " tron@1468: "The information contained in the report is displayed below.\n" truelight@0: "Press \"Emergency save\" to attempt saving the game."; truelight@0: truelight@193: static const char _save_succeeded[] = tron@1468: "Emergency save succeeded.\n" tron@1468: "Be aware that critical parts of the internal game state may have become " tron@1468: "corrupted. The saved game is not guaranteed to work."; truelight@0: Darkvater@1881: static bool EmergencySave(void) Darkvater@1881: { Darkvater@1881: SaveOrLoad("crash.sav", SL_SAVE); Darkvater@1881: return true; Darkvater@1881: } truelight@0: truelight@0: typedef struct { truelight@0: HINTERNET (WINAPI *InternetOpenA)(LPCSTR,DWORD, LPCSTR, LPCSTR, DWORD); truelight@0: HINTERNET (WINAPI *InternetConnectA)(HINTERNET, LPCSTR, INTERNET_PORT, LPCSTR, LPCSTR, DWORD, DWORD, DWORD); truelight@0: HINTERNET (WINAPI *HttpOpenRequestA)(HINTERNET, LPCSTR, LPCSTR, LPCSTR, LPCSTR, LPCSTR *, DWORD, DWORD); truelight@0: BOOL (WINAPI *HttpSendRequestA)(HINTERNET, LPCSTR, DWORD, LPVOID, DWORD); truelight@0: BOOL (WINAPI *InternetCloseHandle)(HINTERNET); truelight@0: BOOL (WINAPI *HttpQueryInfo)(HINTERNET, DWORD, LPVOID, LPDWORD, LPDWORD); truelight@0: } WinInetProcs; truelight@0: truelight@0: #define M(x) x "\0" truelight@193: static const char wininet_files[] = truelight@0: M("wininet.dll") truelight@0: M("InternetOpenA") truelight@0: M("InternetConnectA") truelight@0: M("HttpOpenRequestA") truelight@0: M("HttpSendRequestA") truelight@0: M("InternetCloseHandle") truelight@0: M("HttpQueryInfoA") truelight@0: M(""); truelight@0: #undef M truelight@0: truelight@0: static WinInetProcs _wininet; truelight@0: truelight@0: truelight@0: static char *SubmitCrashReport(HWND wnd, void *msg, size_t msglen, const char *arg) truelight@0: { truelight@0: HINTERNET inet, conn, http; truelight@0: char *err = NULL; truelight@0: DWORD code, len; truelight@0: static char buf[100]; truelight@0: char buff[100]; truelight@193: truelight@0: if (_wininet.InternetOpen == NULL && !LoadLibraryList((void**)&_wininet, wininet_files)) return "can't load wininet.dll"; truelight@0: truelight@0: inet = _wininet.InternetOpen("TTD", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ); truelight@0: if (inet == NULL) { err = "internetopen failed"; goto error1; } truelight@0: truelight@0: conn = _wininet.InternetConnect(inet, "openttd.com", INTERNET_DEFAULT_HTTP_PORT, "", "", INTERNET_SERVICE_HTTP, 0, 0); truelight@0: if (conn == NULL) { err = "internetconnect failed"; goto error2; } truelight@0: truelight@0: sprintf(buff, "/crash.php?file=%s&ident=%d", arg, _ident); truelight@0: truelight@0: http = _wininet.HttpOpenRequest(conn, "POST", buff, NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE , 0); truelight@0: if (http == NULL) { err = "httpopenrequest failed"; goto error3; } truelight@0: truelight@0: if (!_wininet.HttpSendRequest(http, "Content-type: application/binary", -1, msg, msglen)) { err = "httpsendrequest failed"; goto error4; } truelight@0: truelight@0: len = sizeof(code); truelight@0: if (!_wininet.HttpQueryInfo(http, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &code, &len, 0)) { err = "httpqueryinfo failed"; goto error4; } truelight@193: truelight@0: if (code != 200) { truelight@0: int l = sprintf(buf, "Server said: %d ", code); truelight@0: len = sizeof(buf) - l; truelight@0: _wininet.HttpQueryInfo(http, HTTP_QUERY_STATUS_TEXT, buf + l, &len, 0); truelight@0: err = buf; truelight@0: } truelight@0: truelight@0: error4: truelight@0: _wininet.InternetCloseHandle(http); truelight@0: error3: truelight@0: _wininet.InternetCloseHandle(conn); truelight@0: error2: truelight@0: _wininet.InternetCloseHandle(inet); truelight@0: error1: truelight@0: return err; truelight@0: } truelight@0: truelight@0: static void SubmitFile(HWND wnd, const char *file) truelight@0: { truelight@0: HANDLE h; truelight@0: unsigned long size; truelight@0: unsigned long read; truelight@0: void *mem; truelight@0: truelight@0: h = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); truelight@0: if (h == NULL) return; truelight@0: truelight@0: size = GetFileSize(h, NULL); truelight@0: if (size > 500000) goto error1; truelight@193: truelight@0: mem = malloc(size); truelight@0: if (mem == NULL) goto error1; truelight@0: truelight@0: if (!ReadFile(h, mem, size, &read, NULL) || read != size) goto error2; truelight@0: truelight@193: SubmitCrashReport(wnd, mem, size, file); truelight@0: truelight@0: error2: truelight@0: free(mem); truelight@0: error1: truelight@0: CloseHandle(h); truelight@0: } truelight@0: truelight@0: static const char * const _expand_texts[] = {"S&how report >>", "&Hide report <<" }; truelight@0: truelight@0: static void SetWndSize(HWND wnd, int mode) truelight@0: { truelight@0: RECT r,r2; truelight@0: int offs; truelight@0: truelight@0: GetWindowRect(wnd, &r); truelight@193: truelight@0: SetDlgItemText(wnd, 15, _expand_texts[mode == 1]); truelight@193: truelight@0: if (mode >= 0) { truelight@0: GetWindowRect(GetDlgItem(wnd, 11), &r2); truelight@0: offs = r2.bottom - r2.top + 10; tron@1468: if (!mode) offs = -offs; tron@1468: SetWindowPos(wnd, HWND_TOPMOST, 0, 0, tron@1468: r.right - r.left, r.bottom - r.top + offs, SWP_NOMOVE | SWP_NOZORDER); truelight@0: } else { truelight@193: SetWindowPos(wnd, HWND_TOPMOST, tron@1468: (GetSystemMetrics(SM_CXSCREEN) - (r.right - r.left)) / 2, tron@1468: (GetSystemMetrics(SM_CYSCREEN) - (r.bottom - r.top)) / 2, truelight@0: 0, 0, SWP_NOSIZE); truelight@0: } truelight@0: } truelight@0: truelight@0: static bool DoEmergencySave(HWND wnd) truelight@0: { truelight@0: bool b = false; truelight@0: truelight@0: EnableWindow(GetDlgItem(wnd, 13), FALSE); truelight@0: _did_emerg_save = true; truelight@0: __try { truelight@0: b = EmergencySave(); truelight@0: } __except (1) {} truelight@0: return b; truelight@0: } truelight@0: truelight@0: static BOOL CALLBACK CrashDialogFunc(HWND wnd,UINT msg,WPARAM wParam,LPARAM lParam) truelight@0: { truelight@0: switch(msg) { truelight@0: case WM_INITDIALOG: truelight@0: SetDlgItemText(wnd, 10, _crash_desc); truelight@0: SetDlgItemText(wnd, 11, _crash_msg); truelight@0: SendDlgItemMessage(wnd, 11, WM_SETFONT, (WPARAM)GetStockObject(ANSI_FIXED_FONT), FALSE); truelight@0: SetWndSize(wnd, -1); truelight@0: return TRUE; truelight@0: case WM_COMMAND: truelight@0: switch(wParam) { truelight@0: case 12: // Close truelight@0: ExitProcess(0); truelight@0: case 13: { // Emergency save truelight@0: if (DoEmergencySave(wnd)) truelight@0: MessageBoxA(wnd, _save_succeeded, "Save successful", MB_ICONINFORMATION); truelight@0: else truelight@0: MessageBoxA(wnd, "Save failed", "Save failed", MB_ICONINFORMATION); truelight@0: break; truelight@0: } truelight@0: case 14: { // Submit crash report truelight@0: char *s; truelight@193: truelight@0: SetCursor(LoadCursor(NULL, IDC_WAIT)); truelight@0: truelight@0: s = SubmitCrashReport(wnd, _crash_msg, strlen(_crash_msg), ""); truelight@0: if (s) { truelight@0: MessageBoxA(wnd, s, "Error", MB_ICONSTOP); truelight@0: break; truelight@0: } truelight@193: truelight@0: // try to submit emergency savegame truelight@0: if (_did_emerg_save || DoEmergencySave(wnd)) { truelight@0: SubmitFile(wnd, "crash.sav"); truelight@0: } truelight@0: // try to submit the autosaved game truelight@0: if (_opt.autosave) { truelight@0: char buf[40]; truelight@0: sprintf(buf, "autosave%d.sav", (_autosave_ctr - 1) & 3); truelight@0: SubmitFile(wnd, buf); truelight@0: } truelight@0: EnableWindow(GetDlgItem(wnd, 14), FALSE); truelight@0: SetCursor(LoadCursor(NULL, IDC_ARROW)); truelight@0: MessageBoxA(wnd, "Crash report submitted. Thank you.", "Crash Report", MB_ICONINFORMATION); truelight@0: break; truelight@0: } truelight@0: case 15: // Expand truelight@0: _expanded ^= 1; truelight@0: SetWndSize(wnd, _expanded); truelight@0: break; truelight@0: } truelight@0: return TRUE; truelight@0: case WM_CLOSE: truelight@0: ExitProcess(0); truelight@0: } truelight@0: truelight@0: return FALSE; truelight@0: } truelight@0: darkvater@1102: static void Handler2(void) truelight@0: { truelight@0: ShowCursor(TRUE); truelight@0: ShowWindow(GetActiveWindow(), FALSE); truelight@0: DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(100), NULL, CrashDialogFunc); truelight@0: } truelight@0: darkvater@1102: extern bool CloseConsoleLogIfActive(void); darkvater@1023: truelight@0: static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep) truelight@0: { truelight@0: char *output; truelight@0: static bool had_exception; truelight@0: tron@1468: if (had_exception) ExitProcess(0); truelight@0: had_exception = true; truelight@193: truelight@0: _ident = GetTickCount(); // something pretty unique truelight@0: truelight@0: MakeCRCTable(alloca(256 * sizeof(uint32))); truelight@0: _crash_msg = output = LocalAlloc(LMEM_FIXED, 8192); truelight@0: truelight@0: { truelight@0: SYSTEMTIME time; truelight@0: GetLocalTime(&time); truelight@193: output += sprintf(output, truelight@0: "*** OpenTTD Crash Report ***\r\n" truelight@0: "Date: %d-%.2d-%.2d %.2d:%.2d:%.2d\r\n" truelight@0: "Build: %s built on " __TIMESTAMP__ "\r\n", truelight@0: time.wYear, truelight@0: time.wMonth, truelight@0: time.wDay, truelight@0: time.wHour, truelight@0: time.wMinute, truelight@0: time.wSecond, truelight@0: "???" truelight@0: ); truelight@0: } truelight@0: tron@1468: if (_exception_string) tron@1468: output += sprintf(output, "Reason: %s\r\n", _exception_string); truelight@0: truelight@0: output += sprintf(output, "Exception %.8X at %.8X\r\n" truelight@0: "Registers:\r\n" truelight@0: " EAX: %.8X EBX: %.8X ECX: %.8X EDX: %.8X\r\n" truelight@0: " ESI: %.8X EDI: %.8X EBP: %.8X ESP: %.8X\r\n" truelight@0: " EIP: %.8X EFLAGS: %.8X\r\n" truelight@0: "\r\nBytes at CS:EIP:\r\n", truelight@0: ep->ExceptionRecord->ExceptionCode, truelight@0: ep->ExceptionRecord->ExceptionAddress, truelight@0: ep->ContextRecord->Eax, truelight@0: ep->ContextRecord->Ebx, truelight@0: ep->ContextRecord->Ecx, truelight@0: ep->ContextRecord->Edx, truelight@0: ep->ContextRecord->Esi, truelight@0: ep->ContextRecord->Edi, truelight@0: ep->ContextRecord->Ebp, truelight@0: ep->ContextRecord->Esp, truelight@0: ep->ContextRecord->Eip, truelight@0: ep->ContextRecord->EFlags truelight@0: ); truelight@0: truelight@0: { truelight@0: byte *b = (byte*)ep->ContextRecord->Eip; truelight@0: int i; tron@1468: for (i = 0; i != 24; i++) { truelight@0: if (IsBadReadPtr(b, 1)) { truelight@0: output += sprintf(output, " ??"); // OCR: WAS: , 0); truelight@0: } else { truelight@0: output += sprintf(output, " %.2X", *b); truelight@0: } truelight@0: b++; truelight@0: } truelight@0: output += sprintf(output, truelight@0: "\r\n" truelight@0: "\r\nStack trace: \r\n" truelight@0: ); truelight@0: } truelight@0: truelight@0: { truelight@0: int i,j; truelight@0: uint32 *b = (uint32*)ep->ContextRecord->Esp; tron@1468: for (j = 0; j != 24; j++) { tron@1468: for (i = 0; i != 8; i++) { truelight@0: if (IsBadReadPtr(b,sizeof(uint32))) { truelight@0: output += sprintf(output, " ????????"); //OCR: WAS - , 0); truelight@0: } else { truelight@0: output += sprintf(output, " %.8X", *b); truelight@0: } truelight@0: b++; truelight@0: } truelight@0: output += sprintf(output, "\r\n"); truelight@0: } truelight@0: } truelight@0: truelight@0: output += sprintf(output, "\r\nModule information:\r\n"); truelight@0: output = PrintModuleList(output); truelight@0: truelight@0: { truelight@0: OSVERSIONINFO os; truelight@0: os.dwOSVersionInfoSize = sizeof(os); truelight@0: GetVersionEx(&os); truelight@0: output += sprintf(output, "\r\nSystem information:\r\n" tron@1468: " Windows version %d.%d %d %s\r\n", tron@1468: os.dwMajorVersion, os.dwMinorVersion, os.dwBuildNumber, os.szCSDVersion); truelight@0: } truelight@0: truelight@0: { truelight@0: HANDLE file = CreateFile("crash.log", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0); truelight@0: DWORD num_written; truelight@0: if (file != INVALID_HANDLE_VALUE) { truelight@0: WriteFile(file, _crash_msg, output - _crash_msg, &num_written, NULL); truelight@0: CloseHandle(file); truelight@0: } truelight@0: } truelight@0: darkvater@1023: /* Close any possible log files */ darkvater@1023: CloseConsoleLogIfActive(); darkvater@1023: truelight@0: if (_safe_esp) { truelight@0: ep->ContextRecord->Eip = (DWORD)Handler2; truelight@0: ep->ContextRecord->Esp = (DWORD)_safe_esp; truelight@0: return EXCEPTION_CONTINUE_EXECUTION; truelight@0: } darkvater@1023: darkvater@1023: darkvater@1023: return EXCEPTION_EXECUTE_HANDLER; truelight@0: } truelight@0: darkvater@1102: static void Win32InitializeExceptions(void) truelight@0: { truelight@0: _asm { tron@1468: mov _safe_esp, esp truelight@0: } truelight@193: truelight@0: SetUnhandledExceptionFilter(ExceptionHandler); truelight@0: } darkvater@796: #else darkvater@796: /* Get rid of unused variable warnings.. ShowOSErrorBox tron@915: * is now used twice, once in MSVC, and once in all other Win darkvater@796: * compilers (cygwin, mingw, etc.) */ darkvater@796: void ShowOSErrorBox(const char *buf) darkvater@796: { darkvater@796: MyShowCursor(true); darkvater@796: MessageBoxA(GetActiveWindow(), buf, "Error!", MB_ICONSTOP); darkvater@796: } truelight@0: #endif truelight@0: tron@1486: #ifndef __MINGW32__ tron@1486: static inline int strcasecmp(const char* s1, const char* s2) tron@1486: { tron@1486: return stricmp(s1, s2); tron@1486: } tron@1486: #endif tron@1486: truelight@0: static char *_fios_path; truelight@0: static char *_fios_save_path; truelight@0: static char *_fios_scn_path; truelight@0: static FiosItem *_fios_items; truelight@0: static int _fios_count, _fios_alloc; truelight@0: darkvater@1102: static FiosItem *FiosAlloc(void) truelight@0: { truelight@0: if (_fios_count == _fios_alloc) { truelight@0: _fios_alloc += 256; truelight@0: _fios_items = realloc(_fios_items, _fios_alloc * sizeof(FiosItem)); truelight@0: } truelight@0: return &_fios_items[_fios_count++]; truelight@0: } truelight@0: darkvater@1596: static HANDLE MyFindFirstFile(const char *path, const char *file, WIN32_FIND_DATA *fd) truelight@0: { darkvater@1596: UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box darkvater@1596: HANDLE h; truelight@0: char paths[MAX_PATH]; truelight@193: truelight@0: sprintf(paths, "%s\\%s", path, file); darkvater@1596: h = FindFirstFile(paths, fd); darkvater@1596: darkvater@1596: SetErrorMode(sem); // restore previous setting darkvater@1596: return h; truelight@0: } truelight@0: tron@1486: int CDECL compare_FiosItems(const void *a, const void *b) tron@1486: { tron@1468: const FiosItem *da = (const FiosItem *)a; tron@1468: const FiosItem *db = (const FiosItem *)b; truelight@0: int r; truelight@0: truelight@0: if (_savegame_sort_order < 2) // sort by date tron@1486: r = da->mtime < db->mtime ? -1 : 1; truelight@193: else tron@1486: r = strcasecmp( tron@1468: da->title[0] != '\0' ? da->title : da->name, tron@1468: db->title[0] != '\0' ? db->title : db->name tron@1468: ); truelight@0: truelight@0: if (_savegame_sort_order & 1) r = -r; truelight@0: return r; truelight@0: } truelight@0: tron@1486: truelight@0: // Get a list of savegames truelight@0: FiosItem *FiosGetSavegameList(int *num, int mode) truelight@0: { truelight@0: WIN32_FIND_DATA fd; truelight@0: HANDLE h; truelight@0: FiosItem *fios; truelight@0: int sort_start; truelight@0: truelight@0: if (_fios_save_path == NULL) { truelight@0: _fios_save_path = malloc(MAX_PATH); truelight@0: strcpy(_fios_save_path, _path.save_dir); truelight@0: } truelight@0: truelight@0: if (_game_mode == GM_EDITOR) truelight@0: _fios_path = _fios_scn_path; truelight@0: else truelight@0: _fios_path = _fios_save_path; truelight@0: truelight@0: // Parent directory, only if not of the type C:\. tron@1486: if (_fios_path[3] != '\0') { truelight@0: fios = FiosAlloc(); truelight@0: fios->type = FIOS_TYPE_PARENT; tron@1486: fios->mtime = 0; tron@1572: strcpy(fios->name, ".."); truelight@0: strcpy(fios->title, ".. (Parent directory)"); truelight@0: } truelight@193: truelight@0: // Show subdirectories first truelight@0: h = MyFindFirstFile(_fios_path, "*.*", &fd); truelight@0: if (h != INVALID_HANDLE_VALUE) { truelight@0: do { truelight@0: if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && tron@1468: strcmp(fd.cFileName, ".") != 0 && tron@1468: strcmp(fd.cFileName, "..") != 0) { truelight@0: fios = FiosAlloc(); truelight@0: fios->type = FIOS_TYPE_DIR; tron@1486: fios->mtime = 0; tron@1486: ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); tron@1486: snprintf(fios->title, lengthof(fios->title), tron@1486: "%s\\ (Directory)", fd.cFileName); truelight@0: } truelight@0: } while (FindNextFile(h, &fd)); truelight@0: FindClose(h); truelight@0: } truelight@0: truelight@0: // this is where to start sorting truelight@0: sort_start = _fios_count; truelight@0: tron@1486: /* Show savegame files tron@1486: * .SAV OpenTTD saved game tron@1486: * .SS1 Transport Tycoon Deluxe preset game tron@1486: * .SV1 Transport Tycoon Deluxe (Patch) saved game tron@1486: * .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game truelight@0: */ truelight@0: h = MyFindFirstFile(_fios_path, "*.*", &fd); truelight@0: if (h != INVALID_HANDLE_VALUE) { truelight@0: do { tron@1486: char *t; tron@1486: tron@1486: if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; tron@1486: tron@1486: t = strrchr(fd.cFileName, '.'); tron@1486: if (t != NULL && strcasecmp(t, ".sav") == 0) { // OpenTTD tron@1486: fios = FiosAlloc(); tron@1486: fios->type = FIOS_TYPE_FILE; tron@1486: fios->mtime = *(uint64*)&fd.ftLastWriteTime; tron@1486: fios->title[0] = '\0'; tron@1486: ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); tron@1486: } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO) { tron@1486: if (t != NULL && ( tron@1486: strcasecmp(t, ".ss1") == 0 || tron@1486: strcasecmp(t, ".sv1") == 0 || tron@1486: strcasecmp(t, ".sv2") == 0 tron@1486: )) { // TTDLX(Patch) tron@1486: char buf[MAX_PATH]; tron@1486: truelight@0: fios = FiosAlloc(); tron@1486: fios->type = FIOS_TYPE_OLDFILE; truelight@0: fios->mtime = *(uint64*)&fd.ftLastWriteTime; tron@1486: ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); truelight@0: sprintf(buf, "%s\\%s", _fios_path, fd.cFileName); tron@1486: GetOldSaveGameName(fios->title, buf); truelight@0: } truelight@0: } truelight@0: } while (FindNextFile(h, &fd)); truelight@0: FindClose(h); truelight@0: } truelight@0: truelight@0: qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); truelight@0: truelight@0: // Drives truelight@0: { truelight@0: char drives[256]; tron@1486: const char *s; tron@1486: truelight@0: GetLogicalDriveStrings(sizeof(drives), drives); tron@1486: for (s = drives; *s != '\0';) { truelight@0: fios = FiosAlloc(); truelight@0: fios->type = FIOS_TYPE_DRIVE; tron@1580: sprintf(fios->name, "%c:", s[0]); tron@1486: sprintf(fios->title, "%c:", s[0]); tron@1486: while (*s++ != '\0') {} truelight@0: } truelight@0: } tron@1486: truelight@0: *num = _fios_count; truelight@0: return _fios_items; truelight@0: } truelight@0: truelight@0: // Get a list of scenarios truelight@0: FiosItem *FiosGetScenarioList(int *num, int mode) truelight@0: { truelight@0: FiosItem *fios; truelight@0: WIN32_FIND_DATA fd; truelight@0: HANDLE h; truelight@0: int sort_start; truelight@0: truelight@0: if (mode == SLD_NEW_GAME || _fios_scn_path == NULL) { truelight@0: if (_fios_scn_path == NULL) truelight@0: _fios_scn_path = malloc(MAX_PATH); truelight@0: strcpy(_fios_scn_path, _path.scenario_dir); truelight@0: } truelight@0: truelight@0: _fios_path = _fios_scn_path; truelight@0: truelight@0: // Parent directory, only if not of the type C:\. tron@1468: if (_fios_path[3] != '\0' && mode != SLD_NEW_GAME) { truelight@0: fios = FiosAlloc(); truelight@0: fios->type = FIOS_TYPE_PARENT; tron@1486: fios->mtime = 0; truelight@0: strcpy(fios->title, ".. (Parent directory)"); truelight@0: } truelight@0: truelight@0: // Show subdirectories first truelight@0: h = MyFindFirstFile(_fios_scn_path, "*.*", &fd); truelight@0: if (h != INVALID_HANDLE_VALUE && mode != SLD_NEW_GAME) { truelight@0: do { truelight@0: if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && tron@1468: strcmp(fd.cFileName, ".") != 0 && tron@1468: strcmp(fd.cFileName, "..") != 0) { truelight@0: fios = FiosAlloc(); truelight@0: fios->type = FIOS_TYPE_DIR; tron@1486: fios->mtime = 0; tron@1486: ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); tron@1486: snprintf(fios->title, lengthof(fios->title), tron@1486: "%s\\ (Directory)", fd.cFileName); truelight@0: } truelight@0: } while (FindNextFile(h, &fd)); truelight@0: FindClose(h); truelight@0: } truelight@0: truelight@0: // this is where to start sorting truelight@0: sort_start = _fios_count; truelight@0: tron@1486: /* Show scenario files tron@1486: * .SCN OpenTTD style scenario file tron@1486: * .SV0 Transport Tycoon Deluxe (Patch) scenario tron@1486: * .SS0 Transport Tycoon Deluxe preset scenario truelight@0: */ truelight@0: h = MyFindFirstFile(_fios_scn_path, "*.*", &fd); truelight@0: if (h != INVALID_HANDLE_VALUE) { truelight@0: do { tron@1486: char *t; tron@1468: tron@1486: if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue; tron@1486: tron@1486: t = strrchr(fd.cFileName, '.'); tron@1486: if (t != NULL && strcasecmp(t, ".scn") == 0) { // OpenTTD tron@1486: fios = FiosAlloc(); tron@1486: fios->type = FIOS_TYPE_SCENARIO; tron@1486: fios->mtime = *(uint64*)&fd.ftLastWriteTime; tron@1486: fios->title[0] = '\0'; tron@1486: ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); tron@1486: } else if (mode == SLD_LOAD_GAME || mode == SLD_LOAD_SCENARIO || tron@1486: mode == SLD_NEW_GAME) { tron@1486: if (t != NULL && ( tron@1486: strcasecmp(t, ".sv0") == 0 || tron@1486: strcasecmp(t, ".ss0") == 0 tron@1486: )) { // TTDLX(Patch) tron@1486: char buf[MAX_PATH]; tron@1486: truelight@0: fios = FiosAlloc(); tron@1486: fios->type = FIOS_TYPE_OLD_SCENARIO; truelight@0: fios->mtime = *(uint64*)&fd.ftLastWriteTime; truelight@0: sprintf(buf, "%s\\%s", _fios_path, fd.cFileName); tron@1486: GetOldScenarioGameName(fios->title, buf); tron@1486: ttd_strlcpy(fios->name, fd.cFileName, lengthof(fios->name)); truelight@0: } truelight@0: } truelight@0: } while (FindNextFile(h, &fd)); truelight@0: FindClose(h); truelight@0: } truelight@0: truelight@0: qsort(_fios_items + sort_start, _fios_count - sort_start, sizeof(FiosItem), compare_FiosItems); truelight@0: truelight@0: // Drives truelight@0: if (mode != SLD_NEW_GAME) { truelight@0: char drives[256]; tron@1468: const char *s; tron@1468: truelight@0: GetLogicalDriveStrings(sizeof(drives), drives); tron@1468: for (s = drives; *s != '\0';) { truelight@0: fios = FiosAlloc(); truelight@0: fios->type = FIOS_TYPE_DRIVE; tron@1580: sprintf(fios->name, "%c:", s[0]); tron@1468: sprintf(fios->title, "%c:", s[0]); tron@1468: while (*s++ != '\0') {} truelight@0: } truelight@0: } truelight@0: truelight@0: *num = _fios_count; truelight@0: return _fios_items; truelight@0: } truelight@0: tron@1486: truelight@0: // Free the list of savegames darkvater@1102: void FiosFreeSavegameList(void) truelight@0: { truelight@0: free(_fios_items); truelight@0: _fios_items = NULL; truelight@0: _fios_alloc = _fios_count = 0; truelight@0: } truelight@0: truelight@0: // Browse to truelight@0: char *FiosBrowseTo(const FiosItem *item) truelight@0: { truelight@0: char *path = _fios_path; tron@1486: char *s; truelight@0: tron@1468: switch (item->type) { tron@1486: case FIOS_TYPE_DRIVE: tron@1486: sprintf(path, "%c:\\", item->title[0]); tron@1486: break; truelight@0: tron@1486: case FIOS_TYPE_PARENT: tron@1486: s = strrchr(path, '\\'); tron@1486: if (s != NULL) *s = '\0'; darkvater@1686: if (path[2] == '\0' ) strcat(path, "\\"); tron@1486: break; truelight@0: tron@1486: case FIOS_TYPE_DIR: tron@1486: s = strchr(item->name, '\\'); tron@1486: if (s != NULL) *s = '\0'; darkvater@1686: if (path[3] != '\0' ) strcat(path, "\\"); tron@1486: strcat(path, item->name); tron@1486: break; truelight@0: tron@1486: case FIOS_TYPE_FILE: tron@1486: case FIOS_TYPE_OLDFILE: tron@1486: case FIOS_TYPE_SCENARIO: tron@1486: case FIOS_TYPE_OLD_SCENARIO: { tron@1486: static char str_buffr[512]; tron@1468: tron@1486: sprintf(str_buffr, "%s\\%s", path, item->name); tron@1486: return str_buffr; tron@1486: } truelight@0: } truelight@0: truelight@0: return NULL; truelight@0: } truelight@0: darkvater@1596: /** darkvater@1596: * Get descriptive texts. Returns the path and free space darkvater@1596: * left on the device darkvater@1596: * @param path string describing the path darkvater@1596: * @param tfs total free space in megabytes, optional (can be NULL) darkvater@1596: * @return StringID describing the path (free space or failure) darkvater@1596: */ darkvater@1596: StringID FiosGetDescText(const char **path, uint32 *tot) truelight@0: { darkvater@1596: UINT sem = SetErrorMode(SEM_FAILCRITICALERRORS); // disable 'no-disk' message box truelight@0: char root[4]; truelight@0: DWORD spc, bps, nfc, tnc; darkvater@1596: StringID sid; darkvater@1596: truelight@0: *path = _fios_path; truelight@0: tron@1468: sprintf(root, "%c:\\", _fios_path[0]); darkvater@1596: if (tot != NULL && GetDiskFreeSpace(root, &spc, &bps, &nfc, &tnc)) { darkvater@1596: *tot = ((spc * bps) * (uint64)nfc) >> 20; darkvater@1596: sid = STR_4005_BYTES_FREE; darkvater@1596: } else darkvater@1596: sid = STR_4006_UNABLE_TO_READ_DRIVE; darkvater@1596: darkvater@1596: SetErrorMode(sem); // reset previous setting darkvater@1596: return sid; truelight@0: } truelight@0: truelight@0: void FiosMakeSavegameName(char *buf, const char *name) truelight@0: { tron@1508: const char* extension; tron@1508: const char* period; tron@1508: tron@1468: if (_game_mode == GM_EDITOR) tron@1508: extension = ".scn"; truelight@0: else tron@1508: extension = ".sav"; tron@1508: tron@1508: // Don't append the extension, if it is already there tron@1508: period = strrchr(name, '.'); tron@1508: if (period != NULL && strcasecmp(period, extension) == 0) extension = ""; tron@1508: tron@1508: sprintf(buf, "%s\\%s%s", _fios_path, name, extension); truelight@0: } truelight@0: truelight@0: void FiosDelete(const char *name) truelight@0: { tron@1336: char path[512]; tron@1336: tron@1495: snprintf(path, lengthof(path), "%s\\%s", _fios_path, name); truelight@0: DeleteFile(path); truelight@0: } truelight@0: darkvater@223: #define Windows_2000 5 darkvater@223: #define Windows_NT3_51 4 darkvater@223: darkvater@223: /* flags show the minimum required OS to use a given feature. Currently darkvater@287: only dwMajorVersion and dwMinorVersion (WindowsME) are used darkvater@223: MajorVersion MinorVersion darkvater@287: Windows Server 2003 5 2 dmusic darkvater@287: Windows XP 5 1 dmusic darkvater@287: Windows 2000 5 0 dmusic darkvater@287: Windows NT 4.0 4 0 win32 darkvater@287: Windows Me 4 90 dmusic darkvater@287: Windows 98 4 10 win32 darkvater@287: Windows 95 4 0 win32 darkvater@287: Windows NT 3.51 3 51 ????? darkvater@223: */ darkvater@223: truelight@0: const DriverDesc _video_driver_descs[] = { darkvater@223: {"null", "Null Video Driver", &_null_video_driver, 0}, truelight@0: #if defined(WITH_SDL) darkvater@223: {"sdl", "SDL Video Driver", &_sdl_video_driver, 1}, truelight@0: #endif darkvater@223: {"win32", "Win32 GDI Video Driver", &_win32_video_driver, Windows_NT3_51}, truelight@543: { "dedicated", "Dedicated Video Driver", &_dedicated_video_driver, 0}, tron@1466: { NULL, NULL, NULL, 0 } truelight@0: }; truelight@0: truelight@0: const DriverDesc _sound_driver_descs[] = { truelight@0: {"null", "Null Sound Driver", &_null_sound_driver, 0}, truelight@0: #if defined(WITH_SDL) darkvater@223: {"sdl", "SDL Sound Driver", &_sdl_sound_driver, 1}, truelight@0: #endif darkvater@223: {"win32", "Win32 WaveOut Driver", &_win32_sound_driver, Windows_NT3_51}, tron@1466: { NULL, NULL, NULL, 0 } truelight@0: }; truelight@0: truelight@0: const DriverDesc _music_driver_descs[] = { darkvater@223: {"null", "Null Music Driver", &_null_music_driver, 0}, truelight@0: #ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT darkvater@223: {"dmusic", "DirectMusic MIDI Driver", &_dmusic_midi_driver, Windows_2000}, truelight@0: #endif miham@826: // Win32 MIDI driver has higher priority than DMusic, so this one is chosen darkvater@223: {"win32", "Win32 MIDI Driver", &_win32_music_driver, Windows_NT3_51}, tron@1466: { NULL, NULL, NULL, 0 } truelight@0: }; truelight@0: darkvater@1102: byte GetOSVersion(void) darkvater@223: { darkvater@223: OSVERSIONINFO osvi; darkvater@223: darkvater@223: ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); darkvater@223: osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); darkvater@223: darkvater@223: if (GetVersionEx(&osvi)) { darkvater@287: DEBUG(misc, 2) ("Windows Version is %d.%d", osvi.dwMajorVersion, osvi.dwMinorVersion); darkvater@287: // WinME needs directmusic too (dmusic, Windows_2000 mode), all others default to OK darkvater@287: if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) { return Windows_2000;} // WinME darkvater@287: darkvater@287: return osvi.dwMajorVersion; darkvater@223: } darkvater@223: darkvater@223: // GetVersionEx failed, but we can safely assume at least Win95/WinNT3.51 is used darkvater@223: DEBUG(misc, 0) ("Windows version retrieval failed, defaulting to level 4"); darkvater@223: return Windows_NT3_51; darkvater@223: } darkvater@223: truelight@0: bool FileExists(const char *filename) truelight@0: { truelight@0: HANDLE hand = CreateFile(filename, 0, 0, NULL, OPEN_EXISTING, 0, NULL); truelight@0: if (hand == INVALID_HANDLE_VALUE) return false; truelight@0: CloseHandle(hand); truelight@0: return true; truelight@0: } truelight@0: truelight@0: static int CDECL LanguageCompareFunc(const void *a, const void *b) truelight@0: { darkvater@222: return strcmp(*(const char* const *)a, *(const char* const *)b); truelight@0: } truelight@0: truelight@0: int GetLanguageList(char **languages, int max) truelight@0: { truelight@0: HANDLE hand; truelight@0: int num = 0; truelight@0: char filedir[MAX_PATH]; truelight@0: WIN32_FIND_DATA fd; truelight@0: sprintf(filedir, "%s*.lng", _path.lang_dir); truelight@0: truelight@0: hand = FindFirstFile(filedir, &fd); truelight@0: if (hand != INVALID_HANDLE_VALUE) { truelight@0: do { truelight@0: if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { truelight@0: languages[num++] = strdup(fd.cFileName); truelight@0: if (num == max) break; truelight@0: } truelight@0: } while (FindNextFile(hand, &fd)); truelight@0: FindClose(hand); truelight@0: } truelight@0: truelight@0: qsort(languages, num, sizeof(char*), LanguageCompareFunc); truelight@0: return num; truelight@0: } truelight@0: truelight@0: static int ParseCommandLine(char *line, char **argv, int max_argc) truelight@0: { truelight@0: int n = 0; truelight@0: truelight@0: do { truelight@0: // skip whitespace truelight@0: while (*line == ' ' || *line == '\t') truelight@0: line++; truelight@0: truelight@0: // end? tron@1468: if (*line == '\0') truelight@0: break; truelight@193: truelight@0: // special handling when quoted truelight@0: if (*line == '"') { truelight@0: argv[n++] = ++line; truelight@0: while (*line != '"') { tron@1468: if (*line == '\0') return n; truelight@0: line++; truelight@0: } truelight@0: } else { truelight@0: argv[n++] = line; truelight@0: while (*line != ' ' && *line != '\t') { tron@1468: if (*line == '\0') return n; truelight@0: line++; truelight@0: } truelight@0: } tron@1468: *line++ = '\0'; truelight@0: } while (n != max_argc); truelight@0: truelight@0: return n; truelight@0: } truelight@0: truelight@0: truelight@0: #if defined(_MSC_VER) tron@1468: static uint64 _declspec(naked) rdtsc(void) truelight@0: { truelight@0: _asm { truelight@0: rdtsc truelight@0: ret truelight@0: } truelight@0: } truelight@0: #endif truelight@0: darkvater@1102: void CreateConsole(void) truelight@0: { truelight@0: HANDLE hand; truelight@0: CONSOLE_SCREEN_BUFFER_INFO coninfo; truelight@0: truelight@0: if (_has_console) return; truelight@0: truelight@0: _has_console = true; truelight@0: truelight@0: AllocConsole(); truelight@193: truelight@0: hand = GetStdHandle(STD_OUTPUT_HANDLE); truelight@0: GetConsoleScreenBufferInfo(hand, &coninfo); truelight@0: coninfo.dwSize.Y = 500; truelight@0: SetConsoleScreenBufferSize(hand, coninfo.dwSize); truelight@193: truelight@0: // redirect unbuffered STDIN, STDOUT, STDERR to the console truelight@0: #if !defined(__CYGWIN__) truelight@0: *stdout = *_fdopen( _open_osfhandle((long)hand, _O_TEXT), "w" ); truelight@543: *stdin = *_fdopen(_open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT), "r" ); truelight@0: *stderr = *_fdopen(_open_osfhandle((long)GetStdHandle(STD_ERROR_HANDLE), _O_TEXT), "w" ); truelight@0: #else truelight@0: // open_osfhandle is not in cygwin truelight@0: *stdout = *fdopen(1, "w" ); truelight@543: *stdin = *fdopen(0, "r" ); truelight@193: *stderr = *fdopen(2, "w" ); truelight@0: #endif truelight@193: tron@1468: setvbuf(stdin, NULL, _IONBF, 0); tron@1468: setvbuf(stdout, NULL, _IONBF, 0); tron@1468: setvbuf(stderr, NULL, _IONBF, 0); truelight@0: } truelight@0: truelight@0: void ShowInfo(const char *str) truelight@0: { truelight@0: if (_has_console) truelight@0: puts(str); truelight@0: else { truelight@0: bool old; truelight@193: truelight@0: ReleaseCapture(); truelight@0: _left_button_clicked =_left_button_down = false; truelight@193: truelight@0: old = MyShowCursor(true); truelight@0: if (MessageBoxA(GetActiveWindow(), str, "OpenTTD", MB_ICONINFORMATION | MB_OKCANCEL) == IDCANCEL) { truelight@0: CreateConsole(); truelight@0: } truelight@0: MyShowCursor(old); truelight@0: } truelight@0: } truelight@0: truelight@0: tron@1468: int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, tron@1468: LPTSTR lpCmdLine, int nCmdShow) truelight@0: { truelight@0: int argc; truelight@0: char *argv[64]; // max 64 command line arguments truelight@0: _inst = hInstance; truelight@0: truelight@0: #if defined(_DEBUG) truelight@0: CreateConsole(); truelight@0: #endif truelight@0: truelight@0: // make sure we have an autosave folder - Done in DeterminePaths truelight@0: // CreateDirectory("autosave", NULL); truelight@0: truelight@0: // setup random seed to something quite random truelight@0: #if defined(_MSC_VER) truelight@0: { truelight@0: uint64 seed = rdtsc(); tron@1468: _random_seeds[0][0] = seed & 0xffffffff; tron@1468: _random_seeds[0][1] = seed >> 32; truelight@0: } truelight@0: #else signde@206: _random_seeds[0][0] = GetTickCount(); signde@206: _random_seeds[0][1] = _random_seeds[0][0] * 0x1234567; truelight@0: #endif truelight@0: truelight@0: argc = ParseCommandLine(GetCommandLine(), argv, lengthof(argv)); truelight@0: truelight@0: #if defined(WIN32_EXCEPTION_TRACKER) truelight@193: { truelight@0: Win32InitializeExceptions(); truelight@0: } truelight@0: #endif truelight@0: truelight@0: #if defined(WIN32_EXCEPTION_TRACKER_DEBUG) truelight@0: _try { truelight@0: uint32 _stdcall ExceptionHandler(void *ep); truelight@0: #endif truelight@0: ttd_main(argc, argv); truelight@0: truelight@0: #if defined(WIN32_EXCEPTION_TRACKER_DEBUG) truelight@0: } _except (ExceptionHandler(_exception_info())) {} truelight@0: #endif truelight@0: truelight@0: return 0; truelight@0: } truelight@0: Darkvater@1390: void DeterminePaths(void) truelight@0: { truelight@0: char *s; truelight@0: char *cfg; truelight@0: truelight@0: _path.personal_dir = _path.game_data_dir = cfg = malloc(MAX_PATH); truelight@0: GetCurrentDirectory(MAX_PATH - 1, cfg); truelight@193: truelight@0: truelight@0: s = strchr(cfg, 0); tron@1468: if (s[-1] != '\\') strcpy(s, "\\"); truelight@0: truelight@0: _path.save_dir = str_fmt("%ssave", cfg); truelight@0: _path.autosave_dir = str_fmt("%s\\autosave", _path.save_dir); truelight@0: _path.scenario_dir = str_fmt("%sscenario", cfg); truelight@0: _path.gm_dir = str_fmt("%sgm\\", cfg); truelight@0: _path.data_dir = str_fmt("%sdata\\", cfg); truelight@0: _path.lang_dir = str_fmt("%slang\\", cfg); truelight@0: Darkvater@1482: if (_config_file == NULL) Darkvater@1482: _config_file = str_fmt("%sopenttd.cfg", _path.personal_dir); Darkvater@1482: darkvater@983: _highscore_file = str_fmt("%shs.dat", _path.personal_dir); truelight@704: _log_file = str_fmt("%sopenttd.log", _path.personal_dir); truelight@0: truelight@0: // make (auto)save and scenario folder truelight@0: CreateDirectory(_path.save_dir, NULL); truelight@0: CreateDirectory(_path.autosave_dir, NULL); truelight@0: CreateDirectory(_path.scenario_dir, NULL); truelight@0: } truelight@0: darkvater@1022: int CDECL snprintf(char *str, size_t size, const char *format, ...) truelight@543: { truelight@543: va_list ap; truelight@543: int ret; truelight@543: truelight@543: va_start(ap, format); truelight@543: ret = vsnprintf(str, size, format, ap); truelight@543: va_end(ap); truelight@543: return ret; truelight@543: } truelight@543: darkvater@1022: int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap) truelight@543: { truelight@543: int ret; truelight@543: ret = _vsnprintf(str, size, format, ap); truelight@543: if (ret < 0) str[size - 1] = '\0'; truelight@543: return ret; truelight@543: } Darkvater@1390: Darkvater@1390: /** Darkvater@1390: * Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard Darkvater@1390: * and append this up to the maximum length (either absolute or screenlength). If maxlength Darkvater@1390: * is zero, we don't care about the screenlength but only about the physical length of the string Darkvater@1390: * @param tb @Textbuf type to be changed Darkvater@1390: * @return Return true on successfull change of Textbuf, or false otherwise Darkvater@1390: */ Darkvater@1390: bool InsertTextBufferClipboard(Textbuf *tb) Darkvater@1390: { Darkvater@1390: if (IsClipboardFormatAvailable(CF_TEXT)) { Darkvater@1390: HGLOBAL cbuf; Darkvater@1390: const byte *data, *dataptr; Darkvater@1390: uint16 width = 0; Darkvater@1390: uint16 length = 0; Darkvater@1390: Darkvater@1390: OpenClipboard(NULL); Darkvater@1390: cbuf = GetClipboardData(CF_TEXT); Darkvater@1390: data = GlobalLock(cbuf); // clipboard data Darkvater@1390: dataptr = data; Darkvater@1390: Darkvater@1390: for (; IsValidAsciiChar(*dataptr) && (tb->length + length) < tb->maxlength - 1 && Darkvater@1390: (tb->maxwidth == 0 || width + tb->width + GetCharacterWidth((byte)*dataptr) <= tb->maxwidth); dataptr++) { Darkvater@1390: width += GetCharacterWidth((byte)*dataptr); Darkvater@1390: length++; Darkvater@1390: } Darkvater@1390: Darkvater@1390: if (length == 0) Darkvater@1390: return false; Darkvater@1390: Darkvater@1390: memmove(tb->buf + tb->caretpos + length, tb->buf + tb->caretpos, tb->length - tb->caretpos); Darkvater@1390: memcpy(tb->buf + tb->caretpos, data, length); Darkvater@1390: tb->width += width; Darkvater@1390: tb->caretxoffs += width; Darkvater@1390: Darkvater@1390: tb->length += length; Darkvater@1390: tb->caretpos += length; Darkvater@1390: tb->buf[tb->length + 1] = '\0'; // terminating zero Darkvater@1390: Darkvater@1390: GlobalUnlock(cbuf); Darkvater@1390: CloseClipboard(); Darkvater@1390: return true; Darkvater@1390: } Darkvater@1390: return false; Darkvater@1390: }