File: window.cpp

package info (click to toggle)
glest 3.2.2-2
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 2,800 kB
  • ctags: 6,581
  • sloc: cpp: 32,575; sh: 8,341; makefile: 63
file content (303 lines) | stat: -rw-r--r-- 6,513 bytes parent folder | download
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
//This file is part of Glest Shared Library (www.glest.org)
//Copyright (C) 2005 Matthias Braun <matze@braunis.de>

//You can redistribute this code 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.

#include "window.h"

#include <iostream>
#include <stdexcept>
#include <cassert>
#include <cctype>

#include "conversion.h"
#include "platform_util.h"
#include "leak_dumper.h"
#include "sdl_private.h"
#include "noimpl.h"

using namespace Shared::Util;
using namespace std;

namespace Shared{ namespace Platform{

// =======================================
//               WINDOW               
// =======================================

// ========== STATIC INICIALIZATIONS ==========

// Matze: hack for now...
static Window* global_window = 0;

// ========== PUBLIC ==========

Window::Window() {
	memset(lastMouseDown, 0, sizeof(lastMouseDown));
	
	assert(global_window == 0);
	global_window = this;
}

Window::~Window() {
	assert(global_window == this);
	global_window = 0;
}

bool Window::handleEvent() {
	SDL_Event event;
	while(SDL_PollEvent(&event)) {
		try {
			switch(event.type) {
				case SDL_QUIT:
					return false;
				case SDL_MOUSEBUTTONDOWN:
					global_window->handleMouseDown(event);
					break;
				case SDL_MOUSEBUTTONUP: {
					global_window->eventMouseUp(event.button.x,
							event.button.y,getMouseButton(event.button.button));
					break;
				}
				case SDL_MOUSEMOTION: {
					MouseState ms;
					ms.leftMouse = (event.motion.state & SDL_BUTTON_LMASK) != 0;
					ms.rightMouse = (event.motion.state & SDL_BUTTON_RMASK) != 0;
					ms.centerMouse = (event.motion.state & SDL_BUTTON_MMASK) != 0;
					global_window->eventMouseMove(event.motion.x, event.motion.y, &ms);
					break;
				}
				case SDL_KEYDOWN:
					/* handle ALT+Return */
					if(event.key.keysym.sym == SDLK_RETURN 
							&& (event.key.keysym.mod & (KMOD_LALT | KMOD_RALT))) {
						toggleFullscreen();
					}
					global_window->eventKeyDown(getKey(event.key.keysym));
					global_window->eventKeyPress(static_cast<char>(event.key.keysym.unicode));
					break;
				case SDL_KEYUP:
					global_window->eventKeyUp(getKey(event.key.keysym));
					break;
			}
		} catch(std::exception& e) {
			std::cerr << "Couldn't process event: " << e.what() << "\n";
		}
	}

	return true;
}

string Window::getText() {
	char* c = 0;
	SDL_WM_GetCaption(&c, 0);

	return string(c);
}

float Window::getAspect() {
	return static_cast<float>(getClientH())/getClientW();
}

void Window::setText(string text) {
	SDL_WM_SetCaption(text.c_str(), 0);
}

void Window::setSize(int w, int h) {
	this->w = w;
	this->h = h;
	Private::ScreenWidth = w;
	Private::ScreenHeight = h;
}

void Window::setPos(int x, int y)  {
	if(x != 0 || y != 0) {
		NOIMPL;
		return;
	}
}

void Window::minimize() {
	NOIMPL;
}

void Window::setEnabled(bool enabled) {
	NOIMPL;
}

void Window::setVisible(bool visible) {
	 NOIMPL;
}

void Window::setStyle(WindowStyle windowStyle) {
	if(windowStyle == wsFullscreen)
		return;
	// NOIMPL;
}

void Window::create() {
	// nothing here
}

void Window::destroy() {
	SDL_Event event;
	event.type = SDL_QUIT;
	SDL_PushEvent(&event);
}

void Window::toggleFullscreen() {
	SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
}

void Window::handleMouseDown(SDL_Event event) {
	static const Uint32 DOUBLECLICKTIME = 500;
	static const int DOUBLECLICKDELTA = 5;
		
	MouseButton button = getMouseButton(event.button.button);
	Uint32 ticks = SDL_GetTicks();
	int n = (int) button;
	if(ticks - lastMouseDown[n] < DOUBLECLICKTIME
			&& abs(lastMouseX[n] - event.button.x) < DOUBLECLICKDELTA
			&& abs(lastMouseY[n] - event.button.y) < DOUBLECLICKDELTA) {
		eventMouseDown(event.button.x, event.button.y, button);
		eventMouseDoubleClick(event.button.x, event.button.y, button);
	} else {
		eventMouseDown(event.button.x, event.button.y, button);
	}
	lastMouseDown[n] = ticks;
	lastMouseX[n] = event.button.x;
	lastMouseY[n] = event.button.y;
}

MouseButton Window::getMouseButton(int sdlButton) {
	switch(sdlButton) {
		case SDL_BUTTON_LEFT:
			return mbLeft;
		case SDL_BUTTON_RIGHT:
			return mbRight;
		case SDL_BUTTON_MIDDLE:
			return mbCenter;
		default:
			throw std::runtime_error("Mouse Button > 3 not handled.");
	}
}

char Window::getKey(SDL_keysym keysym) {
	switch(keysym.sym) {
		case SDLK_PLUS:
		case SDLK_KP_PLUS:
			return vkAdd;
		case SDLK_MINUS:
		case SDLK_KP_MINUS:
			return vkSubtract;
		case SDLK_LALT:
		case SDLK_RALT:
			return vkAlt;
		case SDLK_LCTRL:
		case SDLK_RCTRL:
			return vkControl;
		case SDLK_LSHIFT:
		case SDLK_RSHIFT:
			return vkShift;
		case SDLK_ESCAPE:
			return vkEscape;
		case SDLK_UP:
			return vkUp;
		case SDLK_LEFT:
			return vkLeft;
		case SDLK_RIGHT:
			return vkRight;
		case SDLK_DOWN:
			return vkDown;
		case SDLK_RETURN:
		case SDLK_KP_ENTER:
			return vkReturn;
		case SDLK_BACKSPACE:
			return vkBack;
		case SDLK_0:
			return '0';
		case SDLK_1:
			return '1';
		case SDLK_2:
			return '2';
		case SDLK_3:
			return '3';
		case SDLK_4:
			return '4';
		case SDLK_5:
			return '5';
		case SDLK_6:
			return '6';
		case SDLK_7:
			return '7';
		case SDLK_8:
			return '8';
		case SDLK_9:
			return '9';
		case SDLK_a:
			return 'A';
		case SDLK_b:
			return 'B';
		case SDLK_c:
			return 'C';
		case SDLK_d:
			return 'D';
		case SDLK_e:
			return 'E';
		case SDLK_f:
			return 'F';
		case SDLK_g:
			return 'G';
		case SDLK_h:
			return 'H';
		case SDLK_i:
			return 'I';
		case SDLK_j:
			return 'J';
		case SDLK_k:
			return 'K';
		case SDLK_l:
			return 'L';
		case SDLK_m:
			return 'M';
		case SDLK_n:
			return 'N';
		case SDLK_o:
			return 'O';
		case SDLK_p:
			return 'P';
		case SDLK_q:
			return 'Q';
		case SDLK_r:
			return 'R';
		case SDLK_s:
			return 'S';
		case SDLK_t:
			return 'T';
		case SDLK_u:
			return 'U';
		case SDLK_v:
			return 'V';
		case SDLK_w:
			return 'W';
		case SDLK_x:
			return 'X';
		case SDLK_y:
			return 'Y';
		case SDLK_z:
			return 'Z'; 
		default:
			Uint16 c = keysym.unicode;
			if((c & 0xFF80) == 0) {
				return toupper(c);
			}
			break;
	}

	return 0;
}

}}//end namespace