]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_os_sdl.cxx
Migrate FlightGear code to use "#include SG_GL*" defined in
[flightgear.git] / src / Main / fg_os_sdl.cxx
index 0190ad784cfce06cc28e66e7151eeda1a5dd8c14..99f93056c115a0b5524a75f3d4d75ccf4fb6bf3d 100644 (file)
@@ -1,5 +1,9 @@
 #include <stdlib.h>
 
+#include <simgear/compiler.h>
+#include <simgear/structure/exception.hxx>
+#include <simgear/debug/logstream.hxx>
+
 #include <SDL/SDL.h>
 #include <plib/pu.h>
 
@@ -63,14 +67,18 @@ void fgOSOpenWindow(int w, int h, int bpp,
     int cbits = (bpp <= 16) ?  5 :  8;
     int zbits = (bpp <= 16) ? 16 : 24;
 
-    SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE); // FIXME: handle errors
+    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) == -1)
+        throw sg_throwable(string("Failed to initialize SDL: ")
+                           + SDL_GetError());
+
+    SDL_WM_SetCaption("FlightGear", "FlightGear");
 
     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, cbits);
     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, cbits);
     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, cbits);
     if(alpha)
         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
-    if(stencil)
+    if(bpp > 16 && stencil)
         SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbits);
     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
@@ -79,9 +87,9 @@ void fgOSOpenWindow(int w, int h, int bpp,
     if(fullscreen) {
         vidmask |= SDL_FULLSCREEN;
     }
-    SDL_SetVideoMode(w, h, 16, vidmask); // FIXME: handle errors
-
-    SDL_WM_SetCaption("FlightGear", "FlightGear");
+    if (SDL_SetVideoMode(w, h, 16, vidmask) == 0)
+        throw sg_throwable(string("Failed to set SDL video mode: ")
+                                   + SDL_GetError());
 
     // This enables keycode translation (e.g. capital letters when
     // shift is pressed, as well as i18n input methods).  Eventually,
@@ -102,7 +110,7 @@ void fgOSOpenWindow(int w, int h, int bpp,
     // int realh = screen->h;
 }
 
-static void handleKey(int key, int keyup)
+static void handleKey(int key, int raw, int keyup)
 {
     int modmask = 0;
     switch(key) {
@@ -135,6 +143,26 @@ static void handleKey(int key, int keyup)
     case SDLK_F11:      key = PU_KEY_F11;       break;
     case SDLK_F12:      key = PU_KEY_F12;       break;
     }
+
+    // Keypad codes.  This is a situation where we *don't* want the
+    // Unicode cooking for our input.  Oddly, neither PUI nor Glut
+    // define these anywhere, so I lifted the numbers out of
+    // FlightGear's keyboard.xml.  Some unused code are therefore
+    // missing.
+    switch(raw) {
+    case SDLK_KP0:      key = 364; break;
+    case SDLK_KP1:      key = 363; break;
+    case SDLK_KP2:      key = 359; break;
+    case SDLK_KP3:      key = 361; break;
+    case SDLK_KP4:      key = 356; break;
+    case SDLK_KP5:      key = 309; break;
+    case SDLK_KP6:      key = 358; break;
+    case SDLK_KP7:      key = 362; break;
+    case SDLK_KP8:      key = 357; break;
+    case SDLK_KP9:      key = 360; break;
+    case SDLK_KP_ENTER: key = 269; break;
+    }
+
     int keymod = 0;
     if(keyup) {
         CurrentModifiers &= ~modmask;
@@ -170,7 +198,7 @@ void fgOSMainLoop()
             case SDL_KEYUP:
                 key = e.key.keysym.unicode;
                 if(key == 0) key = e.key.keysym.sym;
-                handleKey(key, e.key.state == SDL_RELEASED);
+                handleKey(key, e.key.keysym.sym, e.key.state == SDL_RELEASED);
                 break;
             case SDL_MOUSEBUTTONDOWN:
             case SDL_MOUSEBUTTONUP: