]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_os_sdl.cxx
- implement progress information (enabled by default; can be turned off via
[flightgear.git] / src / Main / fg_os_sdl.cxx
index fd3b7242e297e7e524e8087326580373a149887e..16ab62d22f0ef2ac9ace1b4c4336caccc5bc0d22 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>
 
@@ -11,6 +15,7 @@
 
 static fgIdleHandler IdleHandler = 0;
 static fgDrawHandler DrawHandler = 0;
+static fgWindowResizeHandler WindowResizeHandler = 0;
 static fgKeyHandler KeyHandler = 0;
 static fgMouseClickHandler MouseClickHandler = 0;
 static fgMouseMotionHandler MouseMotionHandler = 0;
@@ -18,7 +23,9 @@ static fgMouseMotionHandler MouseMotionHandler = 0;
 static int CurrentModifiers = 0;
 static int CurrentMouseX = 0;
 static int CurrentMouseY = 0;
+static int CurrentMouseCursor = MOUSE_CURSOR_POINTER;
 static bool NeedRedraw = false;
+static int VidMask = SDL_OPENGL|SDL_RESIZABLE;
 
 void fgRegisterIdleHandler(fgIdleHandler func)
 {
@@ -33,7 +40,7 @@ void fgRegisterDrawHandler(fgDrawHandler func)
 
 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
 {
-    // Noop.  SDL does not support window resize.
+    WindowResizeHandler = func;
 }
 
 void fgRegisterKeyHandler(fgKeyHandler func)
@@ -54,33 +61,47 @@ void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
 //
 // fg_os implementation
 //
+static void initCursors();
 
-void fgOSOpenWindow(int w, int h, int bpp, bool alpha, bool fullscreen)
+void fgOSOpenWindow(int w, int h, int bpp,
+                    bool alpha, bool stencil, bool fullscreen)
 {
     int cbits = (bpp <= 16) ?  5 :  8;
-    int zbits = (bpp <= 16) ? 16 : 32;
+    int zbits = (bpp <= 16) ? 16 : 24;
+
+    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) == -1)
+        throw sg_throwable(string("Failed to initialize SDL: ")
+                           + SDL_GetError());
+    atexit(SDL_Quit);
 
-    SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE); // FIXME: handle errors
+    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(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);
 
-    int vidmask = SDL_OPENGL;
-    if(fullscreen) vidmask |= SDL_FULLSCREEN;
-    SDL_SetVideoMode(w, h, 16, vidmask); // FIXME: handle errors
-
-    SDL_WM_SetCaption("FlightGear", "FlightGear");
+    if(fullscreen) {
+        VidMask |= SDL_FULLSCREEN;
+    }
+    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,
     // we may want to port the input maps to specify <mod-shift>
     // explicitly, and will turn this off.
     SDL_EnableUNICODE(1);
+    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
+
+    initCursors();
+    fgSetMouseCursor(MOUSE_CURSOR_POINTER);
 
     // FIXME: we may not get what we asked for (especially in full
     // screen modes), so these need to be propagated back to the
@@ -91,7 +112,7 @@ void fgOSOpenWindow(int w, int h, int bpp, bool alpha, bool fullscreen)
     // 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) {
@@ -124,18 +145,41 @@ static void handleKey(int key, int keyup)
     case SDLK_F11:      key = PU_KEY_F11;       break;
     case SDLK_F12:      key = PU_KEY_F12;       break;
     }
-    if(keyup) CurrentModifiers &= ~modmask;
-    else      CurrentModifiers |= modmask;
+
+    // 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;
+        keymod = CurrentModifiers | KEYMOD_RELEASED;
+    } else {
+        CurrentModifiers |= modmask;
+        keymod = CurrentModifiers & ~KEYMOD_RELEASED;
+    }
     if(modmask == 0 && KeyHandler)
-        (*KeyHandler)(key, CurrentModifiers, CurrentMouseX, CurrentMouseY);
+        (*KeyHandler)(key, keymod, CurrentMouseX, CurrentMouseY);
 }
 
-// FIXME: need to export this and get the rest of FlightGear to use
-// it, the SDL hook is required to do things like reset the video
-// mode and key repeat.  Integrate with existing fgExit() in util.cxx.
+// FIXME: Integrate with existing fgExit() in util.cxx.
 void fgOSExit(int code)
 {
-    SDL_Quit();
     exit(code);
 }
 
