Make non-SDL code indipendent from SDL, fix keyboard issues
[mercenary-reloaded.git] / src / libsdl / sdl.c
index bc19158..45f859d 100644 (file)
@@ -32,7 +32,7 @@ static int sdl_initialized = 0;
 static int audio_initialized = 0;
 static SDL_Window *gl_window = NULL;
 static SDL_GLContext gl_context = NULL;
-static void (*keyboard_sdl)(int down, SDL_Keycode sym) = NULL;
+static void (*keyboard_sdl)(int down, enum keycode keycode) = NULL;
 static void (*audio_sdl)(float *data, int len) = NULL;
 
 static void audio_cb(void __attribute__((unused)) *userdata, Uint8 *stream, int len)
@@ -44,7 +44,7 @@ static void audio_cb(void __attribute__((unused)) *userdata, Uint8 *stream, int
        SDL_MixAudio(stream, (Uint8 *)audio_data, len, SDL_MIX_MAXVOLUME);
 }
 
-int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, SDL_Keycode sym), void (*audio)(float *data, int len), int multisampling)
+int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), void (*audio)(float *data, int len), int multisampling)
 {
        int rc;
 
@@ -72,7 +72,7 @@ int init_sdl(const char *progname, int width, int height, int sound_samplerate,
        /* open window */
        gl_window = SDL_CreateWindow(progname, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
        if (!gl_window) {
-               print_error("Failed to open SDL window: %s\n", SDL_GetError());
+               print_error("Failed to open SDL window: %s (try without multisampling)\n", SDL_GetError());
                rc = EIO;
                goto error;
        }
@@ -167,7 +167,93 @@ error:
        return rc;
 }
 
-static int key_ctrl = 0, fullscreen = 0;
+static enum keycode sdl2keycode(SDL_Keycode sym)
+{
+       switch (sym) {
+       case SDLK_LCTRL: return KEYCODE_LCTRL;
+       case SDLK_RCTRL: return KEYCODE_RCTRL;
+       case SDLK_LSHIFT: return KEYCODE_LSHIFT;
+       case SDLK_RSHIFT: return KEYCODE_RSHIFT;
+       case SDLK_PAUSE: return KEYCODE_PAUSE;
+       case SDLK_LEFT: return KEYCODE_LEFT;
+       case SDLK_RIGHT: return KEYCODE_RIGHT;
+       case SDLK_UP: return KEYCODE_UP;
+       case SDLK_DOWN: return KEYCODE_DOWN;
+       case SDLK_END: return KEYCODE_END;
+       case SDLK_a: return KEYCODE_a;
+       case SDLK_b: return KEYCODE_b;
+       case SDLK_c: return KEYCODE_c;
+       case SDLK_d: return KEYCODE_d;
+       case SDLK_e: return KEYCODE_e;
+       case SDLK_f: return KEYCODE_f;
+       case SDLK_g: return KEYCODE_g;
+       case SDLK_h: return KEYCODE_h;
+       case SDLK_i: return KEYCODE_i;
+       case SDLK_j: return KEYCODE_j;
+       case SDLK_k: return KEYCODE_k;
+       case SDLK_l: return KEYCODE_l;
+       case SDLK_m: return KEYCODE_m;
+       case SDLK_n: return KEYCODE_n;
+       case SDLK_o: return KEYCODE_o;
+       case SDLK_p: return KEYCODE_p;
+       case SDLK_q: return KEYCODE_q;
+       case SDLK_r: return KEYCODE_r;
+       case SDLK_s: return KEYCODE_s;
+       case SDLK_t: return KEYCODE_t;
+       case SDLK_u: return KEYCODE_u;
+       case SDLK_v: return KEYCODE_v;
+       case SDLK_w: return KEYCODE_w;
+       case SDLK_x: return KEYCODE_x;
+       case SDLK_y: return KEYCODE_y;
+       case SDLK_z: return KEYCODE_z;
+       case SDLK_0: return KEYCODE_0;
+       case SDLK_1: return KEYCODE_1;
+       case SDLK_2: return KEYCODE_2;
+       case SDLK_3: return KEYCODE_3;
+       case SDLK_4: return KEYCODE_4;
+       case SDLK_5: return KEYCODE_5;
+       case SDLK_6: return KEYCODE_6;
+       case SDLK_7: return KEYCODE_7;
+       case SDLK_8: return KEYCODE_8;
+       case SDLK_9: return KEYCODE_9;
+       case SDLK_KP_0: return KEYCODE_KP_0;
+       case SDLK_KP_1: return KEYCODE_KP_1;
+       case SDLK_KP_2: return KEYCODE_KP_2;
+       case SDLK_KP_3: return KEYCODE_KP_3;
+       case SDLK_KP_4: return KEYCODE_KP_4;
+       case SDLK_KP_5: return KEYCODE_KP_5;
+       case SDLK_KP_6: return KEYCODE_KP_6;
+       case SDLK_KP_7: return KEYCODE_KP_7;
+       case SDLK_KP_8: return KEYCODE_KP_8;
+       case SDLK_KP_9: return KEYCODE_KP_9;
+       case SDLK_KP_DIVIDE: return KEYCODE_KP_DIVIDE;
+       case SDLK_KP_MULTIPLY: return KEYCODE_KP_MULTIPLY;
+       case SDLK_KP_MINUS: return KEYCODE_KP_MINUS;
+       case SDLK_KP_PLUS: return KEYCODE_KP_PLUS;
+       case SDLK_KP_PERIOD: return KEYCODE_KP_PERIOD;
+       case SDLK_F1: return KEYCODE_F1;
+       case SDLK_F2: return KEYCODE_F2;
+       case SDLK_F3: return KEYCODE_F3;
+       case SDLK_F4: return KEYCODE_F4;
+       case SDLK_F5: return KEYCODE_F5;
+       case SDLK_F6: return KEYCODE_F6;
+       case SDLK_F7: return KEYCODE_F7;
+       case SDLK_F8: return KEYCODE_F8;
+       case SDLK_F9: return KEYCODE_F9;
+       case SDLK_F10: return KEYCODE_F10;
+       case SDLK_SPACE: return KEYCODE_SPACE;
+       case SDLK_BACKSPACE: return KEYCODE_BACKSPACE;
+       case SDLK_TAB: return KEYCODE_TAB;
+       case SDLK_KP_ENTER: return KEYCODE_KP_ENTER;
+       case SDLK_RETURN: return KEYCODE_RETURN;
+       case SDLK_ESCAPE: return KEYCODE_ESCAPE;
+       case SDLK_DELETE: return KEYCODE_DELETE;
+       case SDLK_INSERT: return KEYCODE_INSERT;
+       default: return KEYCODE_UNDEFINED;
+       }
+}
+
+static int key_ctrl = 0, key_f = 0, fullscreen = 0;
 
 int event_sdl(void)
 {
@@ -184,7 +270,7 @@ int event_sdl(void)
                if (event.type == SDL_KEYDOWN) {
                        switch (event.key.keysym.sym) {
                        case SDLK_f:
-                               if (key_ctrl) {
+                               if (key_ctrl && !key_f) {
                                        if (fullscreen) {
                                                fullscreen = 0;
                                                SDL_SetWindowFullscreen(gl_window, 0);
@@ -195,13 +281,14 @@ int event_sdl(void)
                                                fullscreen = 1;
                                        }
                                }
+                               key_f = 1;
                                break;
                        case SDLK_LCTRL:
                        case SDLK_RCTRL:
                                key_ctrl = 1;
                                break;
                        }
-                       keyboard_sdl(1, event.key.keysym.sym);
+                       keyboard_sdl(1, sdl2keycode(event.key.keysym.sym));
                }
                if (event.type == SDL_KEYUP) {
                        switch (event.key.keysym.sym) {
@@ -209,8 +296,11 @@ int event_sdl(void)
                        case SDLK_RCTRL:
                                key_ctrl = 0;
                                break;
+                       case SDLK_f:
+                               key_f = 0;
+                               break;
                        }
-                       keyboard_sdl(0, event.key.keysym.sym);
+                       keyboard_sdl(0, sdl2keycode(event.key.keysym.sym));
                }
        }