aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/kernel/win32window.cpp
blob: d90884147bc3b7fef7912335f9e6bcbb250e9a9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// -----------------------------------------------------------------------------
// This file is part of Broken Sword 2.5
// Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsd�rfer
//
// Broken Sword 2.5 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; either version 2 of the License, or
// (at your option) any later version.
//
// Broken Sword 2.5 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 Broken Sword 2.5; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------

#include "sword25/kernel/win32window.h"
#include "../../projects/resource.h"

#include "sword25/kernel/kernel.h"
#include "sword25/input/inputengine.h"

bool BS_Win32Window::_ClassRegistered = false;

#define BS_LOG_PREFIX "WIN32WINDOW"


// Konstanten
// ----------
static const UINT WINDOW_STYLE			= WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
static const UINT WINDOW_STYLE_EX		= 0;
static const UINT WINDOW_MAX_MESSAGES	= 50;

// Konstruktion/Destruktion
// ------------------------
BS_Win32Window::BS_Win32Window(int X, int Y, int Width, int Height, bool Visible)
{
	const char WINDOW_CLASS[] = "BSEngine-Class";

	// Von negativen Fall ausgehen
	_InitSuccess = false;

	// Fensterklasse registrieren falls n�tig
	if (!_ClassRegistered)
	{
		//Fensterklasse
		WNDCLASSEX wndclass;
		
		//Werte der Fensterklasse festlegen
		ZeroMemory(&wndclass, sizeof(WNDCLASSEX));
		wndclass.cbSize = sizeof(WNDCLASSEX);
		wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
		wndclass.lpfnWndProc = BS_Win32Window::WindowProc;
		wndclass.hInstance = GetModuleHandle(NULL);
		wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
		wndclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
		wndclass.hCursor = NULL;
		wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
		wndclass.lpszClassName = WINDOW_CLASS;
		
		//Fensterklasse registrieren
		if (!RegisterClassEx(&wndclass)) return;

		_ClassRegistered = true;
	}

	//Fenster erstellen
	if (!(_Window=CreateWindowEx(
		WINDOW_STYLE_EX,					// Erweiterte Darstellungsflags
		WINDOW_CLASS,						// Registrierter Fenstername
		"",									// Kein Fenstertitel
		WINDOW_STYLE,						// Darstellungsflags
		0,0,								// Default-Position
		0,0,								// Default-Gr�sse
		NULL,								// Kein Parent-Fenster
		NULL,								// Kein Men�
		GetModuleHandle(NULL),				// Instance-Handle
		NULL)))
		return;

	// Fensterposition und Fenstergr��e setzen
	SetWidth(Width);
	SetHeight(Height);
	SetX(X);
	SetY(Y);

	// Fenstersichtbarkeit setzen
	SetVisible(Visible);

	// Icon setzen
	HICON hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON1));
	if (hIcon)
	{
		SendMessage(_Window, WM_SETICON, ICON_BIG, (LPARAM) hIcon);
		SendMessage(_Window, WM_SETICON, ICON_SMALL, (LPARAM) hIcon);
	}

	// Erfolg signalisieren
	_InitSuccess = true;
	_WindowAlive = true;
	_CloseWanted = false;
}

BS_Win32Window::~BS_Win32Window()
{
	// Fenster zerst�ren, falls dies nicht ohnehin schon passiert ist
	if (_WindowAlive) DestroyWindow(_Window);
}

// Get-Methoden
// ------------
int BS_Win32Window::GetX()
{
	RECT Rect;
	GetWindowRect(_Window, &Rect);
	return Rect.left;
}

int BS_Win32Window::GetY()
{
	RECT Rect;
	GetWindowRect(_Window, &Rect);
	return Rect.top;
}

int BS_Win32Window::GetClientX()
{
	POINT Point = {0, 0};
	ClientToScreen(_Window, &Point);
	return Point.x;
}

int BS_Win32Window::GetClientY()
{
	POINT Point = {0, 0};
	ClientToScreen(_Window, &Point);
	return Point.y;
}

int BS_Win32Window::GetWidth()
{
	RECT Rect;
	GetClientRect(_Window, &Rect);
	return Rect.right - Rect.left;
}

