]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os_sdl.cxx
fg: move most scenery-related code to simgear
[flightgear.git] / src / Main / fg_os_sdl.cxx
1 #include <stdlib.h>
2
3 #include <osgViewer/ViewerEventHandlers>
4 #include <osgViewer/Viewer>
5
6 #include <simgear/compiler.h>
7 #include <simgear/structure/exception.hxx>
8 #include <simgear/debug/logstream.hxx>
9
10 #include <SDL/SDL.h>
11 #include <plib/pu.h>
12
13 #include <Scenery/scenery.hxx>
14 #include "fg_os.hxx"
15 #include "globals.hxx"
16 #include "renderer.hxx"
17
18 //
19 // fg_os callback registration APIs
20 //
21
22 static int CurrentModifiers = 0;
23 static int CurrentMouseX = 0;
24 static int CurrentMouseY = 0;
25 static int CurrentMouseCursor = MOUSE_CURSOR_POINTER;
26 static int VidMask = SDL_OPENGL|SDL_RESIZABLE;
27
28 //
29 // fg_os implementation
30 //
31 static void initCursors();
32
33 static osg::ref_ptr<osgViewer::Viewer> viewer;
34 static osg::ref_ptr<osg::Camera> mainCamera;
35 static osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> gw;
36
37 void fgOSOpenWindow(int w, int h, int bpp,
38                     bool alpha, bool stencil, bool fullscreen)
39 {
40     int cbits = (bpp <= 16) ?  5 :  8;
41     int zbits = (bpp <= 16) ? 16 : 24;
42
43     if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) == -1)
44         throw sg_throwable(string("Failed to initialize SDL: ")
45                            + SDL_GetError());
46     atexit(SDL_Quit);
47
48     SDL_WM_SetCaption("FlightGear", "FlightGear");
49
50     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, cbits);
51     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, cbits);
52     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, cbits);
53     if(alpha)
54         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
55     if(bpp > 16 && stencil)
56         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
57     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbits);
58     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
59
60     if(fullscreen) {
61         VidMask |= SDL_FULLSCREEN;
62     }
63     SDL_Surface* screen = SDL_SetVideoMode(w, h, 16, VidMask);
64     if ( screen == 0)
65         throw sg_throwable(string("Failed to set SDL video mode: ")
66                                    + SDL_GetError());
67
68     // This enables keycode translation (e.g. capital letters when
69     // shift is pressed, as well as i18n input methods).  Eventually,
70     // we may want to port the input maps to specify <mod-shift>
71     // explicitly, and will turn this off.
72     SDL_EnableUNICODE(1);
73     SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
74
75     initCursors();
76     fgSetMouseCursor(MOUSE_CURSOR_POINTER);
77
78     // FIXME: we may not get what we asked for (especially in full
79     // screen modes), so these need to be propagated back to the
80     // property tree for the rest of the code to inspect...
81     //
82     int realw = screen->w;
83     int realh = screen->h;
84     viewer = new osgViewer::Viewer;
85     viewer->setDatabasePager(FGScenery::getPagerSingleton());
86     gw = viewer->setUpViewerAsEmbeddedInWindow(0, 0, realw, realh);
87     // now the main camera ...
88     //osg::ref_ptr<osg::Camera> camera = new osg::Camera;
89     osg::ref_ptr<osg::Camera> camera = viewer->getCamera();
90     mainCamera = camera;
91     osg::Camera::ProjectionResizePolicy rsp = osg::Camera::VERTICAL;
92     // If a viewport isn't set on the camera, then it's hard to dig it
93     // out of the SceneView objects in the viewer, and the coordinates
94     // of mouse events are somewhat bizzare.
95     camera->setViewport(new osg::Viewport(0, 0, realw, realh));
96     camera->setProjectionResizePolicy(rsp);
97     //viewer->addSlave(camera.get());
98     viewer->setCameraManipulator(globals->get_renderer()->getManipulator());
99     // Let FG handle the escape key with a confirmation
100     viewer->setKeyEventSetsDone(0);
101     osgViewer::StatsHandler* statsHandler = new osgViewer::StatsHandler;
102     statsHandler->setKeyEventTogglesOnScreenStats('*');
103     statsHandler->setKeyEventPrintsOutStats(0);
104     viewer->addEventHandler(statsHandler);
105     // The viewer won't start without some root.
106     viewer->setSceneData(new osg::Group);
107     globals->get_renderer()->setViewer(viewer.get());
108 }
109
110 // Cheap trick to avoid typing GUIEventAdapter over and over...
111 class SDLKeyTranslator : osgGA::GUIEventAdapter
112 {
113 public:
114     static int handleKey(int key, int raw, int keyup);
115 };
116
117 int SDLKeyTranslator::handleKey(int key, int raw, int keyup)
118 {
119     using namespace osgGA;
120     
121     int modmask = 0;
122     int osgKey = 0;
123     if (key == 0)
124         key = raw;
125     // Don't pass capslock or numlock to the FGManipulator; SDL
126     // already transforms the key properly, so FGManipulator will get
127     // confused.
128     if (key == SDLK_CAPSLOCK || key == SDLK_NUMLOCK)
129         return -1;
130     switch(key) {
131     case SDLK_RSHIFT: modmask = KEYMOD_SHIFT;  osgKey = KEY_Shift_R;  break;
132     case SDLK_LSHIFT: modmask = KEYMOD_SHIFT;  osgKey = KEY_Shift_L;  break;
133     case SDLK_RCTRL:  modmask = KEYMOD_CTRL;  osgKey = KEY_Control_R;  break;
134     case SDLK_LCTRL:  modmask = KEYMOD_CTRL;  osgKey = KEY_Control_L;  break;
135     case SDLK_RALT:   modmask = KEYMOD_ALT;   osgKey = KEY_Alt_R;  break;
136     case SDLK_LALT:   modmask = KEYMOD_ALT;   osgKey = KEY_Alt_L;  break;
137     case SDLK_RMETA:  modmask = KEYMOD_META;  osgKey = KEY_Meta_R;  break;
138     case SDLK_LMETA:  modmask = KEYMOD_META;  osgKey = KEY_Meta_L;  break;
139     case SDLK_RSUPER: modmask = KEYMOD_SUPER; osgKey = KEY_Super_R;  break;
140     case SDLK_LSUPER: modmask = KEYMOD_SUPER; osgKey = KEY_Super_L;  break;
141
142     case SDLK_LEFT:  osgKey = KEY_Left;  break;
143     case SDLK_UP:  osgKey = KEY_Up;  break;
144     case SDLK_RIGHT:  osgKey = KEY_Right;  break;
145     case SDLK_DOWN:  osgKey = KEY_Down;  break;
146     case SDLK_PAGEUP:  osgKey = KEY_Page_Up;  break;
147     case SDLK_PAGEDOWN:  osgKey = KEY_Page_Down;  break;
148     case SDLK_HOME:  osgKey = KEY_Home;  break;
149     case SDLK_END:  osgKey = KEY_End;  break;
150     case SDLK_INSERT:  osgKey = KEY_Insert;  break;
151     case SDLK_F1:  osgKey = KEY_F1;  break;
152     case SDLK_F2:  osgKey = KEY_F2;  break;
153     case SDLK_F3:  osgKey = KEY_F3;  break;
154     case SDLK_F4:  osgKey = KEY_F4;  break;
155     case SDLK_F5:  osgKey = KEY_F5;  break;
156     case SDLK_F6:  osgKey = KEY_F6;  break;
157     case SDLK_F7:  osgKey = KEY_F7;  break;
158     case SDLK_F8:  osgKey = KEY_F8;  break;
159     case SDLK_F9:  osgKey = KEY_F9;  break;
160     case SDLK_F10:  osgKey = KEY_F10;  break;
161     case SDLK_F11:  osgKey = KEY_F11;  break;
162     case SDLK_F12:  osgKey = KEY_F12;  break;
163     default:
164         osgKey = key;
165     }
166     int keymod = 0;
167     if(keyup) {
168         CurrentModifiers &= ~modmask;
169         keymod = CurrentModifiers | KEYMOD_RELEASED;
170     } else {
171         CurrentModifiers |= modmask;
172         keymod = CurrentModifiers & ~KEYMOD_RELEASED;
173     }
174     return osgKey;
175 }
176
177 // FIXME: Integrate with existing fgExit() in util.cxx.
178 void fgOSExit(int code)
179 {
180     viewer->setDone(true);
181     exit(code);
182 }
183
184 // originally from osgexamples/osgviewerSDL.cpp
185 bool convertEvent(SDL_Event& event, osgGA::EventQueue& eventQueue)
186 {
187     using namespace osgGA;
188     switch (event.type) {
189
190     case SDL_MOUSEMOTION:
191         eventQueue.mouseMotion(event.motion.x, event.motion.y);
192         return true;
193
194     case SDL_MOUSEBUTTONDOWN:
195         eventQueue.mouseButtonPress(event.button.x, event.button.y, event.button.button);
196         return true;
197
198     case SDL_MOUSEBUTTONUP:
199         eventQueue.mouseButtonRelease(event.button.x, event.button.y, event.button.button);
200         return true;
201
202     case SDL_KEYUP:
203     case SDL_KEYDOWN:
204     {
205         int realKey = SDLKeyTranslator::handleKey(event.key.keysym.unicode,
206                                                   event.key.keysym.sym,
207                                                   event.type == SDL_KEYUP);
208         if (realKey < -1)
209             return true;
210         if (event.type == SDL_KEYUP)
211             eventQueue.keyRelease((osgGA::GUIEventAdapter::KeySymbol)realKey);
212         else
213             eventQueue.keyPress((osgGA::GUIEventAdapter::KeySymbol)realKey);
214         return true;
215     }
216     case SDL_VIDEORESIZE:
217         if (SDL_SetVideoMode(event.resize.w, event.resize.h, 16, VidMask) == 0)
218             throw sg_throwable(string("Failed to set SDL video mode: ")
219                                + SDL_GetError());
220         eventQueue.windowResize(0, 0, event.resize.w, event.resize.h );
221         return true;
222
223     default:
224         break;
225     }
226     return false;
227 }
228
229 void fgOSMainLoop()
230 {
231     while(1) {
232         SDL_Event e;
233         int key;
234         while(SDL_PollEvent(&e)) {
235             // pass the SDL event into the viewers event queue
236             convertEvent(e, *(gw->getEventQueue()));
237
238             switch(e.type) {
239             case SDL_QUIT:
240                 fgOSExit(0);
241                 break;
242             case SDL_VIDEORESIZE:
243                 gw->resized(0, 0, e.resize.w, e.resize.h );
244                 break;
245             }
246         }
247         // draw the new frame
248         viewer->frame();
249
250         // Swap Buffers
251         SDL_GL_SwapBuffers();
252     }
253 }
254
255 int fgGetKeyModifiers()
256 {
257     return CurrentModifiers;
258 }
259
260 void fgWarpMouse(int x, int y)
261 {
262     globals->get_renderer()->getManipulator()->setMouseWarped();
263     SDL_WarpMouse(x, y);
264 }
265
266 void fgOSInit(int* argc, char** argv)
267 {
268     // Nothing to do here.  SDL has no command line options.
269 }
270
271 void fgOSFullScreen()
272 {
273     // Noop.  SDL must set fullscreen at window open time and cannot
274     // change modes.
275 }
276
277 static struct cursor_rec {
278     int name;
279     SDL_Cursor* sdlCursor;
280     int w;
281     int h;
282     int hotx;
283     int hoty;
284     char *img[32]; // '.' == white, '#' == black, ' ' == transparent
285 } cursors[] = {
286     { MOUSE_CURSOR_POINTER, 0, // must be first!
287       10, 16, 1, 1,
288       { "..        ",
289         ".#.       ",
290         ".##.      ",
291         ".###.     ",
292         ".####.    ",
293         ".#####.   ",
294         ".######.  ",
295         ".#######. ",
296         ".########.",
297         ".#####....",
298         ".##.##.   ",
299         ".#. .##.  ",
300         "..  .##.  ",
301         "     .##. ",
302         "     .##. ",
303         "      ..  " } },
304     { MOUSE_CURSOR_CROSSHAIR, 0,
305       17, 17, 8, 8,
306       { "       ...       ",
307         "       .#.       ",
308         "       .#.       ",
309         "       .#.       ",
310         "       .#.       ",
311         "       .#.       ",
312         "       .#.       ",
313         "........#........",
314         ".#######.#######.",
315         "........#........",
316         "       .#.       ",
317         "       .#.       ",
318         "       .#.       ",
319         "       .#.       ",
320         "       .#.       ",
321         "       .#.       ",
322         "       ...       " } },
323     { MOUSE_CURSOR_WAIT, 0,
324       16, 16, 7, 7,
325       { "  .########.    ",
326         "  .########.    ",
327         "  .########.    ",
328         " .##########.   ",
329         ".##....#...##.  ",
330         "##.....#....##..",
331         "#......#.....###",
332         "#.....###....###",
333         "#.....###....###",
334         "#....#.......###",
335         "##..#.......##..",
336         ".##........##.  ",
337         " .##########.   ",
338         "  .########.    ",
339         "  .########.    ",
340         "  .########.    " } },
341     { MOUSE_CURSOR_LEFTRIGHT, 0,
342       17, 9, 8, 4,
343       { "    ..     ..    ",
344         "   .#.     .#.   ",
345         "  .##.......##.  ",
346         " .#############. ",
347         ".####.......####.",
348         " .#############. ",
349         "  .##.......##.  ",
350         "   .#.     .#.   ",
351         "    ..     ..    " } },
352     { MOUSE_CURSOR_NONE, 0, // must come last!
353       1, 1, 0, 0, { " " } },
354 };
355
356 #define NCURSORS (sizeof(cursors)/sizeof(struct cursor_rec))
357
358 void fgSetMouseCursor(int cursor)
359 {
360     if(cursor == MOUSE_CURSOR_NONE) {
361         SDL_ShowCursor(SDL_DISABLE);
362         return;
363     }
364     SDL_ShowCursor(SDL_ENABLE);
365     for(unsigned int i=0; i<NCURSORS; i++) {
366         if(cursor == cursors[i].name) {
367             CurrentMouseCursor = cursor;
368             SDL_SetCursor(cursors[i].sdlCursor);
369             return;
370         }
371     }
372     // Default to pointer
373     CurrentMouseCursor = MOUSE_CURSOR_POINTER;
374     SDL_SetCursor(cursors[0].sdlCursor);
375 }
376
377 int fgGetMouseCursor()
378 {
379     return CurrentMouseCursor;
380 }
381
382 static void initCursors()
383 {
384     unsigned char mask[128], img[128];
385     for(unsigned int i=0; i<NCURSORS; i++) {
386         if(cursors[i].name == MOUSE_CURSOR_NONE) break;
387         for(int j=0; j<128; j++) mask[j] = img[j] = 0;
388         for(int y=0; y<cursors[i].h; y++) {
389             for(int x=0; x<cursors[i].w; x++) {
390                 int byte = (4 * y) + (x >> 3);
391                 int bit = 1 << (7 - (x & 7));
392                 int pix = cursors[i].img[y][x];
393                 if(pix != ' ') { mask[byte] |= bit; }
394                 if(pix == '#') { img[byte] |= bit; }
395             }
396         }
397         cursors[i].sdlCursor = SDL_CreateCursor(img, mask, 32, 32, 
398                                                 cursors[i].hotx,
399                                                 cursors[i].hoty);
400     }
401 }
402
403 // Noop; the graphics context is always current
404 void fgMakeCurrent()
405 {
406 }
407
408 bool fgOSIsMainCamera(const osg::Camera*)
409 {
410   return true;
411 }
412
413 bool fgOSIsMainContext(const osg::GraphicsContext*)
414 {
415   return true;
416 }