2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file win32_v.cpp Implementation of the Windows (GDI) video driver. */
|
|
|
|
|
2005-07-25 07:16:10 +00:00
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../openttd.h"
|
2007-12-23 10:56:02 +00:00
|
|
|
#include "../gfx_func.h"
|
2009-08-31 22:38:37 +00:00
|
|
|
#include "../os/windows/win32.h"
|
2008-05-04 21:53:36 +00:00
|
|
|
#include "../rev.h"
|
2007-06-17 20:40:02 +00:00
|
|
|
#include "../blitter/factory.hpp"
|
2007-12-22 23:30:28 +00:00
|
|
|
#include "../network/network.h"
|
2008-01-13 22:07:33 +00:00
|
|
|
#include "../core/math_func.hpp"
|
2008-01-29 00:45:24 +00:00
|
|
|
#include "../core/random_func.hpp"
|
2008-05-06 22:17:12 +00:00
|
|
|
#include "../texteff.hpp"
|
2011-12-10 16:54:46 +00:00
|
|
|
#include "../thread/thread.h"
|
|
|
|
#include "../progress.h"
|
2013-08-05 20:37:14 +00:00
|
|
|
#include "../window_gui.h"
|
2013-08-05 20:36:20 +00:00
|
|
|
#include "../window_func.h"
|
2005-07-25 07:16:10 +00:00
|
|
|
#include "win32_v.h"
|
2005-07-23 15:16:57 +00:00
|
|
|
#include <windows.h>
|
2013-08-05 20:37:06 +00:00
|
|
|
#include <imm.h>
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2013-08-05 20:36:20 +00:00
|
|
|
/* Missing define in MinGW headers. */
|
|
|
|
#ifndef MAPVK_VK_TO_CHAR
|
|
|
|
#define MAPVK_VK_TO_CHAR (2)
|
|
|
|
#endif
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
static struct {
|
|
|
|
HWND main_wnd;
|
|
|
|
HBITMAP dib_sect;
|
2007-06-12 20:24:12 +00:00
|
|
|
void *buffer_bits;
|
2005-07-23 15:16:57 +00:00
|
|
|
HPALETTE gdi_palette;
|
2011-12-10 16:54:46 +00:00
|
|
|
RECT update_rect;
|
2006-08-31 14:50:12 +00:00
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int width_org;
|
|
|
|
int height_org;
|
2005-07-23 15:16:57 +00:00
|
|
|
bool fullscreen;
|
|
|
|
bool has_focus;
|
|
|
|
bool running;
|
|
|
|
} _wnd;
|
|
|
|
|
2006-02-20 23:01:58 +00:00
|
|
|
bool _force_full_redraw;
|
2006-08-13 10:22:34 +00:00
|
|
|
bool _window_maximize;
|
2006-02-20 23:01:58 +00:00
|
|
|
uint _display_hz;
|
2008-06-16 19:38:41 +00:00
|
|
|
static Dimension _bck_resolution;
|
2013-08-05 20:37:25 +00:00
|
|
|
#if !defined(WINCE) || _WIN32_WCE >= 0x400
|
|
|
|
DWORD _imm_props;
|
|
|
|
#endif
|
2006-02-20 23:01:58 +00:00
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
/** Whether the drawing is/may be done in a separate thread. */
|
|
|
|
static bool _draw_threaded;
|
|
|
|
/** Thread used to 'draw' to the screen, i.e. push data to the screen. */
|
|
|
|
static ThreadObject *_draw_thread = NULL;
|
|
|
|
/** Mutex to keep the access to the shared memory controlled. */
|
|
|
|
static ThreadMutex *_draw_mutex = NULL;
|
2014-02-23 16:08:50 +00:00
|
|
|
/** Event that is signaled when the drawing thread has finished initializing. */
|
|
|
|
static HANDLE _draw_thread_initialized = NULL;
|
2011-12-10 16:54:46 +00:00
|
|
|
/** Should we keep continue drawing? */
|
|
|
|
static volatile bool _draw_continue;
|
|
|
|
/** Local copy of the palette for use in the drawing thread. */
|
|
|
|
static Palette _local_palette;
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void MakePalette()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2011-12-10 16:54:46 +00:00
|
|
|
LOGPALETTE *pal = (LOGPALETTE*)alloca(sizeof(LOGPALETTE) + (256 - 1) * sizeof(PALETTEENTRY));
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
pal->palVersion = 0x300;
|
|
|
|
pal->palNumEntries = 256;
|
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
for (uint i = 0; i != 256; i++) {
|
2011-12-08 18:13:29 +00:00
|
|
|
pal->palPalEntry[i].peRed = _cur_palette.palette[i].r;
|
|
|
|
pal->palPalEntry[i].peGreen = _cur_palette.palette[i].g;
|
|
|
|
pal->palPalEntry[i].peBlue = _cur_palette.palette[i].b;
|
2005-07-23 15:16:57 +00:00
|
|
|
pal->palPalEntry[i].peFlags = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
_wnd.gdi_palette = CreatePalette(pal);
|
2008-06-05 20:54:52 +00:00
|
|
|
if (_wnd.gdi_palette == NULL) usererror("CreatePalette failed!\n");
|
2011-12-10 16:54:46 +00:00
|
|
|
|
|
|
|
_cur_palette.first_dirty = 0;
|
|
|
|
_cur_palette.count_dirty = 256;
|
|
|
|
_local_palette = _cur_palette;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdatePalette(HDC dc, uint start, uint count)
|
|
|
|
{
|
|
|
|
RGBQUAD rgb[256];
|
|
|
|
uint i;
|
|
|
|
|
|
|
|
for (i = 0; i != count; i++) {
|
2011-12-10 16:54:46 +00:00
|
|
|
rgb[i].rgbRed = _local_palette.palette[start + i].r;
|
|
|
|
rgb[i].rgbGreen = _local_palette.palette[start + i].g;
|
|
|
|
rgb[i].rgbBlue = _local_palette.palette[start + i].b;
|
2005-07-23 15:16:57 +00:00
|
|
|
rgb[i].rgbReserved = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetDIBColorTable(dc, start, count, rgb);
|
|
|
|
}
|
|
|
|
|
2011-11-17 21:09:08 +00:00
|
|
|
bool VideoDriver_Win32::ClaimMousePointer()
|
|
|
|
{
|
|
|
|
MyShowCursor(false, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct VkMapping {
|
2005-07-23 15:16:57 +00:00
|
|
|
byte vk_from;
|
|
|
|
byte vk_count;
|
|
|
|
byte map_to;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
#define AS(x, z) {x, 0, z}
|
|
|
|
#define AM(x, y, z, w) {x, y - x, z}
|
|
|
|
|
|
|
|
static const VkMapping _vk_mapping[] = {
|
2007-07-23 16:48:19 +00:00
|
|
|
/* Pageup stuff + up/down */
|
2009-01-09 22:56:28 +00:00
|
|
|
AM(VK_PRIOR, VK_DOWN, WKC_PAGEUP, WKC_DOWN),
|
2007-07-23 16:48:19 +00:00
|
|
|
/* Map letters & digits */
|
2009-01-09 22:56:28 +00:00
|
|
|
AM('A', 'Z', 'A', 'Z'),
|
|
|
|
AM('0', '9', '0', '9'),
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
AS(VK_ESCAPE, WKC_ESC),
|
|
|
|
AS(VK_PAUSE, WKC_PAUSE),
|
|
|
|
AS(VK_BACK, WKC_BACKSPACE),
|
|
|
|
AM(VK_INSERT, VK_DELETE, WKC_INSERT, WKC_DELETE),
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-08-28 18:53:03 +00:00
|
|
|
AS(VK_SPACE, WKC_SPACE),
|
|
|
|
AS(VK_RETURN, WKC_RETURN),
|
|
|
|
AS(VK_TAB, WKC_TAB),
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2007-07-23 16:48:19 +00:00
|
|
|
/* Function keys */
|
2006-08-28 18:53:03 +00:00
|
|
|
AM(VK_F1, VK_F12, WKC_F1, WKC_F12),
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2007-07-23 16:48:19 +00:00
|
|
|
/* Numeric part */
|
2008-09-07 11:55:28 +00:00
|
|
|
AM(VK_NUMPAD0, VK_NUMPAD9, '0', '9'),
|
2006-08-28 18:53:03 +00:00
|
|
|
AS(VK_DIVIDE, WKC_NUM_DIV),
|
|
|
|
AS(VK_MULTIPLY, WKC_NUM_MUL),
|
|
|
|
AS(VK_SUBTRACT, WKC_NUM_MINUS),
|
|
|
|
AS(VK_ADD, WKC_NUM_PLUS),
|
2007-07-23 16:39:27 +00:00
|
|
|
AS(VK_DECIMAL, WKC_NUM_DECIMAL),
|
|
|
|
|
2007-07-23 16:48:19 +00:00
|
|
|
/* Other non-letter keys */
|
2007-07-23 16:39:27 +00:00
|
|
|
AS(0xBF, WKC_SLASH),
|
|
|
|
AS(0xBA, WKC_SEMICOLON),
|
|
|
|
AS(0xBB, WKC_EQUALS),
|
|
|
|
AS(0xDB, WKC_L_BRACKET),
|
|
|
|
AS(0xDC, WKC_BACKSLASH),
|
|
|
|
AS(0xDD, WKC_R_BRACKET),
|
|
|
|
|
|
|
|
AS(0xDE, WKC_SINGLEQUOTE),
|
|
|
|
AS(0xBC, WKC_COMMA),
|
|
|
|
AS(0xBD, WKC_MINUS),
|
|
|
|
AS(0xBE, WKC_PERIOD)
|
2005-07-23 15:16:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static uint MapWindowsKey(uint sym)
|
|
|
|
{
|
|
|
|
const VkMapping *map;
|
|
|
|
uint key = 0;
|
|
|
|
|
|
|
|
for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
|
|
|
|
if ((uint)(sym - map->vk_from) <= map->vk_count) {
|
|
|
|
key = sym - map->vk_from + map->map_to;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetAsyncKeyState(VK_SHIFT) < 0) key |= WKC_SHIFT;
|
|
|
|
if (GetAsyncKeyState(VK_CONTROL) < 0) key |= WKC_CTRL;
|
|
|
|
if (GetAsyncKeyState(VK_MENU) < 0) key |= WKC_ALT;
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2011-10-04 21:35:40 +00:00
|
|
|
static bool AllocateDibSection(int w, int h, bool force = false);
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
static void ClientSizeChanged(int w, int h)
|
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
/* allocate new dib section of the new size */
|
2005-07-23 15:16:57 +00:00
|
|
|
if (AllocateDibSection(w, h)) {
|
2011-12-15 22:02:00 +00:00
|
|
|
/* mark all palette colours dirty */
|
2011-12-08 18:13:29 +00:00
|
|
|
_cur_palette.first_dirty = 0;
|
|
|
|
_cur_palette.count_dirty = 256;
|
2011-12-10 16:54:46 +00:00
|
|
|
_local_palette = _cur_palette;
|
2010-01-04 02:32:36 +00:00
|
|
|
|
2014-01-02 22:41:58 +00:00
|
|
|
BlitterFactory::GetCurrentBlitter()->PostResize();
|
2010-01-04 02:32:36 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
GameSizeChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Keep this function here..
|
|
|
|
* It allows you to redraw the screen from within the MSVC debugger */
|
2007-03-07 11:47:46 +00:00
|
|
|
int RedrawScreenDebug()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2009-01-09 22:56:28 +00:00
|
|
|
HDC dc, dc2;
|
2005-07-23 15:16:57 +00:00
|
|
|
static int _fooctr;
|
|
|
|
HBITMAP old_bmp;
|
|
|
|
HPALETTE old_palette;
|
|
|
|
|
|
|
|
UpdateWindows();
|
|
|
|
|
|
|
|
dc = GetDC(_wnd.main_wnd);
|
|
|
|
dc2 = CreateCompatibleDC(dc);
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect);
|
2005-07-23 15:16:57 +00:00
|
|
|
old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE);
|
|
|
|
BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY);
|
|
|
|
SelectPalette(dc, old_palette, TRUE);
|
|
|
|
SelectObject(dc2, old_bmp);
|
|
|
|
DeleteDC(dc2);
|
|
|
|
ReleaseDC(_wnd.main_wnd, dc);
|
|
|
|
|
|
|
|
return _fooctr++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-11-05 01:53:12 +00:00
|
|
|
/* Windows 95 will not have a WM_MOUSELEAVE message, so define it if needed */
|
2006-03-24 00:42:35 +00:00
|
|
|
#if !defined(WM_MOUSELEAVE)
|
|
|
|
#define WM_MOUSELEAVE 0x02A3
|
|
|
|
#endif
|
|
|
|
#define TID_POLLMOUSE 1
|
|
|
|
#define MOUSE_POLL_DELAY 75
|
|
|
|
|
|
|
|
static void CALLBACK TrackMouseTimerProc(HWND hwnd, UINT msg, UINT event, DWORD time)
|
|
|
|
{
|
|
|
|
RECT rc;
|
|
|
|
POINT pt;
|
|
|
|
|
|
|
|
/* Get the rectangle of our window and translate it to screen coordinates.
|
|
|
|
* Compare this with the current screen coordinates of the mouse and if it
|
|
|
|
* falls outside of the area or our window we have left the window. */
|
|
|
|
GetClientRect(hwnd, &rc);
|
2006-05-09 17:24:08 +00:00
|
|
|
MapWindowPoints(hwnd, HWND_DESKTOP, (LPPOINT)(LPRECT)&rc, 2);
|
2006-03-24 00:42:35 +00:00
|
|
|
GetCursorPos(&pt);
|
|
|
|
|
|
|
|
if (!PtInRect(&rc, pt) || (WindowFromPoint(pt) != hwnd)) {
|
|
|
|
KillTimer(hwnd, event);
|
|
|
|
PostMessage(hwnd, WM_MOUSELEAVE, 0, 0L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-02 16:14:23 +00:00
|
|
|
/**
|
|
|
|
* Instantiate a new window.
|
|
|
|
* @param full_screen Whether to make a full screen window or not.
|
|
|
|
* @return True if the window could be created.
|
|
|
|
*/
|
2011-02-26 19:13:58 +00:00
|
|
|
bool VideoDriver_Win32::MakeWindow(bool full_screen)
|
2007-06-21 15:48:00 +00:00
|
|
|
{
|
|
|
|
_fullscreen = full_screen;
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* recreate window? */
|
2007-06-21 15:48:00 +00:00
|
|
|
if ((full_screen || _wnd.fullscreen) && _wnd.main_wnd) {
|
2007-08-09 05:34:56 +00:00
|
|
|
DestroyWindow(_wnd.main_wnd);
|
2007-06-21 15:48:00 +00:00
|
|
|
_wnd.main_wnd = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(WINCE)
|
|
|
|
/* WinCE is always fullscreen */
|
|
|
|
#else
|
|
|
|
if (full_screen) {
|
|
|
|
DEVMODE settings;
|
|
|
|
|
|
|
|
memset(&settings, 0, sizeof(settings));
|
|
|
|
settings.dmSize = sizeof(settings);
|
|
|
|
settings.dmFields =
|
2014-04-27 12:15:14 +00:00
|
|
|
DM_BITSPERPEL |
|
2007-06-21 15:48:00 +00:00
|
|
|
DM_PELSWIDTH |
|
|
|
|
DM_PELSHEIGHT |
|
|
|
|
(_display_hz != 0 ? DM_DISPLAYFREQUENCY : 0);
|
2014-04-27 12:15:14 +00:00
|
|
|
settings.dmBitsPerPel = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
|
2007-06-21 15:48:00 +00:00
|
|
|
settings.dmPelsWidth = _wnd.width_org;
|
|
|
|
settings.dmPelsHeight = _wnd.height_org;
|
|
|
|
settings.dmDisplayFrequency = _display_hz;
|
|
|
|
|
2012-11-25 12:14:13 +00:00
|
|
|
/* Check for 8 bpp support. */
|
2014-04-27 12:15:14 +00:00
|
|
|
if (settings.dmBitsPerPel == 8 &&
|
|
|
|
(_support8bpp != S8BPP_HARDWARE || ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL)) {
|
2012-11-25 12:14:13 +00:00
|
|
|
settings.dmBitsPerPel = 32;
|
|
|
|
}
|
|
|
|
|
2011-02-14 20:16:36 +00:00
|
|
|
/* Test fullscreen with current resolution, if it fails use desktop resolution. */
|
|
|
|
if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
|
|
|
|
RECT r;
|
|
|
|
GetWindowRect(GetDesktopWindow(), &r);
|
2012-11-25 12:14:13 +00:00
|
|
|
/* Guard against recursion. If we already failed here once, just fall through to
|
|
|
|
* the next ChangeDisplaySettings call which will fail and error out appropriately. */
|
2012-12-03 22:08:00 +00:00
|
|
|
if ((int)settings.dmPelsWidth != r.right - r.left || (int)settings.dmPelsHeight != r.bottom - r.top) {
|
2012-11-25 12:14:13 +00:00
|
|
|
return this->ChangeResolution(r.right - r.left, r.bottom - r.top);
|
|
|
|
}
|
2011-02-14 20:16:36 +00:00
|
|
|
}
|
|
|
|
|
2007-06-21 15:48:00 +00:00
|
|
|
if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
|
2011-02-26 19:13:58 +00:00
|
|
|
this->MakeWindow(false); // don't care about the result
|
2008-01-01 14:20:48 +00:00
|
|
|
return false; // the request failed
|
2007-06-21 15:48:00 +00:00
|
|
|
}
|
|
|
|
} else if (_wnd.fullscreen) {
|
2009-03-15 00:32:18 +00:00
|
|
|
/* restore display? */
|
2007-06-21 15:48:00 +00:00
|
|
|
ChangeDisplaySettings(NULL, 0);
|
2012-04-30 16:49:26 +00:00
|
|
|
/* restore the resolution */
|
|
|
|
_wnd.width = _bck_resolution.width;
|
|
|
|
_wnd.height = _bck_resolution.height;
|
2007-06-21 15:48:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
RECT r;
|
|
|
|
DWORD style, showstyle;
|
2012-07-10 17:47:03 +00:00
|
|
|
int w, h;
|
2007-06-21 15:48:00 +00:00
|
|
|
|
|
|
|
showstyle = SW_SHOWNORMAL;
|
|
|
|
_wnd.fullscreen = full_screen;
|
|
|
|
if (_wnd.fullscreen) {
|
|
|
|
style = WS_POPUP;
|
|
|
|
SetRect(&r, 0, 0, _wnd.width_org, _wnd.height_org);
|
|
|
|
} else {
|
|
|
|
style = WS_OVERLAPPEDWINDOW;
|
|
|
|
/* On window creation, check if we were in maximize mode before */
|
|
|
|
if (_window_maximize) showstyle = SW_SHOWMAXIMIZED;
|
|
|
|
SetRect(&r, 0, 0, _wnd.width, _wnd.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(WINCE)
|
|
|
|
AdjustWindowRect(&r, style, FALSE);
|
|
|
|
#endif
|
2012-07-10 17:47:03 +00:00
|
|
|
w = r.right - r.left;
|
|
|
|
h = r.bottom - r.top;
|
2012-04-30 16:48:47 +00:00
|
|
|
|
2012-07-10 17:47:03 +00:00
|
|
|
if (_wnd.main_wnd != NULL) {
|
|
|
|
if (!_window_maximize) SetWindowPos(_wnd.main_wnd, 0, 0, 0, w, h, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
|
|
|
|
} else {
|
2007-06-21 15:48:00 +00:00
|
|
|
TCHAR Windowtitle[50];
|
2012-07-10 17:47:03 +00:00
|
|
|
int x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
|
|
|
|
int y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
|
2007-06-21 15:48:00 +00:00
|
|
|
|
2008-09-24 23:25:24 +00:00
|
|
|
_sntprintf(Windowtitle, lengthof(Windowtitle), _T("OpenTTD %s"), MB_TO_WIDE(_openttd_revision));
|
2007-06-21 15:48:00 +00:00
|
|
|
|
|
|
|
_wnd.main_wnd = CreateWindow(_T("OTTD"), Windowtitle, style, x, y, w, h, 0, 0, GetModuleHandle(NULL), 0);
|
2008-06-05 20:54:52 +00:00
|
|
|
if (_wnd.main_wnd == NULL) usererror("CreateWindow failed");
|
2007-06-21 15:48:00 +00:00
|
|
|
ShowWindow(_wnd.main_wnd, showstyle);
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 02:32:36 +00:00
|
|
|
|
2014-01-02 22:41:58 +00:00
|
|
|
BlitterFactory::GetCurrentBlitter()->PostResize();
|
2010-01-04 02:32:36 +00:00
|
|
|
|
2007-06-21 15:48:00 +00:00
|
|
|
GameSizeChanged(); // invalidate all windows, force redraw
|
2013-01-08 22:46:42 +00:00
|
|
|
return true; // the request succeeded
|
2007-06-21 15:48:00 +00:00
|
|
|
}
|
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
/** Do palette animation and blit to the window. */
|
|
|
|
static void PaintWindow(HDC dc)
|
|
|
|
{
|
|
|
|
HDC dc2 = CreateCompatibleDC(dc);
|
|
|
|
HBITMAP old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect);
|
|
|
|
HPALETTE old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE);
|
|
|
|
|
|
|
|
if (_cur_palette.count_dirty != 0) {
|
2014-01-02 22:41:58 +00:00
|
|
|
Blitter *blitter = BlitterFactory::GetCurrentBlitter();
|
2011-12-10 16:54:46 +00:00
|
|
|
|
|
|
|
switch (blitter->UsePaletteAnimation()) {
|
|
|
|
case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
|
|
|
|
UpdatePalette(dc2, _local_palette.first_dirty, _local_palette.count_dirty);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Blitter::PALETTE_ANIMATION_BLITTER:
|
|
|
|
blitter->PaletteAnimate(_local_palette);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Blitter::PALETTE_ANIMATION_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
_cur_palette.count_dirty = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY);
|
|
|
|
SelectPalette(dc, old_palette, TRUE);
|
|
|
|
SelectObject(dc2, old_bmp);
|
|
|
|
DeleteDC(dc2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PaintWindowThread(void *)
|
|
|
|
{
|
|
|
|
/* First tell the main thread we're started */
|
|
|
|
_draw_mutex->BeginCritical();
|
2014-02-23 16:08:50 +00:00
|
|
|
SetEvent(_draw_thread_initialized);
|
2012-01-04 00:45:36 +00:00
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
/* Now wait for the first thing to draw! */
|
|
|
|
_draw_mutex->WaitForSignal();
|
|
|
|
|
|
|
|
while (_draw_continue) {
|
|
|
|
/* Convert update region from logical to device coordinates. */
|
|
|
|
POINT pt = {0, 0};
|
|
|
|
ClientToScreen(_wnd.main_wnd, &pt);
|
|
|
|
OffsetRect(&_wnd.update_rect, pt.x, pt.y);
|
|
|
|
|
|
|
|
/* Create a device context that is clipped to the region we need to draw.
|
|
|
|
* GetDCEx 'consumes' the update region, so we may not destroy it ourself. */
|
|
|
|
HRGN rgn = CreateRectRgnIndirect(&_wnd.update_rect);
|
|
|
|
HDC dc = GetDCEx(_wnd.main_wnd, rgn, DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_INTERSECTRGN);
|
|
|
|
|
|
|
|
PaintWindow(dc);
|
|
|
|
|
|
|
|
/* Clear update rect. */
|
|
|
|
SetRectEmpty(&_wnd.update_rect);
|
|
|
|
ReleaseDC(_wnd.main_wnd, dc);
|
|
|
|
|
|
|
|
/* Flush GDI buffer to ensure drawing here doesn't conflict with any GDI usage in the main WndProc. */
|
|
|
|
GdiFlush();
|
|
|
|
|
|
|
|
_draw_mutex->WaitForSignal();
|
|
|
|
}
|
|
|
|
|
|
|
|
_draw_mutex->EndCritical();
|
|
|
|
_draw_thread->Exit();
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:36:20 +00:00
|
|
|
/** Forward key presses to the window system. */
|
2013-08-05 20:36:40 +00:00
|
|
|
static LRESULT HandleCharMsg(uint keycode, WChar charcode)
|
2013-08-05 20:36:20 +00:00
|
|
|
{
|
|
|
|
#if !defined(UNICODE)
|
2013-08-05 20:36:51 +00:00
|
|
|
static char prev_char = 0;
|
|
|
|
|
|
|
|
char input[2] = {(char)charcode, 0};
|
|
|
|
int input_len = 1;
|
|
|
|
|
|
|
|
if (prev_char != 0) {
|
|
|
|
/* We stored a lead byte previously, combine it with this byte. */
|
|
|
|
input[0] = prev_char;
|
|
|
|
input[1] = (char)charcode;
|
|
|
|
input_len = 2;
|
|
|
|
} else if (IsDBCSLeadByte(charcode)) {
|
|
|
|
/* We got a lead byte, store and exit. */
|
|
|
|
prev_char = charcode;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
prev_char = 0;
|
|
|
|
|
|
|
|
wchar_t w[2]; // Can get up to two code points as a result.
|
|
|
|
int len = MultiByteToWideChar(CP_ACP, 0, input, input_len, w, 2);
|
|
|
|
switch (len) {
|
|
|
|
case 1: // Normal unicode character.
|
|
|
|
charcode = w[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // Got an UTF-16 surrogate pair back.
|
|
|
|
charcode = Utf16DecodeSurrogate(w[0], w[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // Some kind of error.
|
|
|
|
DEBUG(driver, 1, "Invalid DBCS character sequence encountered, dropping input");
|
|
|
|
charcode = 0;
|
|
|
|
break;
|
|
|
|
}
|
2013-08-05 20:36:40 +00:00
|
|
|
#else
|
|
|
|
static WChar prev_char = 0;
|
|
|
|
|
|
|
|
/* Did we get a lead surrogate? If yes, store and exit. */
|
|
|
|
if (Utf16IsLeadSurrogate(charcode)) {
|
|
|
|
if (prev_char != 0) DEBUG(driver, 1, "Got two UTF-16 lead surrogates, dropping the first one");
|
|
|
|
prev_char = charcode;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stored lead surrogate and incoming trail surrogate? Combine and forward to input handling. */
|
|
|
|
if (prev_char != 0) {
|
|
|
|
if (Utf16IsTrailSurrogate(charcode)) {
|
|
|
|
charcode = Utf16DecodeSurrogate(prev_char, charcode);
|
|
|
|
} else {
|
|
|
|
DEBUG(driver, 1, "Got an UTF-16 lead surrogate without a trail surrogate, dropping the lead surrogate");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prev_char = 0;
|
2013-08-05 20:36:20 +00:00
|
|
|
#endif /* UNICODE */
|
|
|
|
|
2013-08-05 20:36:36 +00:00
|
|
|
HandleKeypress(keycode, charcode);
|
2013-08-05 20:36:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:06 +00:00
|
|
|
#if !defined(WINCE) || _WIN32_WCE >= 0x400
|
2013-08-05 20:37:25 +00:00
|
|
|
/** Should we draw the composition string ourself, i.e is this a normal IME? */
|
|
|
|
static bool DrawIMECompositionString()
|
|
|
|
{
|
|
|
|
return (_imm_props & IME_PROP_AT_CARET) && !(_imm_props & IME_PROP_SPECIAL_UI);
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:14 +00:00
|
|
|
/** Set position of the composition window to the caret position. */
|
|
|
|
static void SetCompositionPos(HWND hwnd)
|
|
|
|
{
|
|
|
|
HIMC hIMC = ImmGetContext(hwnd);
|
|
|
|
if (hIMC != NULL) {
|
|
|
|
COMPOSITIONFORM cf;
|
|
|
|
cf.dwStyle = CFS_POINT;
|
|
|
|
|
|
|
|
if (EditBoxInGlobalFocus()) {
|
|
|
|
/* Get caret position. */
|
|
|
|
Point pt = _focused_window->GetCaretPosition();
|
|
|
|
cf.ptCurrentPos.x = _focused_window->left + pt.x;
|
|
|
|
cf.ptCurrentPos.y = _focused_window->top + pt.y;
|
|
|
|
} else {
|
|
|
|
cf.ptCurrentPos.x = 0;
|
|
|
|
cf.ptCurrentPos.y = 0;
|
|
|
|
}
|
|
|
|
ImmSetCompositionWindow(hIMC, &cf);
|
|
|
|
}
|
|
|
|
ImmReleaseContext(hwnd, hIMC);
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:18 +00:00
|
|
|
/** Set the position of the candidate window. */
|
|
|
|
static void SetCandidatePos(HWND hwnd)
|
|
|
|
{
|
|
|
|
HIMC hIMC = ImmGetContext(hwnd);
|
|
|
|
if (hIMC != NULL) {
|
|
|
|
CANDIDATEFORM cf;
|
|
|
|
cf.dwIndex = 0;
|
|
|
|
cf.dwStyle = CFS_EXCLUDE;
|
|
|
|
|
|
|
|
if (EditBoxInGlobalFocus()) {
|
|
|
|
Point pt = _focused_window->GetCaretPosition();
|
|
|
|
cf.ptCurrentPos.x = _focused_window->left + pt.x;
|
|
|
|
cf.ptCurrentPos.y = _focused_window->top + pt.y;
|
|
|
|
if (_focused_window->window_class == WC_CONSOLE) {
|
|
|
|
cf.rcArea.left = _focused_window->left;
|
|
|
|
cf.rcArea.top = _focused_window->top;
|
|
|
|
cf.rcArea.right = _focused_window->left + _focused_window->width;
|
|
|
|
cf.rcArea.bottom = _focused_window->top + _focused_window->height;
|
|
|
|
} else {
|
|
|
|
cf.rcArea.left = _focused_window->left + _focused_window->nested_focus->pos_x;
|
|
|
|
cf.rcArea.top = _focused_window->top + _focused_window->nested_focus->pos_y;
|
|
|
|
cf.rcArea.right = cf.rcArea.left + _focused_window->nested_focus->current_x;
|
|
|
|
cf.rcArea.bottom = cf.rcArea.top + _focused_window->nested_focus->current_y;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cf.ptCurrentPos.x = 0;
|
|
|
|
cf.ptCurrentPos.y = 0;
|
|
|
|
SetRectEmpty(&cf.rcArea);
|
|
|
|
}
|
|
|
|
ImmSetCandidateWindow(hIMC, &cf);
|
|
|
|
}
|
|
|
|
ImmReleaseContext(hwnd, hIMC);
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:37:06 +00:00
|
|
|
/** Handle WM_IME_COMPOSITION messages. */
|
|
|
|
static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
HIMC hIMC = ImmGetContext(hwnd);
|
|
|
|
|
|
|
|
if (hIMC != NULL) {
|
|
|
|
if (lParam & GCS_RESULTSTR) {
|
|
|
|
/* Read result string from the IME. */
|
|
|
|
LONG len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, NULL, 0); // Length is always in bytes, even in UNICODE build.
|
|
|
|
TCHAR *str = (TCHAR *)_alloca(len + sizeof(TCHAR));
|
|
|
|
len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, str, len);
|
|
|
|
str[len / sizeof(TCHAR)] = '\0';
|
|
|
|
|
|
|
|
/* Transmit text to windowing system. */
|
2013-08-05 20:37:25 +00:00
|
|
|
if (len > 0) {
|
|
|
|
HandleTextInput(NULL, true); // Clear marked string.
|
|
|
|
HandleTextInput(FS2OTTD(str));
|
|
|
|
}
|
2013-08-05 20:37:14 +00:00
|
|
|
SetCompositionPos(hwnd);
|
2013-08-05 20:37:06 +00:00
|
|
|
|
|
|
|
/* Don't pass the result string on to the default window proc. */
|
|
|
|
lParam &= ~(GCS_RESULTSTR | GCS_RESULTCLAUSE | GCS_RESULTREADCLAUSE | GCS_RESULTREADSTR);
|
|
|
|
}
|
2013-08-05 20:37:25 +00:00
|
|
|
|
|
|
|
if ((lParam & GCS_COMPSTR) && DrawIMECompositionString()) {
|
|
|
|
/* Read composition string from the IME. */
|
|
|
|
LONG len = ImmGetCompositionString(hIMC, GCS_COMPSTR, NULL, 0); // Length is always in bytes, even in UNICODE build.
|
|
|
|
TCHAR *str = (TCHAR *)_alloca(len + sizeof(TCHAR));
|
|
|
|
len = ImmGetCompositionString(hIMC, GCS_COMPSTR, str, len);
|
|
|
|
str[len / sizeof(TCHAR)] = '\0';
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
static char utf8_buf[1024];
|
|
|
|
convert_from_fs(str, utf8_buf, lengthof(utf8_buf));
|
|
|
|
|
|
|
|
/* Convert caret position from bytes in the input string to a position in the UTF-8 encoded string. */
|
|
|
|
LONG caret_bytes = ImmGetCompositionString(hIMC, GCS_CURSORPOS, NULL, 0);
|
|
|
|
const char *caret = utf8_buf;
|
|
|
|
for (const TCHAR *c = str; *c != '\0' && *caret != '\0' && caret_bytes > 0; c++, caret_bytes--) {
|
|
|
|
/* Skip DBCS lead bytes or leading surrogates. */
|
|
|
|
#ifdef UNICODE
|
|
|
|
if (Utf16IsLeadSurrogate(*c)) {
|
|
|
|
#else
|
|
|
|
if (IsDBCSLeadByte(*c)) {
|
|
|
|
#endif
|
|
|
|
c++;
|
|
|
|
caret_bytes--;
|
|
|
|
}
|
|
|
|
Utf8Consume(&caret);
|
|
|
|
}
|
|
|
|
|
|
|
|
HandleTextInput(utf8_buf, true, caret);
|
|
|
|
} else {
|
|
|
|
HandleTextInput(NULL, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
lParam &= ~(GCS_COMPSTR | GCS_COMPATTR | GCS_COMPCLAUSE | GCS_CURSORPOS | GCS_DELTASTART);
|
|
|
|
}
|
2013-08-05 20:37:06 +00:00
|
|
|
}
|
|
|
|
ImmReleaseContext(hwnd, hIMC);
|
|
|
|
|
|
|
|
return lParam != 0 ? DefWindowProc(hwnd, WM_IME_COMPOSITION, wParam, lParam) : 0;
|
|
|
|
}
|
2013-08-05 20:37:11 +00:00
|
|
|
|
|
|
|
/** Clear the current composition string. */
|
|
|
|
static void CancelIMEComposition(HWND hwnd)
|
|
|
|
{
|
|
|
|
HIMC hIMC = ImmGetContext(hwnd);
|
|
|
|
if (hIMC != NULL) ImmNotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
|
|
|
|
ImmReleaseContext(hwnd, hIMC);
|
2013-08-05 20:37:25 +00:00
|
|
|
/* Clear any marked string from the current edit box. */
|
|
|
|
HandleTextInput(NULL, true);
|
2013-08-05 20:37:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2013-08-05 20:37:25 +00:00
|
|
|
static bool DrawIMECompositionString() { return false; }
|
2013-08-05 20:37:14 +00:00
|
|
|
static void SetCompositionPos(HWND hwnd) {}
|
2013-08-05 20:37:18 +00:00
|
|
|
static void SetCandidatePos(HWND hwnd) {}
|
2013-08-05 20:37:11 +00:00
|
|
|
static void CancelIMEComposition(HWND hwnd) {}
|
|
|
|
|
|
|
|
#endif /* !defined(WINCE) || _WIN32_WCE >= 0x400 */
|
2013-08-05 20:37:06 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
static LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
2007-03-10 00:30:18 +00:00
|
|
|
static uint32 keycode = 0;
|
2007-04-27 21:27:02 +00:00
|
|
|
static bool console = false;
|
2011-12-10 16:54:46 +00:00
|
|
|
static bool in_sizemove = false;
|
2007-03-10 00:30:18 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
switch (msg) {
|
2006-08-31 14:50:12 +00:00
|
|
|
case WM_CREATE:
|
|
|
|
SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc);
|
2013-08-05 20:37:14 +00:00
|
|
|
SetCompositionPos(hwnd);
|
2013-08-05 20:37:25 +00:00
|
|
|
#if !defined(WINCE) || _WIN32_WCE >= 0x400
|
|
|
|
_imm_props = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
|
|
|
|
#endif
|
2006-08-31 14:50:12 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
case WM_ENTERSIZEMOVE:
|
|
|
|
in_sizemove = true;
|
|
|
|
break;
|
2007-06-19 15:18:26 +00:00
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
case WM_EXITSIZEMOVE:
|
|
|
|
in_sizemove = false;
|
|
|
|
break;
|
2007-06-19 15:04:08 +00:00
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
case WM_PAINT:
|
|
|
|
if (!in_sizemove && _draw_mutex != NULL && !HasModalProgress()) {
|
|
|
|
/* Get the union of the old update rect and the new update rect. */
|
|
|
|
RECT r;
|
|
|
|
GetUpdateRect(hwnd, &r, FALSE);
|
|
|
|
UnionRect(&_wnd.update_rect, &_wnd.update_rect, &r);
|
2007-06-19 15:04:08 +00:00
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
/* Mark the window as updated, otherwise Windows would send more WM_PAINT messages. */
|
|
|
|
ValidateRect(hwnd, NULL);
|
|
|
|
_draw_mutex->SendSignal();
|
|
|
|
} else {
|
|
|
|
PAINTSTRUCT ps;
|
2007-06-19 15:04:08 +00:00
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
BeginPaint(hwnd, &ps);
|
|
|
|
PaintWindow(ps.hdc);
|
|
|
|
EndPaint(hwnd, &ps);
|
2006-08-31 14:50:12 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-08-31 14:50:12 +00:00
|
|
|
case WM_PALETTECHANGED:
|
|
|
|
if ((HWND)wParam == hwnd) return 0;
|
2010-07-29 14:26:28 +00:00
|
|
|
/* FALL THROUGH */
|
2006-08-31 14:50:12 +00:00
|
|
|
|
|
|
|
case WM_QUERYNEWPALETTE: {
|
|
|
|
HDC hDC = GetWindowDC(hwnd);
|
|
|
|
HPALETTE hOldPalette = SelectPalette(hDC, _wnd.gdi_palette, FALSE);
|
|
|
|
UINT nChanged = RealizePalette(hDC);
|
|
|
|
|
|
|
|
SelectPalette(hDC, hOldPalette, TRUE);
|
|
|
|
ReleaseDC(hwnd, hDC);
|
2011-02-25 22:04:38 +00:00
|
|
|
if (nChanged != 0) InvalidateRect(hwnd, NULL, FALSE);
|
2006-08-31 14:50:12 +00:00
|
|
|
return 0;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2006-08-31 14:50:12 +00:00
|
|
|
case WM_CLOSE:
|
2006-09-04 17:30:30 +00:00
|
|
|
HandleExitGameRequest();
|
2006-08-31 14:50:12 +00:00
|
|
|
return 0;
|
|
|
|
|
2006-11-05 01:13:08 +00:00
|
|
|
case WM_DESTROY:
|
2008-06-16 19:38:41 +00:00
|
|
|
if (_window_maximize) _cur_resolution = _bck_resolution;
|
2006-11-05 01:13:08 +00:00
|
|
|
return 0;
|
|
|
|
|
2006-08-31 14:50:12 +00:00
|
|
|
case WM_LBUTTONDOWN:
|
|
|
|
SetCapture(hwnd);
|
|
|
|
_left_button_down = true;
|
2006-11-15 21:01:19 +00:00
|
|
|
HandleMouseEvents();
|
2005-07-23 15:16:57 +00:00
|
|
|
return 0;
|
|
|
|
|
2006-08-31 14:50:12 +00:00
|
|
|
case WM_LBUTTONUP:
|
|
|
|
ReleaseCapture();
|
|
|
|
_left_button_down = false;
|
|
|
|
_left_button_clicked = false;
|
2006-11-15 21:01:19 +00:00
|
|
|
HandleMouseEvents();
|
2006-08-31 14:50:12 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WM_RBUTTONDOWN:
|
|
|
|
SetCapture(hwnd);
|
|
|
|
_right_button_down = true;
|
|
|
|
_right_button_clicked = true;
|
2006-11-15 21:01:19 +00:00
|
|
|
HandleMouseEvents();
|
2006-08-31 14:50:12 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WM_RBUTTONUP:
|
|
|
|
ReleaseCapture();
|
|
|
|
_right_button_down = false;
|
2006-11-15 21:01:19 +00:00
|
|
|
HandleMouseEvents();
|
2006-08-31 14:50:12 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WM_MOUSELEAVE:
|
|
|
|
UndrawMouseCursor();
|
|
|
|
_cursor.in_window = false;
|
2006-11-05 01:53:12 +00:00
|
|
|
|
|
|
|
if (!_left_button_down && !_right_button_down) MyShowCursor(true);
|
|
|
|
return 0;
|
2006-03-24 00:42:35 +00:00
|
|
|
|
2006-08-31 14:50:12 +00:00
|
|
|
case WM_MOUSEMOVE: {
|
|
|
|
int x = (int16)LOWORD(lParam);
|
|
|
|
int y = (int16)HIWORD(lParam);
|
|
|
|
POINT pt;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-08-31 14:50:12 +00:00
|
|
|
/* If the mouse was not in the window and it has moved it means it has
|
2006-11-05 01:53:12 +00:00
|
|
|
* come into the window, so start drawing the mouse. Also start
|
2006-08-31 14:50:12 +00:00
|
|
|
* tracking the mouse for exiting the window */
|
|
|
|
if (!_cursor.in_window) {
|
|
|
|
_cursor.in_window = true;
|
|
|
|
SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc);
|
|
|
|
}
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2015-02-22 23:06:45 +00:00
|
|
|
if (_cursor.UpdateCursorPosition(x, y, true)) {
|
|
|
|
pt.x = _cursor.pos.x;
|
|
|
|
pt.y = _cursor.pos.y;
|
|
|
|
ClientToScreen(hwnd, &pt);
|
|
|
|
SetCursorPos(pt.x, pt.y);
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
2006-08-31 14:50:12 +00:00
|
|
|
MyShowCursor(false);
|
2006-11-15 21:01:19 +00:00
|
|
|
HandleMouseEvents();
|
2006-08-31 14:50:12 +00:00
|
|
|
return 0;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2013-08-05 20:36:51 +00:00
|
|
|
#if !defined(WINCE) || _WIN32_WCE >= 0x400
|
2013-08-05 20:37:25 +00:00
|
|
|
case WM_INPUTLANGCHANGE:
|
|
|
|
_imm_props = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_IME_SETCONTEXT:
|
|
|
|
/* Don't show the composition window if we draw the string ourself. */
|
|
|
|
if (DrawIMECompositionString()) lParam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
|
|
|
|
break;
|
|
|
|
|
2013-08-05 20:37:14 +00:00
|
|
|
case WM_IME_STARTCOMPOSITION:
|
|
|
|
SetCompositionPos(hwnd);
|
2013-08-05 20:37:25 +00:00
|
|
|
if (DrawIMECompositionString()) return 0;
|
2013-08-05 20:37:14 +00:00
|
|
|
break;
|
|
|
|
|
2013-08-05 20:37:06 +00:00
|
|
|
case WM_IME_COMPOSITION:
|
|
|
|
return HandleIMEComposition(hwnd, wParam, lParam);
|
|
|
|
|
2013-08-05 20:37:25 +00:00
|
|
|
case WM_IME_ENDCOMPOSITION:
|
|
|
|
/* Clear any pending composition string. */
|
|
|
|
HandleTextInput(NULL, true);
|
|
|
|
if (DrawIMECompositionString()) return 0;
|
|
|
|
break;
|
|
|
|
|
2013-08-05 20:37:18 +00:00
|
|
|
case WM_IME_NOTIFY:
|
|
|
|
if (wParam == IMN_OPENCANDIDATE) SetCandidatePos(hwnd);
|
|
|
|
break;
|
|
|
|
|
2013-08-05 20:36:51 +00:00
|
|
|
#if !defined(UNICODE)
|
|
|
|
case WM_IME_CHAR:
|
|
|
|
if (GB(wParam, 8, 8) != 0) {
|
|
|
|
/* DBCS character, send lead byte first. */
|
|
|
|
HandleCharMsg(0, GB(wParam, 8, 8));
|
|
|
|
}
|
|
|
|
HandleCharMsg(0, GB(wParam, 0, 8));
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2007-04-27 21:27:02 +00:00
|
|
|
case WM_DEADCHAR:
|
|
|
|
console = GB(lParam, 16, 8) == 41;
|
|
|
|
return 0;
|
|
|
|
|
2007-03-10 00:30:18 +00:00
|
|
|
case WM_CHAR: {
|
|
|
|
uint scancode = GB(lParam, 16, 8);
|
|
|
|
uint charcode = wParam;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2007-04-27 21:27:02 +00:00
|
|
|
/* If the console key is a dead-key, we need to press it twice to get a WM_CHAR message.
|
|
|
|
* But we then get two WM_CHAR messages, so ignore the first one */
|
|
|
|
if (console && scancode == 41) {
|
|
|
|
console = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:36:20 +00:00
|
|
|
/* IMEs and other input methods sometimes send a WM_CHAR without a WM_KEYDOWN,
|
|
|
|
* clear the keycode so a previous WM_KEYDOWN doesn't become 'stuck'. */
|
|
|
|
uint cur_keycode = keycode;
|
|
|
|
keycode = 0;
|
2007-03-10 00:30:18 +00:00
|
|
|
|
2013-08-05 20:36:20 +00:00
|
|
|
return HandleCharMsg(cur_keycode, charcode);
|
2007-03-10 00:30:18 +00:00
|
|
|
}
|
2006-08-31 14:50:12 +00:00
|
|
|
|
2007-03-10 00:30:18 +00:00
|
|
|
case WM_KEYDOWN: {
|
2013-08-05 20:36:20 +00:00
|
|
|
/* No matter the keyboard layout, we will map the '~' to the console. */
|
|
|
|
uint scancode = GB(lParam, 16, 8);
|
2013-11-16 10:05:57 +00:00
|
|
|
keycode = scancode == 41 ? (uint)WKC_BACKQUOTE : MapWindowsKey(wParam);
|
2006-08-31 14:50:12 +00:00
|
|
|
|
2008-09-20 16:07:56 +00:00
|
|
|
/* Silently drop all messages handled by WM_CHAR. */
|
|
|
|
MSG msg;
|
|
|
|
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
|
2013-08-05 20:36:20 +00:00
|
|
|
if ((msg.message == WM_CHAR || msg.message == WM_DEADCHAR) && GB(lParam, 16, 8) == GB(msg.lParam, 16, 8)) {
|
2008-09-20 16:07:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2007-09-08 14:59:58 +00:00
|
|
|
|
2013-08-05 20:36:20 +00:00
|
|
|
uint charcode = MapVirtualKey(wParam, MAPVK_VK_TO_CHAR);
|
|
|
|
|
|
|
|
/* No character translation? */
|
|
|
|
if (charcode == 0) {
|
2013-08-05 20:36:36 +00:00
|
|
|
HandleKeypress(keycode, 0);
|
2013-08-05 20:36:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the console key a dead key? If yes, ignore the first key down event. */
|
|
|
|
if (HasBit(charcode, 31) && !console) {
|
|
|
|
if (scancode == 41) {
|
|
|
|
console = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console = false;
|
|
|
|
|
|
|
|
/* IMEs and other input methods sometimes send a WM_CHAR without a WM_KEYDOWN,
|
|
|
|
* clear the keycode so a previous WM_KEYDOWN doesn't become 'stuck'. */
|
|
|
|
uint cur_keycode = keycode;
|
|
|
|
keycode = 0;
|
|
|
|
|
|
|
|
return HandleCharMsg(cur_keycode, LOWORD(charcode));
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
case WM_SYSKEYDOWN: // user presses F10 or Alt, both activating the title-menu
|
2006-08-31 14:50:12 +00:00
|
|
|
switch (wParam) {
|
|
|
|
case VK_RETURN:
|
2009-03-15 00:32:18 +00:00
|
|
|
case 'F': // Full Screen on ALT + ENTER/F
|
2006-08-31 14:50:12 +00:00
|
|
|
ToggleFullScreen(!_wnd.fullscreen);
|
|
|
|
return 0;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
case VK_MENU: // Just ALT
|
2006-08-31 14:50:12 +00:00
|
|
|
return 0; // do nothing
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
case VK_F10: // F10, ignore activation of menu
|
2013-08-05 20:36:36 +00:00
|
|
|
HandleKeypress(MapWindowsKey(wParam), 0);
|
2006-08-31 14:50:12 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
default: // ALT in combination with something else
|
2013-08-05 20:36:36 +00:00
|
|
|
HandleKeypress(MapWindowsKey(wParam), 0);
|
2006-08-31 14:50:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-07-23 15:16:57 +00:00
|
|
|
break;
|
2006-08-31 14:50:12 +00:00
|
|
|
|
|
|
|
case WM_SIZE:
|
2007-06-30 15:02:21 +00:00
|
|
|
if (wParam != SIZE_MINIMIZED) {
|
2006-11-05 01:09:57 +00:00
|
|
|
/* Set maximized flag when we maximize (obviously), but also when we
|
|
|
|
* switched to fullscreen from a maximized state */
|
|
|
|
_window_maximize = (wParam == SIZE_MAXIMIZED || (_window_maximize && _fullscreen));
|
2012-04-30 16:49:26 +00:00
|
|
|
if (_window_maximize || _fullscreen) _bck_resolution = _cur_resolution;
|
2006-08-31 14:50:12 +00:00
|
|
|
ClientSizeChanged(LOWORD(lParam), HIWORD(lParam));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
2007-01-21 14:32:40 +00:00
|
|
|
#if !defined(WINCE)
|
2006-08-31 14:50:12 +00:00
|
|
|
case WM_SIZING: {
|
2009-01-10 00:31:47 +00:00
|
|
|
RECT *r = (RECT*)lParam;
|
2006-08-31 14:50:12 +00:00
|
|
|
RECT r2;
|
|
|
|
int w, h;
|
|
|
|
|
|
|
|
SetRect(&r2, 0, 0, 0, 0);
|
|
|
|
AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
|
|
|
|
|
|
|
|
w = r->right - r->left - (r2.right - r2.left);
|
|
|
|
h = r->bottom - r->top - (r2.bottom - r2.top);
|
2008-04-18 21:49:38 +00:00
|
|
|
w = max(w, 64);
|
|
|
|
h = max(h, 64);
|
2006-08-31 14:50:12 +00:00
|
|
|
SetRect(&r2, 0, 0, w, h);
|
|
|
|
|
|
|
|
AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
|
|
|
|
w = r2.right - r2.left;
|
|
|
|
h = r2.bottom - r2.top;
|
|
|
|
|
|
|
|
switch (wParam) {
|
|
|
|
case WMSZ_BOTTOM:
|
|
|
|
r->bottom = r->top + h;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WMSZ_BOTTOMLEFT:
|
|
|
|
r->bottom = r->top + h;
|
|
|
|
r->left = r->right - w;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WMSZ_BOTTOMRIGHT:
|
|
|
|
r->bottom = r->top + h;
|
|
|
|
r->right = r->left + w;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WMSZ_LEFT:
|
|
|
|
r->left = r->right - w;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WMSZ_RIGHT:
|
|
|
|
r->right = r->left + w;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WMSZ_TOP:
|
|
|
|
r->top = r->bottom - h;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WMSZ_TOPLEFT:
|
|
|
|
r->top = r->bottom - h;
|
|
|
|
r->left = r->right - w;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WMSZ_TOPRIGHT:
|
|
|
|
r->top = r->bottom - h;
|
|
|
|
r->right = r->left + w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
2007-01-21 14:36:08 +00:00
|
|
|
#endif
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* needed for wheel */
|
2005-07-23 15:16:57 +00:00
|
|
|
#if !defined(WM_MOUSEWHEEL)
|
2006-08-31 14:50:12 +00:00
|
|
|
# define WM_MOUSEWHEEL 0x020A
|
2009-03-15 00:32:18 +00:00
|
|
|
#endif /* WM_MOUSEWHEEL */
|
2005-07-23 15:16:57 +00:00
|
|
|
#if !defined(GET_WHEEL_DELTA_WPARAM)
|
|
|
|
# define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam))
|
2009-03-15 00:32:18 +00:00
|
|
|
#endif /* GET_WHEEL_DELTA_WPARAM */
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-08-31 14:50:12 +00:00
|
|
|
case WM_MOUSEWHEEL: {
|
|
|
|
int delta = GET_WHEEL_DELTA_WPARAM(wParam);
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-08-31 14:50:12 +00:00
|
|
|
if (delta < 0) {
|
|
|
|
_cursor.wheel++;
|
|
|
|
} else if (delta > 0) {
|
|
|
|
_cursor.wheel--;
|
|
|
|
}
|
2006-11-15 21:01:19 +00:00
|
|
|
HandleMouseEvents();
|
2006-08-31 14:50:12 +00:00
|
|
|
return 0;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2007-06-30 15:02:21 +00:00
|
|
|
case WM_SETFOCUS:
|
|
|
|
_wnd.has_focus = true;
|
2013-08-05 20:37:14 +00:00
|
|
|
SetCompositionPos(hwnd);
|
2007-06-30 15:02:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_KILLFOCUS:
|
|
|
|
_wnd.has_focus = false;
|
|
|
|
break;
|
|
|
|
|
2007-06-17 19:00:45 +00:00
|
|
|
#if !defined(WINCE)
|
2007-06-30 15:02:21 +00:00
|
|
|
case WM_ACTIVATE: {
|
2007-08-09 20:59:36 +00:00
|
|
|
/* Don't do anything if we are closing openttd */
|
|
|
|
if (_exit_game) break;
|
|
|
|
|
2007-06-30 15:02:21 +00:00
|
|
|
bool active = (LOWORD(wParam) != WA_INACTIVE);
|
|
|
|
bool minimized = (HIWORD(wParam) != 0);
|
2007-06-17 19:00:45 +00:00
|
|
|
if (_wnd.fullscreen) {
|
2007-06-30 15:02:21 +00:00
|
|
|
if (active && minimized) {
|
2007-06-17 19:00:45 +00:00
|
|
|
/* Restore the game window */
|
|
|
|
ShowWindow(hwnd, SW_RESTORE);
|
2014-04-28 21:06:51 +00:00
|
|
|
static_cast<VideoDriver_Win32 *>(VideoDriver::GetInstance())->MakeWindow(true);
|
2007-06-30 15:02:21 +00:00
|
|
|
} else if (!active && !minimized) {
|
2007-06-17 19:00:45 +00:00
|
|
|
/* Minimise the window and restore desktop */
|
|
|
|
ShowWindow(hwnd, SW_MINIMIZE);
|
|
|
|
ChangeDisplaySettings(NULL, 0);
|
|
|
|
}
|
|
|
|
}
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-06-30 15:02:21 +00:00
|
|
|
#endif
|
2007-08-04 13:51:41 +00:00
|
|
|
}
|
2007-06-30 15:02:21 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
return DefWindowProc(hwnd, msg, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void RegisterWndClass()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2006-08-31 14:50:12 +00:00
|
|
|
static bool registered = false;
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
if (!registered) {
|
|
|
|
HINSTANCE hinst = GetModuleHandle(NULL);
|
|
|
|
WNDCLASS wnd = {
|
2011-12-10 16:54:46 +00:00
|
|
|
CS_OWNDC,
|
2005-07-23 15:16:57 +00:00
|
|
|
WndProcGdi,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
hinst,
|
|
|
|
LoadIcon(hinst, MAKEINTRESOURCE(100)),
|
|
|
|
LoadCursor(NULL, IDC_ARROW),
|
|
|
|
0,
|
|
|
|
0,
|
2006-11-28 19:58:13 +00:00
|
|
|
_T("OTTD")
|
2005-07-23 15:16:57 +00:00
|
|
|
};
|
2006-08-31 14:50:12 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
registered = true;
|
2008-06-05 20:54:52 +00:00
|
|
|
if (!RegisterClass(&wnd)) usererror("RegisterClass failed");
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-04 21:35:40 +00:00
|
|
|
static bool AllocateDibSection(int w, int h, bool force)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
BITMAPINFO *bi;
|
|
|
|
HDC dc;
|
2014-09-30 21:10:32 +00:00
|
|
|
uint bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2008-04-18 21:49:38 +00:00
|
|
|
w = max(w, 64);
|
|
|
|
h = max(h, 64);
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2008-06-05 20:54:52 +00:00
|
|
|
if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
|
2007-06-12 20:24:12 +00:00
|
|
|
|
2011-10-04 21:35:40 +00:00
|
|
|
if (!force && w == _screen.width && h == _screen.height) return false;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2007-04-18 22:10:36 +00:00
|
|
|
bi = (BITMAPINFO*)alloca(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
|
|
|
|
memset(bi, 0, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
|
2005-07-23 15:16:57 +00:00
|
|
|
bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
|
|
|
|
|
|
bi->bmiHeader.biWidth = _wnd.width = w;
|
2014-01-13 17:54:24 +00:00
|
|
|
bi->bmiHeader.biHeight = -(_wnd.height = h);
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
bi->bmiHeader.biPlanes = 1;
|
2014-01-02 22:41:58 +00:00
|
|
|
bi->bmiHeader.biBitCount = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
|
2005-07-23 15:16:57 +00:00
|
|
|
bi->bmiHeader.biCompression = BI_RGB;
|
|
|
|
|
2006-08-31 14:50:12 +00:00
|
|
|
if (_wnd.dib_sect) DeleteObject(_wnd.dib_sect);
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
dc = GetDC(0);
|
2007-06-12 20:24:12 +00:00
|
|
|
_wnd.dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID**)&_wnd.buffer_bits, NULL, 0);
|
2008-06-05 20:54:52 +00:00
|
|
|
if (_wnd.dib_sect == NULL) usererror("CreateDIBSection failed");
|
2005-07-23 15:16:57 +00:00
|
|
|
ReleaseDC(0, dc);
|
|
|
|
|
2014-02-23 14:15:55 +00:00
|
|
|
_screen.width = w;
|
|
|
|
_screen.pitch = (bpp == 8) ? Align(w, 4) : w;
|
|
|
|
_screen.height = h;
|
|
|
|
_screen.dst_ptr = _wnd.buffer_bits;
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-06-16 19:38:41 +00:00
|
|
|
static const Dimension default_resolutions[] = {
|
2006-08-31 14:50:12 +00:00
|
|
|
{ 640, 480 },
|
|
|
|
{ 800, 600 },
|
|
|
|
{ 1024, 768 },
|
|
|
|
{ 1152, 864 },
|
|
|
|
{ 1280, 800 },
|
|
|
|
{ 1280, 960 },
|
|
|
|
{ 1280, 1024 },
|
|
|
|
{ 1400, 1050 },
|
|
|
|
{ 1600, 1200 },
|
|
|
|
{ 1680, 1050 },
|
|
|
|
{ 1920, 1200 }
|
2005-07-23 15:16:57 +00:00
|
|
|
};
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void FindResolutions()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2006-06-10 08:37:41 +00:00
|
|
|
uint n = 0;
|
2007-01-21 14:32:40 +00:00
|
|
|
#if defined(WINCE)
|
2009-03-15 00:32:18 +00:00
|
|
|
/* EnumDisplaySettingsW is only supported in CE 4.2+
|
|
|
|
* XXX -- One might argue that we assume 4.2+ on every system. Then we can use this function safely */
|
2007-01-21 14:32:40 +00:00
|
|
|
#else
|
2006-06-10 08:37:41 +00:00
|
|
|
uint i;
|
2006-11-28 20:01:46 +00:00
|
|
|
DEVMODEA dm;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2014-04-27 12:15:14 +00:00
|
|
|
/* Check modes for the relevant fullscreen bpp */
|
2014-09-30 21:10:32 +00:00
|
|
|
uint bpp = _support8bpp != S8BPP_HARDWARE ? 32 : BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
|
2014-04-27 12:15:14 +00:00
|
|
|
|
2006-11-28 20:01:46 +00:00
|
|
|
/* XXX - EnumDisplaySettingsW crashes with unicows.dll on Windows95
|
|
|
|
* Doesn't really matter since we don't pass a string anyways, but still
|
|
|
|
* a letdown */
|
|
|
|
for (i = 0; EnumDisplaySettingsA(NULL, i, &dm) != 0; i++) {
|
2014-04-27 12:15:14 +00:00
|
|
|
if (dm.dmBitsPerPel == bpp &&
|
2008-04-18 21:49:38 +00:00
|
|
|
dm.dmPelsWidth >= 640 && dm.dmPelsHeight >= 480) {
|
2006-06-10 08:37:41 +00:00
|
|
|
uint j;
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
for (j = 0; j < n; j++) {
|
2009-06-27 20:53:45 +00:00
|
|
|
if (_resolutions[j].width == dm.dmPelsWidth && _resolutions[j].height == dm.dmPelsHeight) break;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* In the previous loop we have checked already existing/added resolutions if
|
|
|
|
* they are the same as the new ones. If this is not the case (j == n); we have
|
|
|
|
* looped all and found none, add the new one to the list. If we have reached the
|
|
|
|
* maximum amount of resolutions, then quit querying the display */
|
|
|
|
if (j == n) {
|
2008-06-16 19:38:41 +00:00
|
|
|
_resolutions[j].width = dm.dmPelsWidth;
|
|
|
|
_resolutions[j].height = dm.dmPelsHeight;
|
2005-07-23 15:16:57 +00:00
|
|
|
if (++n == lengthof(_resolutions)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-21 14:32:40 +00:00
|
|
|
#endif
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
/* We have found no resolutions, show the default list */
|
|
|
|
if (n == 0) {
|
|
|
|
memcpy(_resolutions, default_resolutions, sizeof(default_resolutions));
|
|
|
|
n = lengthof(default_resolutions);
|
|
|
|
}
|
|
|
|
|
|
|
|
_num_resolutions = n;
|
|
|
|
SortResolutions(_num_resolutions);
|
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
static FVideoDriver_Win32 iFVideoDriver_Win32;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
const char *VideoDriver_Win32::Start(const char * const *parm)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
memset(&_wnd, 0, sizeof(_wnd));
|
|
|
|
|
|
|
|
RegisterWndClass();
|
|
|
|
|
|
|
|
MakePalette();
|
|
|
|
|
|
|
|
FindResolutions();
|
|
|
|
|
2009-06-27 20:53:45 +00:00
|
|
|
DEBUG(driver, 2, "Resolution for display: %ux%u", _cur_resolution.width, _cur_resolution.height);
|
2007-08-04 12:53:41 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* fullscreen uses those */
|
2008-06-16 19:38:41 +00:00
|
|
|
_wnd.width_org = _cur_resolution.width;
|
|
|
|
_wnd.height_org = _cur_resolution.height;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2008-06-16 19:38:41 +00:00
|
|
|
AllocateDibSection(_cur_resolution.width, _cur_resolution.height);
|
2011-02-26 19:13:58 +00:00
|
|
|
this->MakeWindow(_fullscreen);
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2008-04-18 21:49:38 +00:00
|
|
|
MarkWholeScreenDirty();
|
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
_draw_threaded = GetDriverParam(parm, "no_threads") == NULL && GetDriverParam(parm, "no_thread") == NULL && GetCPUCoreCount() > 1;
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
void VideoDriver_Win32::Stop()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2006-03-21 21:55:49 +00:00
|
|
|
DeleteObject(_wnd.gdi_palette);
|
|
|
|
DeleteObject(_wnd.dib_sect);
|
2007-08-09 05:34:56 +00:00
|
|
|
DestroyWindow(_wnd.main_wnd);
|
2006-03-21 21:55:49 +00:00
|
|
|
|
2007-01-21 14:32:40 +00:00
|
|
|
#if !defined(WINCE)
|
2005-07-23 15:16:57 +00:00
|
|
|
if (_wnd.fullscreen) ChangeDisplaySettings(NULL, 0);
|
2007-01-21 14:32:40 +00:00
|
|
|
#endif
|
2005-07-23 15:16:57 +00:00
|
|
|
MyShowCursor(true);
|
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
void VideoDriver_Win32::MakeDirty(int left, int top, int width, int height)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
RECT r = { left, top, left + width, top + height };
|
|
|
|
|
|
|
|
InvalidateRect(_wnd.main_wnd, &r, FALSE);
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void CheckPaletteAnim()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2011-12-08 18:13:29 +00:00
|
|
|
if (_cur_palette.count_dirty == 0) return;
|
2010-07-24 10:14:39 +00:00
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
_local_palette = _cur_palette;
|
2005-07-23 15:16:57 +00:00
|
|
|
InvalidateRect(_wnd.main_wnd, NULL, FALSE);
|
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
void VideoDriver_Win32::MainLoop()
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
|
|
|
MSG mesg;
|
2007-01-10 15:00:20 +00:00
|
|
|
uint32 cur_ticks = GetTickCount();
|
2007-06-22 20:04:21 +00:00
|
|
|
uint32 last_cur_ticks = cur_ticks;
|
2010-11-19 10:35:59 +00:00
|
|
|
uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
if (_draw_threaded) {
|
|
|
|
/* Initialise the mutex first, because that's the thing we *need*
|
|
|
|
* directly in the newly created thread. */
|
|
|
|
_draw_mutex = ThreadMutex::New();
|
2014-02-23 16:08:50 +00:00
|
|
|
_draw_thread_initialized = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
if (_draw_mutex == NULL || _draw_thread_initialized == NULL) {
|
2011-12-10 16:54:46 +00:00
|
|
|
_draw_threaded = false;
|
|
|
|
} else {
|
|
|
|
_draw_continue = true;
|
|
|
|
_draw_threaded = ThreadObject::New(&PaintWindowThread, NULL, &_draw_thread);
|
|
|
|
|
|
|
|
/* Free the mutex if we won't be able to use it. */
|
|
|
|
if (!_draw_threaded) {
|
|
|
|
delete _draw_mutex;
|
|
|
|
_draw_mutex = NULL;
|
2014-02-23 16:08:50 +00:00
|
|
|
CloseHandle(_draw_thread_initialized);
|
|
|
|
_draw_thread_initialized = NULL;
|
2011-12-10 16:54:46 +00:00
|
|
|
} else {
|
|
|
|
DEBUG(driver, 1, "Threaded drawing enabled");
|
2014-02-23 16:08:50 +00:00
|
|
|
/* Wait till the draw thread has started itself. */
|
|
|
|
WaitForSingleObject(_draw_thread_initialized, INFINITE);
|
|
|
|
_draw_mutex->BeginCritical();
|
2011-12-10 16:54:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
_wnd.running = true;
|
|
|
|
|
2011-12-08 19:37:33 +00:00
|
|
|
CheckPaletteAnim();
|
2006-02-01 07:36:15 +00:00
|
|
|
for (;;) {
|
2007-01-10 15:00:20 +00:00
|
|
|
uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
while (PeekMessage(&mesg, NULL, 0, 0, PM_REMOVE)) {
|
|
|
|
InteractiveRandom(); // randomness
|
2013-08-05 20:36:20 +00:00
|
|
|
/* Convert key messages to char messages if we want text input. */
|
|
|
|
if (EditBoxInGlobalFocus()) TranslateMessage(&mesg);
|
2005-07-23 15:16:57 +00:00
|
|
|
DispatchMessage(&mesg);
|
|
|
|
}
|
2005-07-29 16:40:29 +00:00
|
|
|
if (_exit_game) return;
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
#if defined(_DEBUG)
|
2006-11-15 19:49:16 +00:00
|
|
|
if (_wnd.has_focus && GetAsyncKeyState(VK_SHIFT) < 0 &&
|
2005-07-23 15:16:57 +00:00
|
|
|
#else
|
2006-11-15 19:49:16 +00:00
|
|
|
/* Speed up using TAB, but disable for ALT+TAB of course */
|
|
|
|
if (_wnd.has_focus && GetAsyncKeyState(VK_TAB) < 0 && GetAsyncKeyState(VK_MENU) >= 0 &&
|
2005-07-23 15:16:57 +00:00
|
|
|
#endif
|
2006-11-15 19:49:16 +00:00
|
|
|
!_networking && _game_mode != GM_MENU) {
|
|
|
|
_fast_forward |= 2;
|
2006-06-27 21:25:53 +00:00
|
|
|
} else if (_fast_forward & 2) {
|
2005-07-23 15:16:57 +00:00
|
|
|
_fast_forward = 0;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
cur_ticks = GetTickCount();
|
2009-05-06 15:06:57 +00:00
|
|
|
if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
|
2007-06-22 20:07:39 +00:00
|
|
|
_realtime_tick += cur_ticks - last_cur_ticks;
|
|
|
|
last_cur_ticks = cur_ticks;
|
2010-11-19 10:35:59 +00:00
|
|
|
next_tick = cur_ticks + MILLISECONDS_PER_TICK;
|
2008-02-17 17:00:43 +00:00
|
|
|
|
|
|
|
bool old_ctrl_pressed = _ctrl_pressed;
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
_ctrl_pressed = _wnd.has_focus && GetAsyncKeyState(VK_CONTROL)<0;
|
|
|
|
_shift_pressed = _wnd.has_focus && GetAsyncKeyState(VK_SHIFT)<0;
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* determine which directional keys are down */
|
2005-07-23 15:16:57 +00:00
|
|
|
if (_wnd.has_focus) {
|
|
|
|
_dirkeys =
|
|
|
|
(GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
|
|
|
|
(GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
|
|
|
|
(GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
|
|
|
|
(GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
2005-07-23 15:16:57 +00:00
|
|
|
_dirkeys = 0;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2008-02-17 17:00:43 +00:00
|
|
|
if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
|
|
|
|
|
2011-12-10 16:54:46 +00:00
|
|
|
#if !defined(WINCE)
|
|
|
|
/* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
|
|
|
|
GdiFlush();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The game loop is the part that can run asynchronously.
|
|
|
|
* The rest except sleeping can't. */
|
|
|
|
if (_draw_threaded) _draw_mutex->EndCritical();
|
2005-07-23 15:16:57 +00:00
|
|
|
GameLoop();
|
2011-12-10 16:54:46 +00:00
|
|
|
if (_draw_threaded) _draw_mutex->BeginCritical();
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
if (_force_full_redraw) MarkWholeScreenDirty();
|
2005-07-23 15:16:57 +00:00
|
|
|
|
|
|
|
UpdateWindows();
|
|
|
|
CheckPaletteAnim();
|
|
|
|
} else {
|
2007-01-21 14:32:40 +00:00
|
|
|
#if !defined(WINCE)
|
2011-12-10 16:54:46 +00:00
|
|
|
/* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
|
2005-07-23 15:16:57 +00:00
|
|
|
GdiFlush();
|
2007-01-21 14:32:40 +00:00
|
|
|
#endif
|
2011-12-10 16:54:46 +00:00
|
|
|
|
|
|
|
/* Release the thread while sleeping */
|
|
|
|
if (_draw_threaded) _draw_mutex->EndCritical();
|
|
|
|
Sleep(1);
|
|
|
|
if (_draw_threaded) _draw_mutex->BeginCritical();
|
|
|
|
|
2008-08-11 22:45:11 +00:00
|
|
|
NetworkDrawChatMessage();
|
2005-07-23 15:16:57 +00:00
|
|
|
DrawMouseCursor();
|
|
|
|
}
|
|
|
|
}
|
2011-12-10 16:54:46 +00:00
|
|
|
|
|
|
|
if (_draw_threaded) {
|
|
|
|
_draw_continue = false;
|
|
|
|
/* Sending signal if there is no thread blocked
|
|
|
|
* is very valid and results in noop */
|
|
|
|
_draw_mutex->SendSignal();
|
|
|
|
_draw_mutex->EndCritical();
|
|
|
|
_draw_thread->Join();
|
|
|
|
|
2014-02-23 16:08:50 +00:00
|
|
|
CloseHandle(_draw_thread_initialized);
|
2011-12-10 16:54:46 +00:00
|
|
|
delete _draw_mutex;
|
|
|
|
delete _draw_thread;
|
|
|
|
}
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
bool VideoDriver_Win32::ChangeResolution(int w, int h)
|
2005-07-23 15:16:57 +00:00
|
|
|
{
|
2014-02-23 14:16:36 +00:00
|
|
|
if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
|
2012-07-10 17:47:03 +00:00
|
|
|
if (_window_maximize) ShowWindow(_wnd.main_wnd, SW_SHOWNORMAL);
|
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
_wnd.width = _wnd.width_org = w;
|
|
|
|
_wnd.height = _wnd.height_org = h;
|
|
|
|
|
2014-02-23 14:16:36 +00:00
|
|
|
bool ret = this->MakeWindow(_fullscreen); // _wnd.fullscreen screws up ingame resolution switching
|
|
|
|
if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
|
|
|
|
return ret;
|
2005-07-23 15:16:57 +00:00
|
|
|
}
|
|
|
|
|
2008-01-01 14:20:48 +00:00
|
|
|
bool VideoDriver_Win32::ToggleFullscreen(bool full_screen)
|
2006-08-31 14:50:12 +00:00
|
|
|
{
|
2014-02-23 14:16:36 +00:00
|
|
|
if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
|
|
|
|
bool ret = this->MakeWindow(full_screen);
|
|
|
|
if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
|
|
|
|
return ret;
|
2006-08-31 14:50:12 +00:00
|
|
|
}
|
2011-10-04 21:35:40 +00:00
|
|
|
|
|
|
|
bool VideoDriver_Win32::AfterBlitterChange()
|
|
|
|
{
|
2014-02-23 14:16:36 +00:00
|
|
|
if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
|
|
|
|
bool ret = AllocateDibSection(_screen.width, _screen.height, true) && this->MakeWindow(_fullscreen);
|
|
|
|
if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
|
|
|
|
return ret;
|
2011-10-04 21:35:40 +00:00
|
|
|
}
|
2013-08-05 20:37:11 +00:00
|
|
|
|
|
|
|
void VideoDriver_Win32::EditBoxLostFocus()
|
|
|
|
{
|
2014-02-23 14:16:36 +00:00
|
|
|
if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
|
2013-08-05 20:37:11 +00:00
|
|
|
CancelIMEComposition(_wnd.main_wnd);
|
2013-08-05 20:37:14 +00:00
|
|
|
SetCompositionPos(_wnd.main_wnd);
|
2013-08-05 20:37:18 +00:00
|
|
|
SetCandidatePos(_wnd.main_wnd);
|
2014-02-23 14:16:36 +00:00
|
|
|
if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
|
2013-08-05 20:37:11 +00:00
|
|
|
}
|