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