int BS_Win32Window::GetHeight()
{
	RECT Rect;
	GetClientRect(_Window, &Rect);
	return Rect.bottom - Rect.top;
}

std::string BS_Win32Window::GetTitle()
{
	char String[512];
	if (GetWindowText(_Window, String, sizeof(String)))
		return std::string(String);

	return std::string("");
}

bool BS_Win32Window::IsVisible()
{
	return IsWindowVisible(_Window) ? true : false;
}

bool BS_Win32Window::HasFocus()
{
	return GetForegroundWindow() == _Window ? true : false;
}

UINT BS_Win32Window::GetWindowHandle()
{
	return (UINT)_Window;
}

// Set Methoden
// ------------

void BS_Win32Window::SetX(int X)
{
	int RealX;
	if (X == -1)
	{
		RECT Rect;
		GetWindowRect(_Window, &Rect);
		RealX = (GetSystemMetrics(SM_CXSCREEN) - (Rect.right - Rect.left)) / 2;
	}
	else
		RealX = X;

	SetWindowPos(_Window, NULL, RealX, GetY(), 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}

void BS_Win32Window::SetY(int Y)
{
	int RealY;
	if (Y == -1)
	{
		RECT Rect;
		GetWindowRect(_Window, &Rect);
		RealY = (GetSystemMetrics(SM_CYSCREEN) - (Rect.bottom - Rect.top)) / 2;
	}
	else
		RealY = Y;
	
	SetWindowPos(_Window, NULL, GetX(), RealY, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}

void BS_Win32Window::SetWidth(int Width)
{
	RECT Rect = {0, 0, Width, GetHeight()};
	AdjustWindowRectEx(&Rect, WINDOW_STYLE, false, WINDOW_STYLE_EX);
	SetWindowPos(_Window, NULL, 0, 0, Rect.right - Rect.left, Rect.bottom - Rect.top, SWP_NOMOVE | SWP_NOZORDER);
}

void BS_Win32Window::SetHeight(int Height)
{
	RECT Rect = {0, 0, GetWidth(), Height};
	AdjustWindowRectEx(&Rect, WINDOW_STYLE, false, WINDOW_STYLE_EX);
	SetWindowPos(_Window, NULL, 0, 0, Rect.right - Rect.left, Rect.bottom - Rect.top, SWP_NOMOVE | SWP_NOZORDER);	
}

void BS_Win32Window::SetVisible(bool Visible)
{
	ShowWindow(_Window, Visible ? SW_SHOW : SW_HIDE);
}

void BS_Win32Window::SetTitle(std::string Title)
{
	SetWindowText(_Window, Title.c_str());
}

// Asynchroner Message-Loop
bool BS_Win32Window::ProcessMessages()
{
	for (UINT i = 0; i < WINDOW_MAX_MESSAGES; i++)
	{
		MSG msg;
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
			{
				_WindowAlive = false;
				return false;
			}

			// Alle Nachrichten zur Verarbeitung durch WindowProc vorbereiten
			TranslateMessage(&msg);
			// Nachricht an WindowProc �bergeben
			DispatchMessage(&msg);
		}
		else
			return true;
	}

	return true;
}

// Synchroner Message-Loop
bool BS_Win32Window::WaitForFocus()
{
	MSG msg;
	
	// Fenster minimieren
	ShowWindow(_Window, SW_MINIMIZE);

	for (;;)
	{
		// Auf Nachricht warten
		WaitMessage();
		// Nachricht einlesen
		GetMessage(&msg, NULL, 0, 0);
		// Nachricht zur Verarbeitung durch WindowProc vorbereiten
		TranslateMessage(&msg);
		// Nachricht an WindowProc �bergeben
		DispatchMessage(&msg);

		// �berpr�fen ob das Fenster geschlossen wurde
		if (msg.message == WM_QUIT)
		{
			_WindowAlive = false;
			return false;
		}

		// �berpr�fen, ob das Fenster den Focus wiedererlangt hat
		if (HasFocus()) return true;
	}
}

// Die WindowProc aller Fenster der Klasse
LRESULT CALLBACK BS_Win32Window::WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_PAINT:
		ValidateRect(hwnd, NULL);
		break;
		
	case WM_DESTROY:
		// Das Fenster wird zerst�rt
		PostQuitMessage(0);
		break;

	case WM_CLOSE:
		{
			BS_Window * WindowPtr = BS_Kernel::GetInstance()->GetWindow();
			if (WindowPtr) {
				WindowPtr->SetCloseWanted(true);
			}
			break;
		}

	case WM_KEYDOWN:
		{
			// Tastendr�cke, die f�r das Inputmodul interessant sind, werden diesem gemeldet.
			BS_InputEngine * InputPtr = BS_Kernel::GetInstance()->GetInput();

			if (InputPtr)
			{
				switch (wParam)
				{
				case VK_RETURN:
					InputPtr->ReportCommand(BS_InputEngine::KEY_COMMAND_ENTER);
					break;

				case VK_LEFT:
					InputPtr->ReportCommand(BS_InputEngine::KEY_COMMAND_LEFT);
					break;

				case VK_RIGHT:
					InputPtr->ReportCommand(BS_InputEngine::KEY_COMMAND_RIGHT);
					break;

				case VK_HOME:
					InputPtr->ReportCommand(BS_InputEngine::KEY_COMMAND_HOME);
					break;

				case VK_END:
					InputPtr->ReportCommand(BS_InputEngine::KEY_COMMAND_END);
					break;

				case VK_BACK:
					InputPtr->ReportCommand(BS_InputEngine::KEY_COMMAND_BACKSPACE);
					break;

				case VK_TAB:
					InputPtr->ReportCommand(BS_InputEngine::KEY_COMMAND_TAB);
					break;

				case VK_INSERT:
					InputPtr->ReportCommand(BS_InputEngine::KEY_COMMAND_INSERT);
					break;

				case VK_DELETE:
					InputPtr->ReportCommand(BS_InputEngine::KEY_COMMAND_DELETE);
					break;
				}
			}
			break;
		}

	case WM_KEYUP:
	case WM_SYSKEYUP:
		// Alle Tastendr�cke werden ignoriert, damit Windows per DefWindowProc() nicht darauf
		// reagieren kann und damit unerwartete Seiteneffekte ausl�st.
		// Zum Beispiel w�rden ALT und F10 Tastendr�cke das "Men�" aktivieren und somit den Message-Loop zum Stillstand bringen.
		break;

	case WM_SYSCOMMAND:
		// Verhindern, dass der Bildschirmschoner aktiviert wird, w�hrend das Spiel l�uft
		if (wParam != SC_SCREENSAVE) return DefWindowProc(hwnd,uMsg,wParam,lParam);
		break;

	case WM_CHAR:
		{
			unsigned char theChar = static_cast<unsigned char>(wParam & 0xff);

			// Alle Zeichen, die keine Steuerzeichen sind, werden als Buchstaben dem Input-Service mitgeteilt.
			if (theChar >= 32)
			{
				BS_InputEngine * InputPtr = BS_Kernel::GetInstance()->GetInput();
				if (InputPtr) InputPtr->ReportCharacter(theChar);
			}
		}
		break;

	case WM_SETCURSOR:
		{
			// Der Systemcursor wird in der Client-Area des Fensters nicht angezeigt, jedoch in der nicht Client-Area, damit der Benutzer das Fenster wie gewohnt
			// schlie�en und verschieben kann.

			// Koordinaten des Cursors in der Client-Area berechnen.
			POINT pt;
			GetCursorPos(&pt);
			ScreenToClient(hwnd, &pt);

			// Feststellen, ob sich der Cursor in der Client-Area befindet.
			// Get client rect
			RECT rc;
			GetClientRect(hwnd, &rc);

			// See if cursor is in client area
			if(PtInRect(&rc, pt))
				// In der Client-Area keinen Cursor anzeigen.
				SetCursor(NULL);
			else
				// Ausserhalb der Client-Area den Cursor anzeigen.
				SetCursor(LoadCursor(NULL, IDC_ARROW));

			return TRUE;
		}
		break;

	default:
		// Um alle anderen Vorkommnisse k�mmert sich Windows
		return DefWindowProc(hwnd,uMsg,wParam,lParam);
	}

	return 0;
}