]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os_sdl.cxx
Don't ask for 32 bit depth buffer. Some cards do only 24, leaving 8
[flightgear.git] / src / Main / fg_os_sdl.cxx
1 #include <stdlib.h>
2
3 #include <SDL/SDL.h>
4 #include <plib/pu.h>
5
6 #include "fg_os.hxx"
7
8 //
9 // fg_os callback registration APIs
10 //
11
12 static fgIdleHandler IdleHandler = 0;
13 static fgDrawHandler DrawHandler = 0;
14 static fgKeyHandler KeyHandler = 0;
15 static fgMouseClickHandler MouseClickHandler = 0;
16 static fgMouseMotionHandler MouseMotionHandler = 0;
17
18 static int CurrentModifiers = 0;
19 static int CurrentMouseX = 0;
20 static int CurrentMouseY = 0;
21 static int CurrentMouseCursor = MOUSE_CURSOR_POINTER;
22 static bool NeedRedraw = false;
23
24 void fgRegisterIdleHandler(fgIdleHandler func)
25 {
26     IdleHandler = func;
27 }
28
29 void fgRegisterDrawHandler(fgDrawHandler func)
30 {
31     DrawHandler = func;
32     NeedRedraw = true;
33 }
34
35 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
36 {
37     // Noop.  SDL does not support window resize.
38 }
39
40 void fgRegisterKeyHandler(fgKeyHandler func)
41 {
42     KeyHandler = func;
43 }
44
45 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
46 {
47     MouseClickHandler = func;
48 }
49
50 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
51 {
52     MouseMotionHandler = func;
53 }
54
55 //
56 // fg_os implementation
57 //
58 static void initCursors();
59
60 void fgOSOpenWindow(int w, int h, int bpp,
61                     bool alpha, bool stencil, bool fullscreen)
62 {
63     int cbits = (bpp <= 16) ?  5 :  8;
64     int zbits = (bpp <= 16) ? 16 : 24;
65
66     SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE); // FIXME: handle errors
67
68     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, cbits);
69     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, cbits);
70     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, cbits);
71     if(alpha)
72         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
73     if(stencil)
74         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
75     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbits);
76     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
77
78     int vidmask = SDL_OPENGL;
79     if(fullscreen) {
80         vidmask |= SDL_FULLSCREEN;
81     }
82     SDL_SetVideoMode(w, h, 16, vidmask); // FIXME: handle errors
83
84     SDL_WM_SetCaption("FlightGear", "FlightGear");
85
86     // This enables keycode translation (e.g. capital letters when
87     // shift is pressed, as well as i18n input methods).  Eventually,
88     // we may want to port the input maps to specify <mod-shift>
89     // explicitly, and will turn this off.
90     SDL_EnableUNICODE(1);
91     SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
92
93     initCursors();
94     fgSetMouseCursor(MOUSE_CURSOR_POINTER);
95
96     // FIXME: we may not get what we asked for (especially in full
97     // screen modes), so these need to be propagated back to the
98     // property tree for the rest of the code to inspect...
99     //
100     // SDL_Surface* screen = SDL_GetVideoSurface();
101     // int realw = screen->w;
102     // int realh = screen->h;
103 }
104
105 static void handleKey(int key, int keyup)
106 {
107     int modmask = 0;
108     switch(key) {
109     case SDLK_RSHIFT: modmask = KEYMOD_SHIFT; break;
110     case SDLK_LSHIFT: modmask = KEYMOD_SHIFT; break;
111     case SDLK_RCTRL:  modmask = KEYMOD_CTRL;  break;
112     case SDLK_LCTRL:  modmask = KEYMOD_CTRL;  break;
113     case SDLK_RALT:   modmask = KEYMOD_ALT;   break;
114     case SDLK_LALT:   modmask = KEYMOD_ALT;   break;
115
116     case SDLK_LEFT:     key = PU_KEY_LEFT;      break;
117     case SDLK_UP:       key = PU_KEY_UP;        break;
118     case SDLK_RIGHT:    key = PU_KEY_RIGHT;     break;
119     case SDLK_DOWN:     key = PU_KEY_DOWN;      break;
120     case SDLK_PAGEUP:   key = PU_KEY_PAGE_UP;   break;
121     case SDLK_PAGEDOWN: key = PU_KEY_PAGE_DOWN; break;
122     case SDLK_HOME:     key = PU_KEY_HOME;      break;
123     case SDLK_END:      key = PU_KEY_END;       break;
124     case SDLK_INSERT:   key = PU_KEY_INSERT;    break;
125     case SDLK_F1:       key = PU_KEY_F1;        break;
126     case SDLK_F2:       key = PU_KEY_F2;        break;
127     case SDLK_F3:       key = PU_KEY_F3;        break;
128     case SDLK_F4:       key = PU_KEY_F4;        break;
129     case SDLK_F5:       key = PU_KEY_F5;        break;
130     case SDLK_F6:       key = PU_KEY_F6;        break;
131     case SDLK_F7:       key = PU_KEY_F7;        break;
132     case SDLK_F8:       key = PU_KEY_F8;        break;
133     case SDLK_F9:       key = PU_KEY_F9;        break;
134     case SDLK_F10:      key = PU_KEY_F10;       break;
135     case SDLK_F11:      key = PU_KEY_F11;       break;
136     case SDLK_F12:      key = PU_KEY_F12;       break;
137     }
138     int keymod = 0;
139     if(keyup) {
140         CurrentModifiers &= ~modmask;
141         keymod = CurrentModifiers | KEYMOD_RELEASED;
142     } else {
143         CurrentModifiers |= modmask;
144         keymod = CurrentModifiers & ~KEYMOD_RELEASED;
145     }
146     if(modmask == 0 && KeyHandler)
147         (*KeyHandler)(key, keymod, CurrentMouseX, CurrentMouseY);
148 }
149
150 // FIXME: need to export this and get the rest of FlightGear to use
151 // it, the SDL hook is required to do things like reset the video
152 // mode and key repeat.  Integrate with existing fgExit() in util.cxx.
153 void fgOSExit(int code)
154 {
155     SDL_Quit();
156     exit(code);
157 }
158
159 void fgOSMainLoop()
160 {
161     while(1) {
162         SDL_Event e;
163         int key;
164         while(SDL_PollEvent(&e)) {
165             switch(e.type) {
166             case SDL_QUIT:
167                 fgOSExit(0);
168                 break;
169             case SDL_KEYDOWN:
170             case SDL_KEYUP:
171                 key = e.key.keysym.unicode;
172                 if(key == 0) key = e.key.keysym.sym;
173                 handleKey(key, e.key.state == SDL_RELEASED);
174                 break;
175             case SDL_MOUSEBUTTONDOWN:
176             case SDL_MOUSEBUTTONUP:
177                 // Note offset: SDL uses buttons 1,2,3 not 0,1,2
178                 CurrentMouseX = e.button.x;
179                 CurrentMouseY = e.button.y;
180                 if(MouseClickHandler)
181                     (*MouseClickHandler)(e.button.button - 1,
182                                          e.button.state == SDL_RELEASED,
183                                          e.button.x, e.button.y);
184                 break;
185             case SDL_MOUSEMOTION:
186                 CurrentMouseX = e.motion.x;
187                 CurrentMouseY = e.motion.y;
188                 if(MouseMotionHandler)
189                     (*MouseMotionHandler)(e.motion.x, e.motion.y);
190                 break;
191             }
192         }
193         if(IdleHandler) (*IdleHandler)();
194         if(NeedRedraw && DrawHandler) {
195             (*DrawHandler)();
196             SDL_GL_SwapBuffers();
197             NeedRedraw = false;
198         }
199     }
200 }
201
202 int fgGetKeyModifiers()
203 {
204     return CurrentModifiers;
205 }
206
207 void fgWarpMouse(int x, int y)
208 {
209     SDL_WarpMouse(x, y);
210 }
211
212 void fgRequestRedraw()
213 {
214     NeedRedraw = true;
215 }
216
217 void fgOSInit(int* argc, char** argv)
218 {
219     // Nothing to do here.  SDL has no command line options.
220 }
221
222 void fgOSFullScreen()
223 {
224     // Noop.  SDL must set fullscreen at window open time and cannot
225     // change modes.
226 }
227
228 static struct cursor_rec {
229     int name;
230     SDL_Cursor* sdlCursor;
231     int w;
232     int h;
233     int hotx;
234     int hoty;
235     char *img[32]; // '.' == white, '#' == black, ' ' == transparent
236 } cursors[] = {
237     { MOUSE_CURSOR_POINTER, 0, // must be first!
238       10, 16, 1, 1,
239       { "..        ",
240         ".#.       ",
241         ".##.      ",
242         ".###.     ",
243         ".####.    ",
244         ".#####.   ",
245         ".######.  ",
246         ".#######. ",
247         ".########.",
248         ".#####....",
249         ".##.##.   ",
250         ".#. .##.  ",
251         "..  .##.  ",
252         "     .##. ",
253         "     .##. ",
254         "      ..  " } },
255     { MOUSE_CURSOR_CROSSHAIR, 0,
256       17, 17, 8, 8,
257       { "       ...       ",
258         "       .#.       ",
259         "       .#.       ",
260         "       .#.       ",
261         "       .#.       ",
262         "       .#.       ",
263         "       .#.       ",
264         "........#........",
265         ".#######.#######.",
266         "........#........",
267         "       .#.       ",
268         "       .#.       ",
269         "       .#.       ",
270         "       .#.       ",
271         "       .#.       ",
272         "       .#.       ",
273         "       ...       " } },
274     { MOUSE_CURSOR_WAIT, 0,
275       16, 16, 7, 7,
276       { "  .########.    ",
277         "  .########.    ",
278         "  .########.    ",
279         " .##########.   ",
280         ".##....#...##.  ",
281         "##.....#....##..",
282         "#......#.....###",
283         "#.....###....###",
284         "#.....###....###",
285         "#....#.......###",
286         "##..#.......##..",
287         ".##........##.  ",
288         " .##########.   ",
289         "  .########.    ",
290         "  .########.    ",
291         "  .########.    " } },
292     { MOUSE_CURSOR_LEFTRIGHT, 0,
293       17, 9, 8, 4,
294       { "    ..     ..    ",
295         "   .#.     .#.   ",
296         "  .##.......##.  ",
297         " .#############. ",
298         ".####.......####.",
299         " .#############. ",
300         "  .##.......##.  ",
301         "   .#.     .#.   ",
302         "    ..     ..    " } },
303     { MOUSE_CURSOR_NONE, 0, // must come last!
304       1, 1, 0, 0, { " " } },
305 };
306
307 #define NCURSORS (sizeof(cursors)/sizeof(struct cursor_rec))
308
309 void fgSetMouseCursor(int cursor)
310 {
311     if(cursor == MOUSE_CURSOR_NONE) {
312         SDL_ShowCursor(SDL_DISABLE);
313         return;
314     }
315     SDL_ShowCursor(SDL_ENABLE);
316     for(int i=0; i<NCURSORS; i++) {
317         if(cursor == cursors[i].name) {
318             CurrentMouseCursor = cursor;
319             SDL_SetCursor(cursors[i].sdlCursor);
320             return;
321         }
322     }
323     // Default to pointer
324     CurrentMouseCursor = MOUSE_CURSOR_POINTER;
325     SDL_SetCursor(cursors[0].sdlCursor);
326 }
327
328 int fgGetMouseCursor()
329 {
330     return CurrentMouseCursor;
331 }
332
333 static void initCursors()
334 {
335     unsigned char mask[128], img[128];
336     int i=0;
337     for(int i=0; i<NCURSORS; i++) {
338         if(cursors[i].name == MOUSE_CURSOR_NONE) break;
339         for(int j=0; j<128; j++) mask[j] = img[j] = 0;
340         for(int y=0; y<cursors[i].h; y++) {
341             for(int x=0; x<cursors[i].w; x++) {
342                 int byte = (4 * y) + (x >> 3);
343                 int bit = 1 << (7 - (x & 7));
344                 int pix = cursors[i].img[y][x];
345                 if(pix != ' ') { mask[byte] |= bit; }
346                 if(pix == '#') { img[byte] |= bit; }
347             }
348         }
349         cursors[i].sdlCursor = SDL_CreateCursor(img, mask, 32, 32, 
350                                                 cursors[i].hotx,
351                                                 cursors[i].hoty);
352     }
353 }