SDL backend shifted key translation
[dosdemo] / src / sdl / main.c
index b9ed604..fcaee9b 100644 (file)
@@ -16,6 +16,8 @@ static void toggle_fullscreen(void);
 static int handle_sball_event(sball_event *ev);
 static void recalc_sball_matrix(float *xform);
 
+static int sdlkey_to_demokey(int sdlkey, unsigned int mod);
+
 
 static int quit;
 static SDL_Surface *fbsurf;
@@ -48,7 +50,7 @@ int main(int argc, char **argv)
                fprintf(stderr, "failed to allocate virtual framebuffer\n");
                return 1;
        }
-       vmem_front = vmem_back = fb_pixels;
+       vmem = fb_pixels;
 
        SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
        if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, fb_bpp, sdl_flags))) {
@@ -91,7 +93,6 @@ int main(int argc, char **argv)
 
                time_msec = get_msec();
                demo_draw();
-               drawFps(fb_pixels);
 
                if(SDL_MUSTLOCK(fbsurf)) {
                        SDL_LockSurface(fbsurf);
@@ -140,6 +141,8 @@ void wait_vsync(void)
 
 void swap_buffers(void *pixels)
 {
+       demo_post_draw(pixels ? pixels : fb_pixels);
+
        /* do nothing, all pointers point to the same buffer */
        if(opt.vsync) {
                wait_vsync();
@@ -163,6 +166,8 @@ static int bnmask(int sdlbn)
 
 static void handle_event(SDL_Event *ev)
 {
+       int key;
+
        switch(ev->type) {
        case SDL_QUIT:
                quit = 1;
@@ -175,7 +180,8 @@ static void handle_event(SDL_Event *ev)
                        toggle_fullscreen();
                        break;
                }
-               demo_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED ? 1 : 0);
+               key = sdlkey_to_demokey(ev->key.keysym.sym, ev->key.keysym.mod);
+               demo_keyboard(key, ev->key.state == SDL_PRESSED ? 1 : 0);
                break;
 
        case SDL_MOUSEMOTION:
@@ -252,10 +258,37 @@ static int handle_sball_event(sball_event *ev)
        return 0;
 }
 
-void recalc_sball_matrix(float *xform)
+static void recalc_sball_matrix(float *xform)
 {
        quat_to_mat(xform, rot);
        xform[12] = pos.x;
        xform[13] = pos.y;
        xform[14] = pos.z;
 }
+
+#define SSORG  '\''
+#define SSEND  '`'
+static char symshift[] = {
+       '"', 0, 0, 0, 0, '<', '_', '>', '?',
+       ')', '!', '@', '#', '$', '%', '^', '&', '*', '(',
+       0, ':', 0, '+', 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       '{', '|', '}', 0, 0, '~'
+};
+
+
+static int sdlkey_to_demokey(int sdlkey, unsigned int mod)
+{
+       if(sdlkey < 128) {
+               if(mod & (KMOD_SHIFT)) {
+                       if(sdlkey >= 'a' && sdlkey <= 'z') {
+                               sdlkey = toupper(sdlkey);
+                       } else if(sdlkey >= SSORG && sdlkey <= SSEND) {
+                               sdlkey = symshift[sdlkey - SSORG];
+                       }
+               }
+               return sdlkey;
+       }
+       if(sdlkey < 256) return 0;
+       return sdlkey - 128;
+}