1 /*         ______   ___    ___
2  *        /\  _  \ /\_ \  /\_ \
3  *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___
4  *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
5  *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
6  *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7  *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8  *                                           /\____/
9  *                                           \_/__/
10  *
11  *      Common gfx subsystem routines.
12  *
13  *      By Jason Wilkins.
14  *
15  *      See readme.txt for copyright information.
16  */
17 
18 #include "bealleg.h"
19 #include "allegro/internal/aintern.h"
20 #include "allegro/platform/aintbeos.h"
21 
22 #if !defined ALLEGRO_BEOS && !defined ALLEGRO_HAIKU
23 #error something is wrong with the makefile
24 #endif
25 
26 
27 #define SUSPEND_ALL_THREADS()		\
28 {									\
29    be_key_suspend();				\
30    be_sound_suspend();				\
31    be_time_suspend();				\
32    be_sys_suspend();				\
33    be_main_suspend();				\
34 }
35 
36 #define RESUME_ALL_THREADS()		\
37 {									\
38    be_key_resume();					\
39    be_sound_resume();				\
40    be_time_resume();				\
41    be_sys_resume();					\
42    be_main_resume();				\
43 }
44 
45 
46 
47 AL_CONST BE_MODE_TABLE _be_mode_table[] = {
48    { 8,    640,  400, B_8_BIT_640x400,    B_CMAP8 },
49    { 8,    640,  480, B_8_BIT_640x480,    B_CMAP8 },
50    { 8,    800,  600, B_8_BIT_800x600,    B_CMAP8 },
51    { 8,   1024,  768, B_8_BIT_1024x768,   B_CMAP8 },
52    { 8,   1152,  900, B_8_BIT_1152x900,   B_CMAP8 },
53    { 8,   1280, 1024, B_8_BIT_1280x1024,  B_CMAP8 },
54    { 8,   1600, 1200, B_8_BIT_1600x1200,  B_CMAP8 },
55    { 15,   640,  480, B_15_BIT_640x480,   B_RGB15 },
56    { 15,   800,  600, B_15_BIT_800x600,   B_RGB15 },
57    { 15,  1024,  768, B_15_BIT_1024x768,  B_RGB15 },
58    { 15,  1152,  900, B_15_BIT_1152x900,  B_RGB15 },
59    { 15,  1280, 1024, B_15_BIT_1280x1024, B_RGB15 },
60    { 15,  1600, 1200, B_15_BIT_1600x1200, B_RGB15 },
61    { 16,   640,  480, B_16_BIT_640x480,   B_RGB16 },
62    { 16,   800,  600, B_16_BIT_800x600,   B_RGB16 },
63    { 16,  1024,  768, B_16_BIT_1024x768,  B_RGB16 },
64    { 16,  1152,  900, B_16_BIT_1152x900,  B_RGB16 },
65    { 16,  1280, 1024, B_16_BIT_1280x1024, B_RGB16 },
66    { 16,  1600, 1200, B_16_BIT_1600x1200, B_RGB16 },
67    { 32,   640,  480, B_32_BIT_640x480,   B_RGB32 },
68    { 32,   800,  600, B_32_BIT_800x600,   B_RGB32 },
69    { 32,  1024,  768, B_32_BIT_1024x768,  B_RGB32 },
70    { 32,  1152,  900, B_32_BIT_1152x900,  B_RGB32 },
71    { 32,  1280, 1024, B_32_BIT_1280x1024, B_RGB32 },
72    { 32,  1600, 1200, B_32_BIT_1600x1200, B_RGB32 },
73    { -1,     0,    0,                  0,       0 }
74 };
75 
76 sem_id _be_fullscreen_lock = -1;
77 sem_id _be_window_lock = -1;
78 volatile int _be_lock_count  = 0;
79 int *_be_dirty_lines = NULL;
80 int _be_mouse_z = 0;
81 void (*_be_window_close_hook)() = NULL;
82 volatile bool _be_gfx_initialized = false;
83 
84 BeAllegroView         *_be_allegro_view = NULL;
85 BeAllegroScreen       *_be_allegro_screen = NULL;
86 BeAllegroWindow       *_be_allegro_window = NULL;
87 BeAllegroDirectWindow *_be_allegro_direct_window = NULL;
88 BeAllegroOverlay      *_be_allegro_overlay = NULL;
89 BWindow               *_be_window = NULL;
90 
91 static int refresh_rate = 60;
92 
93 
94 
95 /* BeAllegroView::BeAllegroView:
96  */
BeAllegroView(BRect frame,const char * name,uint32 resizingMode,uint32 flags,int f)97 BeAllegroView::BeAllegroView(BRect frame, const char *name,
98    uint32 resizingMode, uint32 flags, int f)
99    : BView(frame, name, resizingMode, flags)
100 {
101    this->flags = f;
102 }
103 
104 
105 
106 /* BeAllegroView::MessageReceived:
107  */
MessageReceived(BMessage * message)108 void BeAllegroView::MessageReceived(BMessage *message)
109 {
110    switch (message->what) {
111       case B_SIMPLE_DATA:
112          break;
113 
114       default:
115          BView::MessageReceived(message);
116          break;
117    }
118 }
119 
120 
121 
Draw(BRect update_rect)122 void BeAllegroView::Draw(BRect update_rect)
123 {
124    if (flags & BE_ALLEGRO_VIEW_OVERLAY) {
125       SetHighColor(_be_allegro_overlay->color_key);
126       FillRect(update_rect);
127       return;
128    }
129 
130    if ((flags & BE_ALLEGRO_VIEW_DIRECT) || (!_be_allegro_window))
131       return;
132 
133    if ((!_be_focus_count) &&
134        ((_be_switch_mode == SWITCH_AMNESIA) ||
135         (_be_switch_mode == SWITCH_BACKAMNESIA)))
136       return;
137 
138    if (_be_allegro_window->screen_depth == 8)
139       DrawBitmap(_be_allegro_window->aux_buffer, update_rect, update_rect);
140    else
141       DrawBitmap(_be_allegro_window->buffer, update_rect, update_rect);
142 }
143 
144 
145 
_be_gfx_set_truecolor_shifts()146 void _be_gfx_set_truecolor_shifts()
147 {
148    _rgb_r_shift_15 = 10;
149    _rgb_g_shift_15 = 5;
150    _rgb_b_shift_15 = 0;
151 
152    _rgb_r_shift_16 = 11;
153    _rgb_g_shift_16 = 5;
154    _rgb_b_shift_16 = 0;
155 
156    _rgb_r_shift_24 = 16;
157    _rgb_g_shift_24 = 8;
158    _rgb_b_shift_24 = 0;
159 
160    _rgb_a_shift_32 = 24;
161    _rgb_r_shift_32 = 16;
162    _rgb_g_shift_32 = 8;
163    _rgb_b_shift_32 = 0;
164 }
165 
166 
167 
_be_change_focus(bool active)168 void _be_change_focus(bool active)
169 {
170    int i;
171 
172    if (_be_focus_count < 0)
173       _be_focus_count = 0;
174 
175    if (active) {
176       _be_focus_count++;
177       if (_be_gfx_initialized) {
178          switch (_be_switch_mode) {
179             case SWITCH_AMNESIA:
180             case SWITCH_BACKAMNESIA:
181                if ((_be_allegro_direct_window) &&
182                    (_be_allegro_direct_window->drawing_thread_id > 0)) {
183                   resume_thread(_be_allegro_direct_window->drawing_thread_id);
184                }
185                if (_be_switch_mode == SWITCH_BACKAMNESIA)
186                   break;
187             case SWITCH_PAUSE:
188                RESUME_ALL_THREADS();
189                break;
190          }
191          _switch_in();
192       }
193    }
194    else {
195       _be_focus_count--;
196       if (_be_gfx_initialized) {
197          _switch_out();
198          switch (_be_switch_mode) {
199             case SWITCH_AMNESIA:
200             case SWITCH_BACKAMNESIA:
201                if ((_be_allegro_direct_window) &&
202                    (_be_allegro_direct_window->drawing_thread_id > 0)) {
203                   suspend_thread(_be_allegro_direct_window->drawing_thread_id);
204                }
205                if (_be_switch_mode == SWITCH_BACKAMNESIA)
206                   break;
207             case SWITCH_PAUSE:
208                if (_be_midisynth)
209                   _be_midisynth->AllNotesOff(false);
210                SUSPEND_ALL_THREADS();
211                break;
212          }
213       }
214    }
215 }
216 
217 
218 
_be_handle_window_close(const char * title)219 bool _be_handle_window_close(const char *title)
220 {
221    if (_be_window_close_hook)
222       _be_window_close_hook();
223 
224    return false;
225 }
226 
227 
228 
be_gfx_vsync(void)229 extern "C" void be_gfx_vsync(void)
230 {
231    if(BScreen(_be_window).WaitForRetrace() != B_OK) {
232       if (_timer_installed) {
233          int start_count;
234 
235          start_count = retrace_count;
236 
237          while (start_count == retrace_count) {
238          }
239       }
240       else {
241          snooze (500000 / refresh_rate);
242       }
243    }
244 }
245 
246