@@ -143,6 +187,7 @@ void fgOSMainLoop()
 {
     while(1) {
         SDL_Event e;
+        int key;
         while(SDL_PollEvent(&e)) {
             switch(e.type) {
             case SDL_QUIT:
@@ -150,7 +195,9 @@ void fgOSMainLoop()
                 break;
             case SDL_KEYDOWN:
             case SDL_KEYUP:
-                handleKey(e.key.keysym.unicode, e.key.state == SDL_RELEASED);
+                key = e.key.keysym.unicode;
+                if(key == 0) key = e.key.keysym.sym;
+                handleKey(key, e.key.keysym.sym, e.key.state == SDL_RELEASED);
                 break;
             case SDL_MOUSEBUTTONDOWN:
             case SDL_MOUSEBUTTONUP:
@@ -168,6 +215,14 @@ void fgOSMainLoop()
                 if(MouseMotionHandler)
                     (*MouseMotionHandler)(e.motion.x, e.motion.y);
                 break;
+            case SDL_VIDEORESIZE:
+                if (SDL_SetVideoMode(e.resize.w, e.resize.h, 16, VidMask) == 0)
+                    throw sg_throwable(string("Failed to set SDL video mode: ")
+                            + SDL_GetError());
+
+                if (WindowResizeHandler)
+                    (*WindowResizeHandler)(e.resize.w, e.resize.h);
+                break;
             }
         }
         if(IdleHandler) (*IdleHandler)();
@@ -205,6 +260,128 @@ void fgOSFullScreen()
     // change modes.
 }
 
-// Figure out what to do with these
-void fgSetMouseCursor(int cursor) {}
-int  fgGetMouseCursor() { return MOUSE_CURSOR_POINTER; }
+static struct cursor_rec {
+    int name;
+    SDL_Cursor* sdlCursor;
+    int w;
+    int h;
+    int hotx;
+    int hoty;
+    char *img[32]; // '.' == white, '#' == black, ' ' == transparent
+} cursors[] = {
+    { MOUSE_CURSOR_POINTER, 0, // must be first!
+      10, 16, 1, 1,
+      { "..        ",
+        ".#.       ",
+        ".##.      ",
+        ".###.     ",
+        ".####.    ",
+        ".#####.   ",
+        ".######.  ",
+        ".#######. ",
+        ".########.",
+        ".#####....",
+        ".##.##.   ",
+        ".#. .##.  ",
+        "..  .##.  ",
+        "     .##. ",
+        "     .##. ",
+        "      ..  " } },
+    { MOUSE_CURSOR_CROSSHAIR, 0,
+      17, 17, 8, 8,
+      { "       ...       ",
+        "       .#.       ",
+        "       .#.       ",
+        "       .#.       ",
+        "       .#.       ",
+        "       .#.       ",
+        "       .#.       ",
+        "........#........",
+        ".#######.#######.",
+        "........#........",
+        "       .#.       ",
+        "       .#.       ",
+        "       .#.       ",
+        "       .#.       ",
+        "       .#.       ",
+        "       .#.       ",
+        "       ...       " } },
+    { MOUSE_CURSOR_WAIT, 0,
+      16, 16, 7, 7,
+      { "  .########.    ",
+        "  .########.    ",
+        "  .########.    ",
+        " .##########.   ",
+        ".##....#...##.  ",
+        "##.....#....##..",
+        "#......#.....###",
+        "#.....###....###",
+        "#.....###....###",
+        "#....#.......###",
+        "##..#.......##..",
+        ".##........##.  ",
+        " .##########.   ",
+        "  .########.    ",
+        "  .########.    ",
+        "  .########.    " } },
+    { MOUSE_CURSOR_LEFTRIGHT, 0,
+      17, 9, 8, 4,
+      { "    ..     ..    ",
+        "   .#.     .#.   ",
+        "  .##.......##.  ",
+        " .#############. ",
+        ".####.......####.",
+        " .#############. ",
+        "  .##.......##.  ",
+        "   .#.     .#.   ",
+        "    ..     ..    " } },
+    { MOUSE_CURSOR_NONE, 0, // must come last!
+      1, 1, 0, 0, { " " } },
+};
+
+#define NCURSORS (sizeof(cursors)/sizeof(struct cursor_rec))
+
+void fgSetMouseCursor(int cursor)
+{
+    if(cursor == MOUSE_CURSOR_NONE) {
+        SDL_ShowCursor(SDL_DISABLE);
+        return;
+    }
+    SDL_ShowCursor(SDL_ENABLE);
+    for(unsigned int i=0; i<NCURSORS; i++) {
+        if(cursor == cursors[i].name) {
+            CurrentMouseCursor = cursor;
+            SDL_SetCursor(cursors[i].sdlCursor);
+            return;
+        }
+    }
+    // Default to pointer
+    CurrentMouseCursor = MOUSE_CURSOR_POINTER;
+    SDL_SetCursor(cursors[0].sdlCursor);
+}
+
+int fgGetMouseCursor()
+{
+    return CurrentMouseCursor;
+}
+
+static void initCursors()
+{
+    unsigned char mask[128], img[128];
+    for(unsigned int i=0; i<NCURSORS; i++) {
+        if(cursors[i].name == MOUSE_CURSOR_NONE) break;
+        for(int j=0; j<128; j++) mask[j] = img[j] = 0;
+        for(int y=0; y<cursors[i].h; y++) {
+            for(int x=0; x<cursors[i].w; x++) {
+                int byte = (4 * y) + (x >> 3);
+                int bit = 1 << (7 - (x & 7));
+                int pix = cursors[i].img[y][x];
+                if(pix != ' ') { mask[byte] |= bit; }
+                if(pix == '#') { img[byte] |= bit; }
+            }
+        }
+        cursors[i].sdlCursor = SDL_CreateCursor(img, mask, 32, 32, 
+                                                cursors[i].hotx,
+                                                cursors[i].hoty);
+    }
+}