Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
388 lines (316 loc) · 8.95 KB

SDL_BApp.h

File metadata and controls

388 lines (316 loc) · 8.95 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef SDL_BAPP_H
#define SDL_BAPP_H
Jul 22, 2011
Jul 22, 2011
24
#include <InterfaceKit.h>
Jul 30, 2011
Jul 30, 2011
25
#include <OpenGLKit.h>
Jul 22, 2011
Jul 22, 2011
26
Jul 26, 2011
Jul 26, 2011
27
28
29
#include "../../video/bwindow/SDL_bkeyboard.h"
30
31
32
33
34
#ifdef __cplusplus
extern "C" {
#endif
#include "SDL_config.h"
Jul 22, 2011
Jul 22, 2011
35
36
37
38
39
#include "SDL_video.h"
/* Local includes */
#include "../../events/SDL_events_c.h"
Jul 26, 2011
Jul 26, 2011
40
#include "../../video/bwindow/SDL_bkeyboard.h"
Jul 30, 2011
Jul 30, 2011
41
#include "../../video/bwindow/SDL_bframebuffer.h"
42
43
44
45
46
#ifdef __cplusplus
}
#endif
Jul 30, 2011
Jul 30, 2011
47
48
#include <vector>
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
/* Forward declarations */
class SDL_BWin;
/* Message constants */
enum ToSDL {
/* Intercepted by BWindow on its way to BView */
BAPP_MOUSE_MOVED,
BAPP_MOUSE_BUTTON,
BAPP_MOUSE_WHEEL,
BAPP_KEY,
BAPP_REPAINT, /* from _UPDATE_ */
/* From BWindow */
BAPP_MAXIMIZE, /* from B_ZOOM */
BAPP_MINIMIZE,
BAPP_RESTORE, /* TODO: IMPLEMENT! */
BAPP_SHOW,
BAPP_HIDE,
BAPP_MOUSE_FOCUS, /* caused by MOUSE_MOVE */
BAPP_KEYBOARD_FOCUS, /* from WINDOW_ACTIVATED */
BAPP_WINDOW_CLOSE_REQUESTED,
BAPP_WINDOW_MOVED,
BAPP_WINDOW_RESIZED,
BAPP_SCREEN_CHANGED
};
/* Create a descendant of BApplication */
class SDL_BApp : public BApplication {
public:
SDL_BApp(const char* signature) :
BApplication(signature) {
Jul 30, 2011
Jul 30, 2011
84
_current_context = NULL;
Jul 13, 2011
Jul 13, 2011
85
}
Jul 30, 2011
Jul 30, 2011
86
87
Jul 13, 2011
Jul 13, 2011
88
virtual ~SDL_BApp() {
Jul 30, 2011
Jul 30, 2011
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
/* Event-handling functions */
virtual void MessageReceived(BMessage* message) {
/* Sort out SDL-related messages */
switch ( message->what ) {
case BAPP_MOUSE_MOVED:
_HandleMouseMove(message);
break;
case BAPP_MOUSE_BUTTON:
_HandleMouseButton(message);
break;
case BAPP_MOUSE_WHEEL:
_HandleMouseWheel(message);
break;
case BAPP_KEY:
_HandleKey(message);
break;
case BAPP_REPAINT:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_EXPOSED);
break;
case BAPP_MAXIMIZE:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_MAXIMIZED);
break;
case BAPP_MINIMIZE:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_MINIMIZED);
break;
case BAPP_SHOW:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_SHOWN);
break;
case BAPP_HIDE:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_HIDDEN);
break;
case BAPP_MOUSE_FOCUS:
_HandleMouseFocus(message);
break;
case BAPP_KEYBOARD_FOCUS:
_HandleKeyboardFocus(message);
break;
case BAPP_WINDOW_CLOSE_REQUESTED:
_HandleBasicWindowEvent(message, SDL_WINDOWEVENT_CLOSE);
break;
case BAPP_WINDOW_MOVED:
_HandleWindowMoved(message);
break;
case BAPP_WINDOW_RESIZED:
_HandleWindowResized(message);
break;
case BAPP_SCREEN_CHANGED:
/* TODO: Handle screen resize or workspace change */
break;
default:
BApplication::MessageReceived(message);
break;
}
}
/* Window creation/destruction methods */
int32 GetID(SDL_Window *win) {
int32 i;
Jul 13, 2011
Jul 13, 2011
166
for(i = 0; i < _GetNumWindowSlots(); ++i) {
Jul 27, 2011
Jul 27, 2011
167
if( GetSDLWindow(i) == NULL ) {
Jul 13, 2011
Jul 13, 2011
168
_SetSDLWindow(win, i);
169
170
171
172
173
return i;
}
}
/* Expand the vector if all slots are full */
Jul 13, 2011
Jul 13, 2011
174
175
if( i == _GetNumWindowSlots() ) {
_PushBackWindow(win);
176
177
178
179
return i;
}
}
Jul 22, 2011
Jul 22, 2011
180
/* Modes methods */
Jul 30, 2011
Jul 30, 2011
181
void SetPrevMode(display_mode *prevMode) { _saved_mode = prevMode; }
Jul 22, 2011
Jul 22, 2011
182
Jul 30, 2011
Jul 30, 2011
183
display_mode* GetPrevMode() { return _saved_mode; }
Jul 22, 2011
Jul 22, 2011
184
Jul 13, 2011
Jul 13, 2011
185
186
/* FIXME: Bad coding practice, but I can't include SDL_BWin.h here. Is
there another way to do this? */
187
188
void ClearID(SDL_BWin *bwin); /* Defined in SDL_BeApp.cc */
Jul 27, 2011
Jul 27, 2011
189
190
SDL_Window *GetSDLWindow(int32 winID) {
Jul 30, 2011
Jul 30, 2011
191
return _window_map[winID];
Jul 27, 2011
Jul 27, 2011
192
193
}
Jul 30, 2011
Jul 30, 2011
194
195
196
197
198
199
void SetCurrentContext(BGLView *newContext) {
if(_current_context)
_current_context->UnlockGL();
_current_context = newContext;
_current_context->LockGL();
}
200
201
202
203
204
205
206
207
208
209
private:
/* Event management */
void _HandleBasicWindowEvent(BMessage *msg, int32 sdlEventType) {
SDL_Window *win;
int32 winID;
if(
!_GetWinID(msg, &winID)
) {
return;
}
Jul 27, 2011
Jul 27, 2011
210
win = GetSDLWindow(winID);
211
212
213
214
215
216
SDL_SendWindowEvent(win, sdlEventType, 0, 0);
}
void _HandleMouseMove(BMessage *msg) {
SDL_Window *win;
int32 winID;
Jul 26, 2011
Jul 26, 2011
217
int32 x = 0, y = 0;
218
219
if(
!_GetWinID(msg, &winID) ||
Jul 26, 2011
Jul 26, 2011
220
221
msg->FindInt32("x", &x) != B_OK || /* x movement */
msg->FindInt32("y", &y) != B_OK /* y movement */
222
223
224
) {
return;
}
Jul 27, 2011
Jul 27, 2011
225
win = GetSDLWindow(winID);
Jul 26, 2011
Jul 26, 2011
226
SDL_SendMouseMotion(win, 0, x, y);
Jul 27, 2011
Jul 27, 2011
227
228
229
/* FIXME: Attempt at fixing rendering problems */
BE_UpdateWindowFramebuffer(NULL,win,NULL,-1);
230
231
232
233
234
235
236
237
238
239
240
241
242
}
void _HandleMouseButton(BMessage *msg) {
SDL_Window *win;
int32 winID;
int32 button, state; /* left/middle/right, pressed/released */
if(
!_GetWinID(msg, &winID) ||
msg->FindInt32("button-id", &button) != B_OK ||
msg->FindInt32("button-state", &state) != B_OK
) {
return;
}
Jul 27, 2011
Jul 27, 2011
243
win = GetSDLWindow(winID);
244
245
246
247
248
249
250
251
252
253
254
255
256
257
SDL_SendMouseButton(win, state, button);
}
void _HandleMouseWheel(BMessage *msg) {
SDL_Window *win;
int32 winID;
int32 xTicks, yTicks;
if(
!_GetWinID(msg, &winID) ||
msg->FindInt32("xticks", &xTicks) != B_OK ||
msg->FindInt32("yticks", &yTicks) != B_OK
) {
return;
}
Jul 27, 2011
Jul 27, 2011
258
win = GetSDLWindow(winID);
259
260
261
262
263
264
265
266
267
268
269
SDL_SendMouseWheel(win, xTicks, yTicks);
}
void _HandleKey(BMessage *msg) {
int32 scancode, state; /* scancode, pressed/released */
if(
msg->FindInt32("key-state", &state) != B_OK ||
msg->FindInt32("key-scancode", &scancode) != B_OK
) {
return;
}
Jul 26, 2011
Jul 26, 2011
270
271
272
273
274
275
276
/* Make sure this isn't a repeated event (key pressed and held) */
if(state == SDL_PRESSED && BE_GetKeyState(scancode) == SDL_PRESSED) {
return;
}
BE_SetKeyState(scancode, state);
SDL_SendKeyboardKey(state, BE_GetScancodeFromBeKey(scancode));
277
278
279
280
281
282
283
284
285
286
287
288
}
void _HandleMouseFocus(BMessage *msg) {
SDL_Window *win;
int32 winID;
bool bSetFocus; /* If false, lose focus */
if(
!_GetWinID(msg, &winID) ||
msg->FindBool("focusGained", &bSetFocus) != B_OK
) {
return;
}
Jul 27, 2011
Jul 27, 2011
289
win = GetSDLWindow(winID);
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
if(bSetFocus) {
SDL_SetMouseFocus(win);
} else if(SDL_GetMouseFocus() == win) {
/* Only lose all focus if this window was the current focus */
SDL_SetMouseFocus(NULL);
}
}
void _HandleKeyboardFocus(BMessage *msg) {
SDL_Window *win;
int32 winID;
bool bSetFocus; /* If false, lose focus */
if(
!_GetWinID(msg, &winID) ||
msg->FindBool("focusGained", &bSetFocus) != B_OK
) {
return;
}
Jul 27, 2011
Jul 27, 2011
308
win = GetSDLWindow(winID);
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
if(bSetFocus) {
SDL_SetKeyboardFocus(win);
} else if(SDL_GetKeyboardFocus() == win) {
/* Only lose all focus if this window was the current focus */
SDL_SetKeyboardFocus(NULL);
}
}
void _HandleWindowMoved(BMessage *msg) {
SDL_Window *win;
int32 winID;
int32 xPos, yPos;
/* Get the window id and new x/y position of the window */
if(
!_GetWinID(msg, &winID) ||
msg->FindInt32("window-x", &xPos) != B_OK ||
msg->FindInt32("window-y", &yPos) != B_OK
) {
return;
}
Jul 27, 2011
Jul 27, 2011
329
win = GetSDLWindow(winID);
330
SDL_SendWindowEvent(win, SDL_WINDOWEVENT_MOVED, xPos, yPos);
Jul 27, 2011
Jul 27, 2011
331
332
333
/* FIXME: Attempt at fixing rendering problems */
BE_UpdateWindowFramebuffer(NULL,win,NULL,-1);
334
335
336
337
338
339
340
341
342
343
344
345
346
347
}
void _HandleWindowResized(BMessage *msg) {
SDL_Window *win;
int32 winID;
int32 w, h;
/* Get the window id ]and new x/y position of the window */
if(
!_GetWinID(msg, &winID) ||
msg->FindInt32("window-w", &w) != B_OK ||
msg->FindInt32("window-h", &h) != B_OK
) {
return;
}
Jul 27, 2011
Jul 27, 2011
348
win = GetSDLWindow(winID);
349
SDL_SendWindowEvent(win, SDL_WINDOWEVENT_RESIZED, w, h);
Jul 28, 2011
Jul 28, 2011
350
Jul 27, 2011
Jul 27, 2011
351
352
/* FIXME: Attempt at fixing rendering problems */
BE_UpdateWindowFramebuffer(NULL,win,NULL,-1);
353
354
355
356
357
}
bool _GetWinID(BMessage *msg, int32 *winID) {
return msg->FindInt32("window-id", winID) == B_OK;
}
Jul 13, 2011
Jul 13, 2011
358
359
360
Jul 28, 2011
Jul 28, 2011
361
362
/* Vector functions: Wraps vector stuff in case we need to change
implementation */
Jul 13, 2011
Jul 13, 2011
363
void _SetSDLWindow(SDL_Window *win, int32 winID) {
Jul 30, 2011
Jul 30, 2011
364
_window_map[winID] = win;
Jul 13, 2011
Jul 13, 2011
365
366
367
}
int32 _GetNumWindowSlots() {
Jul 30, 2011
Jul 30, 2011
368
return _window_map.size();
Jul 13, 2011
Jul 13, 2011
369
370
371
372
}
void _PopBackWindow() {
Jul 30, 2011
Jul 30, 2011
373
_window_map.pop_back();
Jul 13, 2011
Jul 13, 2011
374
375
376
}
void _PushBackWindow(SDL_Window *win) {
Jul 30, 2011
Jul 30, 2011
377
_window_map.push_back(win);
Jul 13, 2011
Jul 13, 2011
378
379
380
}
Jul 30, 2011
Jul 30, 2011
382
vector<SDL_Window*> _window_map; /* Keeps track of SDL_Windows by index-id */
Jul 22, 2011
Jul 22, 2011
383
Jul 30, 2011
Jul 30, 2011
384
385
display_mode *_saved_mode;
BGLView *_current_context;