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

Latest commit

 

History

History
587 lines (501 loc) · 15 KB

SDL_BWin.h

File metadata and controls

587 lines (501 loc) · 15 KB
 
Apr 26, 2001
Apr 26, 2001
1
/*
Apr 8, 2011
Apr 8, 2011
2
3
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
Apr 26, 2001
Apr 26, 2001
4
Apr 8, 2011
Apr 8, 2011
5
6
7
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.
Apr 26, 2001
Apr 26, 2001
8
Apr 8, 2011
Apr 8, 2011
9
10
11
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:
Apr 26, 2001
Apr 26, 2001
12
Apr 8, 2011
Apr 8, 2011
13
14
15
16
17
18
19
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.
Apr 26, 2001
Apr 26, 2001
20
21
22
23
24
*/
#ifndef _SDL_BWin_h
#define _SDL_BWin_h
Jul 12, 2011
Jul 12, 2011
25
26
27
28
#ifdef __cplusplus
extern "C" {
#endif
Feb 17, 2006
Feb 17, 2006
29
#include "SDL_config.h"
Jul 12, 2011
Jul 12, 2011
30
31
32
33
34
35
#include "SDL.h"
#include "SDL_syswm.h"
#ifdef __cplusplus
}
#endif
Feb 17, 2006
Feb 17, 2006
36
Apr 26, 2001
Apr 26, 2001
37
38
39
40
#include <stdio.h>
#include <AppKit.h>
#include <InterfaceKit.h>
#include <be/game/DirectWindow.h>
Feb 16, 2006
Feb 16, 2006
41
#if SDL_VIDEO_OPENGL
Apr 26, 2001
Apr 26, 2001
42
43
44
45
#include <be/opengl/GLView.h>
#endif
#include "SDL_events.h"
#include "SDL_BView.h"
Jul 12, 2011
Jul 12, 2011
46
47
48
49
50
51
52
53
54
55
56
57
#include "../../main/beos/SDL_BApp.h"
enum WinCommands {
BWIN_MOVE_WINDOW,
BWIN_RESIZE_WINDOW,
BWIN_SHOW_WINDOW,
BWIN_HIDE_WINDOW,
BWIN_MAXIMIZE_WINDOW,
BWIN_MINIMIZE_WINDOW,
BWIN_RESTORE_WINDOW, /* TODO: IMPLEMENT THIS! */
BWIN_SET_TITLE,
BWIN_FULLSCREEN
Apr 26, 2001
Apr 26, 2001
58
59
};
Jul 12, 2011
Jul 12, 2011
60
Jul 10, 2006
Jul 10, 2006
61
class SDL_BWin:public BDirectWindow
Apr 26, 2001
Apr 26, 2001
62
{
Jul 10, 2006
Jul 10, 2006
63
public:
Jul 12, 2011
Jul 12, 2011
64
/* Constructor/Destructor */
Jul 10, 2006
Jul 10, 2006
65
66
67
68
SDL_BWin(BRect bounds):BDirectWindow(bounds, "Untitled",
B_TITLED_WINDOW, 0)
{
last_buttons = 0;
Jul 20, 2011
Jul 20, 2011
69
printf("SDL_BWin.h: 69\n");
Jul 10, 2006
Jul 10, 2006
70
the_view = NULL;
Feb 16, 2006
Feb 16, 2006
71
#if SDL_VIDEO_OPENGL
Jul 10, 2006
Jul 10, 2006
72
SDL_GLView = NULL;
Apr 26, 2001
Apr 26, 2001
73
#endif
Jul 10, 2006
Jul 10, 2006
74
SDL_View = NULL;
Jul 12, 2011
Jul 12, 2011
75
_shown = false;
Jul 10, 2006
Jul 10, 2006
76
inhibit_resize = false;
Jul 12, 2011
Jul 12, 2011
77
mouse_focused = false;
Jul 20, 2011
Jul 20, 2011
78
prev_frame = NULL; printf("SDL_BWin.h: 79\n");
Jul 10, 2006
Jul 10, 2006
79
80
81
82
83
84
}
virtual ~ SDL_BWin()
{
Lock();
if (the_view) {
Feb 16, 2006
Feb 16, 2006
85
#if SDL_VIDEO_OPENGL
Jul 10, 2006
Jul 10, 2006
86
87
88
if (the_view == SDL_GLView) {
SDL_GLView->UnlockGL();
}
Apr 26, 2001
Apr 26, 2001
89
#endif
Jul 10, 2006
Jul 10, 2006
90
91
92
93
RemoveChild(the_view);
the_view = NULL;
}
Unlock();
Feb 16, 2006
Feb 16, 2006
94
#if SDL_VIDEO_OPENGL
Jul 10, 2006
Jul 10, 2006
95
96
97
if (SDL_GLView) {
delete SDL_GLView;
}
Apr 26, 2001
Apr 26, 2001
98
#endif
Jul 10, 2006
Jul 10, 2006
99
100
101
102
if (SDL_View) {
delete SDL_View;
}
}
Jul 12, 2011
Jul 12, 2011
103
104
105
/* Other construction */
Jul 10, 2006
Jul 10, 2006
106
107
108
109
110
111
virtual int CreateView(Uint32 flags, Uint32 gl_flags)
{
int retval;
retval = 0;
Lock();
Jun 24, 2011
Jun 24, 2011
112
if (flags & SDL_OPENGL/*SDL_INTERNALOPENGL*/) {
Feb 16, 2006
Feb 16, 2006
113
#if SDL_VIDEO_OPENGL
Jul 10, 2006
Jul 10, 2006
114
115
116
117
118
119
120
121
122
123
124
125
126
127
if (SDL_GLView == NULL) {
SDL_GLView = new BGLView(Bounds(), "SDL GLView",
B_FOLLOW_ALL_SIDES,
(B_WILL_DRAW | B_FRAME_EVENTS),
gl_flags);
}
if (the_view != SDL_GLView) {
if (the_view) {
RemoveChild(the_view);
}
AddChild(SDL_GLView);
SDL_GLView->LockGL();
the_view = SDL_GLView;
}
Apr 26, 2001
Apr 26, 2001
128
#else
Jul 10, 2006
Jul 10, 2006
129
130
SDL_SetError("OpenGL support not enabled");
retval = -1;
Apr 26, 2001
Apr 26, 2001
131
#endif
Jul 10, 2006
Jul 10, 2006
132
133
134
135
136
137
} else {
if (SDL_View == NULL) {
SDL_View = new SDL_BView(Bounds());
}
if (the_view != SDL_View) {
if (the_view) {
Feb 16, 2006
Feb 16, 2006
138
#if SDL_VIDEO_OPENGL
Jul 10, 2006
Jul 10, 2006
139
140
141
if (the_view == SDL_GLView) {
SDL_GLView->UnlockGL();
}
Apr 26, 2001
Apr 26, 2001
142
#endif
Jul 10, 2006
Jul 10, 2006
143
144
145
146
147
148
149
150
151
RemoveChild(the_view);
}
AddChild(SDL_View);
the_view = SDL_View;
}
}
Unlock();
return (retval);
}
Jul 12, 2011
Jul 12, 2011
152
Jul 20, 2011
Jul 20, 2011
153
154
155
156
157
158
/* * * * * Framebuffering* * * * */
virtual void DirectConnected(direct_buffer_info *info) {
}
Jul 12, 2011
Jul 12, 2011
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
/* * * * * Event sending * * * * */
/* Hook functions */
virtual void FrameMoved(BPoint origin) {
/* Post a message to the BApp so that it can handle the window event */
BMessage msg(BAPP_WINDOW_MOVED);
msg.AddInt32("window-x", (int)origin.x);
msg.AddInt32("window-y", (int)origin.y);
_PostWindowEvent(msg);
/* Perform normal hook operations */
BDirectWindow::FrameMoved(origin);
}
virtual void FrameResized(float width, float height) {
/* Post a message to the BApp so that it can handle the window event */
BMessage msg(BAPP_WINDOW_RESIZED);
msg.AddInt32("window-w", (int)width) + 1; /* TODO: Check that +1 is needed */
msg.AddInt32("window-h", (int)height) + 1;
_PostWindowEvent(msg);
/* Perform normal hook operations */
BDirectWindow::FrameResized(width, height);
}
virtual bool QuitRequested() {
BMessage msg(BAPP_WINDOW_CLOSE_REQUESTED);
_PostWindowEvent(msg);
/* We won't allow a quit unless asked by DestroyWindow() */
return false;
}
virtual void WindowActivated(bool active) {
BMessage msg(BAPP_KEYBOARD_FOCUS); /* Mouse focus sold separately */
_PostWindowEvent(msg);
}
virtual void Zoom(BPoint origin,
float width,
float height) {
BMessage msg(BAPP_MAXIMIZE); /* Closest thing to maximization Haiku has */
_PostWindowEvent(msg);
/* Before the window zooms, record its size */
if( !prev_frame )
prev_frame = new BRect(Frame());
/* Perform normal hook operations */
BDirectWindow::Zoom(origin, width, height);
}
/* Member functions */
virtual void Show() {
BDirectWindow::Show();
_shown = true;
BMessage msg(BAPP_SHOW);
_PostWindowEvent(msg);
}
virtual void Hide() {
/* FIXME: Multiple hides require multiple shows to undo. Should
this be altered to prevent this from happening? */
BDirectWindow::Hide();
_shown = false;
BMessage msg(BAPP_HIDE);
_PostWindowEvent(msg);
}
virtual void Minimize(bool minimize) {
BDirectWindow::Minimize(minimize);
int32 minState = (minimize ? BAPP_MINIMIZE : BAPP_RESTORE);
BMessage msg(minState);
_PostWindowEvent(msg);
}
/* BView message interruption */
virtual void DispatchMessage(BMessage * msg, BHandler * target)
Jul 10, 2006
Jul 10, 2006
240
{
Jul 12, 2011
Jul 12, 2011
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
BPoint where; /* Used by mouse moved */
int32 buttons; /* Used for mouse button events */
int32 key; /* Used for key events */
switch (msg->what) {
case B_MOUSE_MOVED:
where;
int32 transit;
if (msg->FindPoint("where", &where) == B_OK
&& msg->FindInt32("be:transit", &transit) == B_OK) {
_MouseMotionEvent(where, transit);
}
/* FIXME: Apparently a button press/release event might be dropped
if made before before a different button is released. Does
B_MOUSE_MOVED have the data needed to check if a mouse button
state has changed? */
if (msg->FindInt32("buttons", &buttons) == B_OK) {
_MouseButtonEvent(buttons);
}
break;
case B_MOUSE_DOWN:
case B_MOUSE_UP:
/* _MouseButtonEvent() detects any and all buttons that may have
changed state, as well as that button's new state */
if (msg->FindInt32("buttons", &buttons) == B_OK) {
_MouseButtonEvent(buttons);
}
break;
case B_MOUSE_WHEEL_CHANGED:
float x, y;
if (msg->FindFloat("be:wheel_delta_x", &x) == B_OK
&& msg->FindFloat("be:wheel_delta_y", &y) == B_OK) {
_MouseWheelEvent((int)x, (int)y);
}
break;
case B_KEY_DOWN:
case B_UNMAPPED_KEY_DOWN: /* modifier keys are unmapped */
if (msg->FindInt32("key", &key) == B_OK) {
_KeyEvent((SDL_Scancode)key, SDL_PRESSED);
}
break;
case B_KEY_UP:
case B_UNMAPPED_KEY_UP: /* modifier keys are unmapped */
if (msg->FindInt32("key", &key) == B_OK) {
_KeyEvent(key, SDL_RELEASED);
}
break;
case _UPDATE_:
case _UPDATE_IF_NEEDED_: /* Hopefully one doesn't call the other */
_RepaintEvent();
break;
default:
/* move it after switch{} so it's always handled
that way we keep BeOS feautures like:
- CTRL+Q to close window (and other shortcuts)
- PrintScreen to make screenshot into /boot/home
- etc.. */
//BDirectWindow::DispatchMessage(msg, target);
break;
}
BDirectWindow::DispatchMessage(msg, target);
}
/* Handle command messages */
virtual void MessageReceived(BMessage* message) {
switch (message->what) {
/* Handle commands from SDL */
case BWIN_SET_TITLE:
_SetTitle(message);
break;
case BWIN_MOVE_WINDOW:
_MoveTo(message);
break;
case BWIN_RESIZE_WINDOW:
_ResizeTo(message);
break;
case BWIN_SHOW_WINDOW:
Show();
break;
case BWIN_HIDE_WINDOW:
Hide();
break;
case BWIN_MAXIMIZE_WINDOW:
BWindow::Zoom();
break;
case BWIN_MINIMIZE_WINDOW:
Minimize(true);
break;
case BWIN_RESTORE_WINDOW:
_Restore();
break;
case BWIN_FULLSCREEN:
_SetFullScreen(message);
break;
default:
/* Perform normal message handling */
BDirectWindow::MessageReceived(message);
break;
}
Jul 10, 2006
Jul 10, 2006
349
}
Jul 12, 2011
Jul 12, 2011
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* Accessor methods */
bool IsShown() { return _shown; }
int32 GetID() { return _id; }
/* Setter methods */
void SetID(int32 id) { _id = id; }
/* FIXME: Methods copied directly; do we need them? */
#if 0 /* Disabled until its purpose is determined */
Jul 10, 2006
Jul 10, 2006
370
371
virtual void SetXYOffset(int x, int y)
{
Feb 16, 2006
Feb 16, 2006
372
#if SDL_VIDEO_OPENGL
Jul 10, 2006
Jul 10, 2006
373
374
375
if (the_view == SDL_GLView) {
return;
}
Apr 26, 2001
Apr 26, 2001
376
#endif
Jul 10, 2006
Jul 10, 2006
377
378
379
380
SDL_View->SetXYOffset(x, y);
}
virtual void GetXYOffset(int &x, int &y)
{
Feb 16, 2006
Feb 16, 2006
381
#if SDL_VIDEO_OPENGL
Jul 10, 2006
Jul 10, 2006
382
383
384
385
386
if (the_view == SDL_GLView) {
x = 0;
y = 0;
return;
}
Apr 26, 2001
Apr 26, 2001
387
#endif
Jul 10, 2006
Jul 10, 2006
388
389
SDL_View->GetXYOffset(x, y);
}
Jul 12, 2011
Jul 12, 2011
390
#endif
Jul 10, 2006
Jul 10, 2006
391
392
393
394
395
396
397
398
399
400
401
402
403
virtual bool BeginDraw(void)
{
return (Lock());
}
virtual void DrawAsync(BRect updateRect)
{
SDL_View->DrawAsync(updateRect);
}
virtual void EndDraw(void)
{
SDL_View->Sync();
Unlock();
}
Feb 16, 2006
Feb 16, 2006
404
#if SDL_VIDEO_OPENGL
Jul 10, 2006
Jul 10, 2006
405
406
407
408
409
410
virtual void SwapBuffers(void)
{
SDL_GLView->UnlockGL();
SDL_GLView->LockGL();
SDL_GLView->SwapBuffers();
}
Apr 26, 2001
Apr 26, 2001
411
#endif
Jul 10, 2006
Jul 10, 2006
412
413
414
415
416
virtual BView *View(void)
{
return (the_view);
}
Jul 12, 2011
Jul 12, 2011
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
private:
/* Event redirection */
void _MouseMotionEvent(BPoint &where, int32 transit) {
if(transit == B_EXITED_VIEW) {
/* Change mouse focus */
if(mouse_focused) {
_MouseFocusEvent(false);
}
} else {
static int x = 0, y = 0;
/* Change mouse focus */
if (!mouse_focused) {
_MouseFocusEvent(true);
}
// GetXYOffset(x, y); //FIXME: What is this doing? (from SDL 1.2)
BMessage msg(BAPP_MOUSE_MOVED);
msg.AddInt32("dx", where.x - x);
msg.AddInt32("dy", where.y - y);
x = (int) where.x;
y = (int) where.y;
_PostWindowEvent(msg);
}
Jul 10, 2006
Jul 10, 2006
445
}
Jul 12, 2011
Jul 12, 2011
446
447
448
449
450
451
452
453
454
455
void _MouseFocusEvent(bool focusGained) {
mouse_focused = focusGained;
BMessage msg(BAPP_MOUSE_FOCUS);
msg.AddBool("focusGained", focusGained);
_PostWindowEvent(msg);
//FIXME: Why were these here?
// if false: be_app->SetCursor(B_HAND_CURSOR);
// if true: SDL_SetCursor(NULL);
Jul 10, 2006
Jul 10, 2006
456
}
Jul 12, 2011
Jul 12, 2011
457
458
459
460
461
462
463
464
465
466
467
void _MouseButtonEvent(int32 buttons) {
int32 buttonStateChange = buttons ^ last_buttons;
/* Make sure at least one button has changed state */
if( !(buttonStateChange) ) {
return;
}
/* Add any mouse button events */
if(buttonStateChange & B_PRIMARY_MOUSE_BUTTON) {
Jul 20, 2011
Jul 20, 2011
468
469
_SendMouseButton(SDL_BUTTON_LEFT, buttons &
B_PRIMARY_MOUSE_BUTTON);
Jul 12, 2011
Jul 12, 2011
470
471
}
if(buttonStateChange & B_SECONDARY_MOUSE_BUTTON) {
Jul 20, 2011
Jul 20, 2011
472
473
_SendMouseButton(SDL_BUTTON_RIGHT, buttons &
B_PRIMARY_MOUSE_BUTTON);
Jul 12, 2011
Jul 12, 2011
474
475
}
if(buttonStateChange & B_TERTIARY_MOUSE_BUTTON) {
Jul 20, 2011
Jul 20, 2011
476
477
_SendMouseButton(SDL_BUTTON_MIDDLE, buttons &
B_PRIMARY_MOUSE_BUTTON);
Jul 12, 2011
Jul 12, 2011
478
479
480
}
last_buttons = buttons;
Jul 10, 2006
Jul 10, 2006
481
}
Jul 12, 2011
Jul 12, 2011
482
483
484
485
486
487
void _SendMouseButton(int32 button, int32 state) {
BMessage msg(BAPP_MOUSE_BUTTON);
msg.AddInt32("button-id", button);
msg.AddInt32("button-state", state);
_PostWindowEvent(msg);
Jul 10, 2006
Jul 10, 2006
488
}
Jul 12, 2011
Jul 12, 2011
489
490
491
492
493
494
495
void _MouseWheelEvent(int32 x, int32 y) {
/* Create a message to pass along to the BeApp thread */
BMessage msg(BAPP_MOUSE_WHEEL);
msg.AddInt32("xticks", x);
msg.AddInt32("yticks", y);
_PostWindowEvent(msg);
Jul 10, 2006
Jul 10, 2006
496
}
Jul 12, 2011
Jul 12, 2011
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
void _KeyEvent(int32 keyCode, int32 keyState) {
/* Create a message to pass along to the BeApp thread */
BMessage msg(BAPP_KEY);
msg.AddInt32("key-state", keyState);
msg.AddInt32("key-scancode", keyCode);
be_app->PostMessage(&msg);
/* Apparently SDL only uses the scancode */
}
void _RepaintEvent() {
/* Force a repaint: Call the SDL exposed event */
BMessage msg(BAPP_REPAINT);
_PostWindowEvent(msg);
}
void _PostWindowEvent(BMessage &msg) {
msg.AddInt32("window-id", _id);
be_app->PostMessage(&msg);
}
/* Command methods (functions called upon by SDL) */
void _SetTitle(BMessage *msg) {
const char *title;
if(
msg->FindString("window-title", &title) != B_OK
) {
return;
}
SetTitle(title);
}
void _MoveTo(BMessage *msg) {
int32 x, y;
if(
msg->FindInt32("window-x", &x) != B_OK ||
msg->FindInt32("window-y", &y) != B_OK
) {
return;
}
MoveTo(x, y);
}
void _ResizeTo(BMessage *msg) {
int32 w, h;
if(
msg->FindInt32("window-w", &w) != B_OK ||
msg->FindInt32("window-h", &h) != B_OK
) {
return;
}
ResizeTo(w, h);
}
void _Restore() {
if(IsMinimized()) {
Minimize(false);
} else if(IsHidden()) {
Show();
} else if(prev_frame != NULL) { /* Zoomed */
MoveTo(prev_frame->left, prev_frame->top);
ResizeTo(prev_frame->Width(), prev_frame->Height());
}
Jul 10, 2006
Jul 10, 2006
559
560
}
Jul 12, 2011
Jul 12, 2011
561
562
563
564
565
566
567
568
569
570
571
void _SetFullScreen(BMessage *msg) {
bool fullscreen;
if(
msg->FindBool("fullscreen", &fullscreen) != B_OK
) {
return;
}
SetFullScreen(fullscreen);
}
/* Members */
Feb 16, 2006
Feb 16, 2006
572
#if SDL_VIDEO_OPENGL
Jul 10, 2006
Jul 10, 2006
573
BGLView * SDL_GLView;
Apr 26, 2001
Apr 26, 2001
574
#endif
Jul 10, 2006
Jul 10, 2006
575
576
SDL_BView *SDL_View;
BView *the_view;
Jul 12, 2011
Jul 12, 2011
577
Jul 10, 2006
Jul 10, 2006
578
int32 last_buttons;
Jul 12, 2011
Jul 12, 2011
579
580
581
582
int32 _id; /* Window id used by SDL_BApp */
bool mouse_focused; /* Does this window have mouse focus? */
bool _shown;
bool inhibit_resize;
Jun 24, 2011
Jun 24, 2011
583
Jul 12, 2011
Jul 12, 2011
584
BRect *prev_frame; /* Previous position and size of the window */
Apr 26, 2001
Apr 26, 2001
585
586
};
Jul 12, 2011
Jul 12, 2011
587
#endif