OVR: Show mirror of single eye only
[mercenary-reloaded.git] / src / mercenary / main.c
index 24ea8a0..45d87e4 100644 (file)
@@ -30,7 +30,7 @@
 #include "../libovr/ovr.h"
 #include "../libovr/keyboard.h"
 #endif
-#include "../libsdl/opengl.h"
+#include "../libopengl/opengl.h"
 #include "../libsdl/print.h"
 #include "../libcpu/m68k.h"
 #include "../libcpu/execute.h"
@@ -55,7 +55,7 @@
 
 static int config_ctrl_c = 0;
 static int config_amiga_speed = 0; /* fast speed */
-static double config_fps = 16.0;
+static double config_fps = 10.0;
 #if !defined(_WIN32)
 static const char *config_gamesave_dir = ".mercenary";
 #endif
@@ -64,17 +64,18 @@ static int config_audio_filter = 1;
 static int config_render = 1; /* opengl render */
 static int config_skip_intro = 0;
 static int config_multisampling = 8;
+static int config_joystick = -1, config_joystick_x = -1, config_joystick_y = -1, config_joystick_fire = -1;
 #ifdef HAVE_OVR
-static double config_benson_size = 0.5;
+static double config_benson_size = 0.7;
 static double config_fov = FOV_JOLLY;
 #else
 static double config_benson_size = 1.0;
 static double config_fov = FOV_NOVAGEN;
 #endif
-static double config_monitor_distance = 31.5; /* inch */
+static double config_monitor_distance = 41.5; /* inch */
 #ifdef HAVE_OVR
-static double config_keyboard_distance = 30.5; /* inch */
-static double config_keyboard_height = -22.0; /* inch */
+static double config_keyboard_distance = 38.5; /* inch */
+static double config_keyboard_height = -26.0; /* inch */
 #endif
 static int config_debug_transparent = 0;
 static int config_debug_opengl = 0;
@@ -108,18 +109,13 @@ static int config_improve_round_planets = 0;      /* set to 1 to make planets exactly
 static uint8_t *memory = NULL;
 static uint8_t *stop_event = NULL;
 static uint8_t *image = NULL;
-static uint8_t *help_osd[2] = { NULL, NULL };
+static uint8_t *help_osd[3] = { NULL, NULL, NULL };
 static uint8_t *info_osd = NULL;
 static int help_view = 1;
-static int help_views = 1;
-static int32_t osd_timer = 0;
-#ifdef HAVE_OVR
-#define SCREEN_WIDTH   1344
-#define SCREEN_HEIGHT  800
-#else
+static int help_views = 0;
+static int32_t osd_timer = 0, border_timer = 0;
 #define SCREEN_WIDTH   (320*3)
 #define SCREEN_HEIGHT  (200*3)
-#endif
 #define IMAGE_WIDTH    320
 #define IMAGE_HEIGHT   200
 #define BENSON_AT_LINE 136
@@ -139,6 +135,7 @@ static int render_improved = 0; /* render improved image, if set */
 static int debug_opengl = 0; /* render both, amiga screen and  improved image, if set */
 static int intro_skipped = 0; /* indicated if we already have landed */
 static int window_width, window_height;
+static int mission_disk = 0;
 
 static const char *home_dir;
 
@@ -170,6 +167,12 @@ int parse_args(int argc, char *argv[])
                        print_info("        Set field-of-view. Default is %.0f.\n", FOV_NOVAGEN);
                        print_info(" -i --skip-intro\n");
                        print_info("        Skip intro sequence approaching to Eris space port.\n");
+                       print_info(" -j --joystick <num> | list\n");
+                       print_info("        Select given joystick number or show list. (default = 0, if available)\n");
+                       print_info("    --joystick-x <axis num>\n");
+                       print_info("    --joystick-y <axis num>\n");
+                       print_info("    --joystick-fire <button num>\n");
+                       print_info("        Specify explicit axis and button of joystick.\n");
                        print_info("Improvement options:\n");
                        print_info("    --extend-roads 1 | 0\n");
                        print_info("        Roads in the distance end in a single point. This was ok for low\n");
@@ -294,6 +297,34 @@ illegal_parameter:
                if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "--skip-intro")) {
                        config_skip_intro = 1;
                } else
+               if (!strcmp(argv[i], "-j") || !strcmp(argv[i], "--joystick")) {
+                       i++;
+                       if (argc == i)
+                               goto missing_parameter;
+                       if (!strcmp(argv[i], "list")) {
+                               sdl_list_joysticks();
+                               return 1;
+                       }
+                       config_joystick = atoi(argv[i]);
+               } else
+               if (!strcmp(argv[i], "--joystick-x")) {
+                       i++;
+                       if (argc == i)
+                               goto missing_parameter;
+                       config_joystick_x = atoi(argv[i]);
+               } else
+               if (!strcmp(argv[i], "--joystick-y")) {
+                       i++;
+                       if (argc == i)
+                               goto missing_parameter;
+                       config_joystick_y = atoi(argv[i]);
+               } else
+               if (!strcmp(argv[i], "--joystick-fire")) {
+                       i++;
+                       if (argc == i)
+                               goto missing_parameter;
+                       config_joystick_fire = atoi(argv[i]);
+               } else
                if (!strcmp(argv[i], "--extend-roads")) {
                        i++;
                        if (argc == i)
@@ -359,10 +390,23 @@ static void osd_info(const char *param, const char *value)
        osd_timer = ticks_sdl() + 2500;
 }
 
+static void toggle_help(void)
+{
+       if (help_view == 0)
+               help_view = 2;
+       else
+       if (help_view == help_views)
+               help_view = 0;
+       else
+               help_view++;
+}
+
 static void resize_window(int width, int height)
 {
        window_width = width;
        window_height = height;
+       border_timer = ticks_sdl() + 1500;
+       osd_info("", "window resized");
 }
 
 #ifdef HAVE_OVR
@@ -372,6 +416,8 @@ static int thrust_last = 0;
 static int joystick_set_x_last = 0, joystick_set_y_last = 0;
 static int keyboard_on = 0;
 static enum keycode vr_key_pressed = 0, vr_key = 0;
+static int we_walk = 0, we_rotate = 0;
+static double degrees45 = 45.0 / 180.0 * M_PI;
 
 static void handle_vr_poses(void)
 {
@@ -380,49 +426,44 @@ static void handle_vr_poses(void)
        double hand_right_yaw = 0.0, hand_right_pitch = 0.0, hand_right_roll = 0.0;
        double head_yaw = 0.0, head_pitch = 0.0, head_roll = 0.0;
        double stick_left_x = 0.0, stick_left_y = 0.0, stick_right_x = 0.0, stick_right_y = 0.0;
-       int thrust = 0;
+       int thrust = thrust_last;
        int joystick_set_x = 0, joystick_set_y = 0;
        static uint32_t current_time, last_time = 0, diff;
        static double increment, inc_count = 0.0;
 
        /* handle input */
        get_poses_ovr(&button_a, &button_b, &button_x, &button_y, &button_menu, &button_left_trigger, &button_right_trigger, &button_left_thumb, &button_right_thumb, &hand_right_x, &hand_right_y, &hand_right_z, &hand_right_yaw, &hand_right_pitch, &hand_right_roll, &stick_left_x, &stick_left_y, &stick_right_x, &stick_right_y, &head_yaw, &head_pitch, &head_roll);
-       if (button_menu && !button_menu_last) {
+       if (button_a && !button_a_last) {
                /* menu toggle */
-               if (help_view == help_views)
-                       help_view = 0;
-               else
-                       help_view++;
+               toggle_help();
        }
-       if (button_left_trigger && button_right_trigger) {
+       if (button_b && !button_b_last) {
+               /* reset observer */
+               reset_observer_ovr();
+               osd_info("", "reset observer");
+       }
+       if (button_x && !button_x_last) {
+               /* menu toggle */
+               toggle_help();
+       }
+       if (button_y && !button_y_last) {
                /* reset observer */
                reset_observer_ovr();
                osd_info("", "reset observer");
        }
        if (!help_view) {
-               if (button_x && !button_x_last) {
-                       /* 'board' pressed */
-                       set_amiga_key(KEYCODE_b, 1);
-                       set_amiga_key(KEYCODE_b, 0);
-               }
-               if (button_y && !button_y_last) {
-                       /* 'leave' pressed */
-                       set_amiga_key(KEYCODE_l, 1);
-                       set_amiga_key(KEYCODE_l, 0);
-               }
-               if (button_right_thumb && !button_right_thumb_last) {
-                       /* 'change orientation' pressed */
-                       double roll, pitch, yaw;
-                       mercenary_get_orientation(&roll, &pitch, &yaw);
-                       mercenary_set_orientation(yaw + hand_right_yaw);
-               }
                if (button_right_trigger && !button_right_trigger_last) {
                        /* trigger pressed */
-                       if (!keyboard_on) {
+                       if (keyboard_on) {
+                               if (vr_key) {
+                                       vr_key_pressed = vr_key;
+                                       set_amiga_key(vr_key_pressed, 1);
+                                       /* reset vr thrust */
+                                       thrust = 0;
+                               }
+                       } else {
+                               /* fire button */
                                set_joystick(-1, -1, -1, -1, 1);
-                       } else if (vr_key) {
-                               vr_key_pressed = vr_key;
-                               set_amiga_key(vr_key_pressed, 1);
                        }
                }
                if (!button_right_trigger && button_right_trigger_last) {
@@ -464,24 +505,48 @@ static void handle_vr_poses(void)
                        }
                } else {
                        double roll, pitch, yaw;
-                       double dist, dir, east, north;
+                       double tilt, dir, east, north;
                        int32_t move_east[4], move_north[4];
 
-                       /* check if we push the stick */
-                       dist = sqrt(stick_right_x * stick_right_x + stick_right_y * stick_right_y);
-                       if (dist > STICK_WALK_THRESHOLD) {
+                       tilt = sqrt(stick_right_x * stick_right_x + stick_right_y * stick_right_y);
+
+                       /* check if we rotate, walk or rest */
+                       if (we_rotate == 0 && we_walk == 0) {
+                               /* we rest */
+                               if (fabs(stick_right_y) > fabs(stick_right_x) && tilt > STICK_WALK_THRESHOLD)
+                                       we_walk = 1;
+                               if (fabs(stick_right_x) > fabs(stick_right_y) && tilt > STICK_ROTATE_THRESHOLD)
+                                       we_rotate = 1;
+                       } else if (we_walk) {
+                               /* we walk */
+                               if (tilt < STICK_WALK_THRESHOLD) {
+                                       we_walk = 0;
+                                       /* we need to stop right here and not continue until next rendering */
+                                       set_joystick(-1, -1, 0, 0, -1); /* stop y */
+                                       reset_joystick(); /* commit stop */
+                               }
+                       } else {
+                               /* we rotate */
+                               if (tilt < STICK_ROTATE_THRESHOLD)
+                                       we_rotate = 0;
+                       }
+
+                       /* if we walk */
+                       if (we_walk) {
                                /* get stick amplitude (dist) and direction (dir) */
-                               if (dist > 1.0)
-                                       dist = 1.0;
-                               dist = (dist - STICK_WALK_THRESHOLD) / (1.0 - STICK_WALK_THRESHOLD) * 40.0;
+                               if (tilt > 1.0)
+                                       tilt = 1.0;
+                               tilt = (tilt - STICK_WALK_THRESHOLD) / (1.0 - STICK_WALK_THRESHOLD) * 40.0;
                                dir = atan2(stick_right_x, stick_right_y);
+#if 0
                                /* use hand direction to get actual stick direction */
                                dir = fmod(dir - hand_right_yaw, M_PI * 2.0);
                                if (dir < -M_PI)
                                        dir += M_PI * 2.0;
                                if (dir > M_PI)
                                        dir -= M_PI * 2.0;
-                               /* flip directin, if we walk backwards */
+#endif
+                               /* flip direction, if we walk backwards */
                                if (dir > M_PI / 2.0 || dir < -M_PI / 2.0) {
                                        /* go backwards */
                                        set_joystick(-1, -1, 0, 1, -1);
@@ -491,10 +556,9 @@ static void handle_vr_poses(void)
                                        set_joystick(-1, -1, 1, 0, -1);
                                        joystick_set_y = 1;
                                }
-                               joystick_set_y = 1;
                                mercenary_get_orientation(&roll, &pitch, &yaw);
-                               east = sin(yaw - dir + M_PI) * dist;
-                               north = -cos(yaw - dir + M_PI) * dist;
+                               east = sin(yaw - dir + M_PI) * tilt;
+                               north = -cos(yaw - dir + M_PI) * tilt;
                                /* calculatate the integer positions of 4 steps */
                                move_east[0] = (int32_t)((east * 0.25) + 0.5);
                                move_north[0] = (int32_t)((north * 0.25) + 0.5);
@@ -514,21 +578,39 @@ static void handle_vr_poses(void)
                                /* the game takes 4 steps to move the player */
                                mercenary_vr_move(1, move_east, move_north, 256, 256);
                        }
+
+                       /* snap orientation to steps of 45 degrees */
+                       if (we_rotate == 1) {
+                               mercenary_get_orientation(&roll, &pitch, &yaw);
+                               yaw = round(yaw / degrees45) * degrees45;
+                               /* if we rotate: change orientation */
+                               if (stick_right_x > 0)
+                                       yaw -= degrees45;
+                               else
+                                       yaw += degrees45;
+                               /* rotate only once per stick movement */
+                               we_rotate = 2;
+                               mercenary_set_orientation(yaw);
+                       }
                }
                if (joystick_set_x_last && !joystick_set_x)
                        set_joystick(0, 0, -1, -1, -1);
                if (joystick_set_y_last && !joystick_set_y)
                        set_joystick(-1, -1, 0, 0, -1);
                /* thrust */
-               thrust = thrust_last;
                /* button to toggle between stop and escape */
                if (button_left_thumb && !button_left_thumb_last) {
                        /* 'escape' pressed */
-                       if (thrust_last == 0)
+                       if (thrust_last != -99)
                                thrust = -99;
                        else
                                thrust = 0;
                }
+               /* button to stop */
+               if (button_left_trigger && !button_left_trigger_last) {
+                       /* 'stop' pressed */
+                       thrust = 0;
+               }
                /* get stick to increment or decrement thrust */
                if (stick_left_y > STICK_THRUST_THRESHOLD)
                        increment = (stick_left_y - STICK_THRUST_THRESHOLD) / (1.0 - STICK_THRUST_THRESHOLD);
@@ -541,6 +623,9 @@ static void handle_vr_poses(void)
                if (increment) {
                        diff = current_time - last_time;
                        inc_count += increment * diff;
+                       /* if we are in 'escape' mode, we stop thrust first */
+                       if (thrust == -99)
+                               thrust = 0;
                        if (inc_count > 150.0 && thrust >= -10 && thrust < 10) {
                                thrust++;
                                inc_count = 0.0;
@@ -555,6 +640,12 @@ static void handle_vr_poses(void)
                last_time = current_time;
                /* send thrust change as keycodes */
                if (thrust_last != thrust) {
+                       /* if we were in escape mode, we stop us first and then apply the new code */
+                       if (thrust_last == -99) {
+                               set_amiga_key(KEYCODE_SPACE, 1);
+                               set_amiga_key(KEYCODE_SPACE, 0);
+                       }
+
                        if (thrust >= 10) {
                                set_amiga_key(KEYCODE_0, 1);
                                set_amiga_key(KEYCODE_0, 0);
@@ -674,6 +765,9 @@ static void main_loop(void)
        /* render result on window */
        while (!quit) {
 #ifdef HAVE_OVR
+               /* quit by OVR server */
+               if (should_quit_ovr())
+                       break;
                /* get vr poses */
                handle_vr_poses();
 #endif
@@ -736,6 +830,8 @@ static void main_loop(void)
                        /* for amiga speed: set delay by the number of cycles */
                        if (config_amiga_speed)
                                render_delay += (double)cycle_count / CPU_SPEED;
+                       /* reset joystick after rendering */
+                       reset_joystick();
                }
 
                /* STEP 2: transfer legacy image (or just benson) in memory to OpenGL texture */
@@ -774,6 +870,18 @@ static void main_loop(void)
                                        if (osd_timer - (int32_t)ticks_sdl() < 0)
                                                osd_timer = 0;
                                }
+#ifndef HAVE_OVR
+                               /* draw border around legacy render area */
+                               if (border_timer) {
+                                       opengl_render_color(1.0, 0.0, 0.0, 1.0);
+                                       opengl_render_line(160, -68, 256, 160, 68, 256, 0.0);
+                                       opengl_render_line(-160, -68, 256, -160, 68, 256, 0.0);
+                                       opengl_render_line(-160, 68, 256, 160, 68, 256, 0.0);
+                                       opengl_render_line(-160, -68, 256, 160, -68, 256, 0.0);
+                                       if (border_timer - (int32_t)ticks_sdl() < 0)
+                                               border_timer = 0;
+                               }
+#endif
                        }
                        /* setup viewport for legacy image and render image, if enabled */
                        /* also render legacy, if render_improved failed due to not (yet) available items */
@@ -927,27 +1035,44 @@ static void disk_read(int track, int __attribute__((unused)) side, uint32_t data
        if (!(track & 1)) {
                char filename[256];
                int gamesave_num = (track - 2) >> 1;
-               int got;
-               FILE *fp;
 
                memset(game_save, 0, sizeof(game_save)); /* clear so make the game fail, if we fail */
+
+               if (mission_disk == 0) {
+                       int got;
+                       FILE *fp;
+
 #if defined(_WIN32)
-               filename[0] = '\0';
+                       filename[0] = '\0';
 #else
-               sprintf(filename, "%s/%s/", home_dir, config_gamesave_dir);
-               mkdir(filename, 0777);
+                       sprintf(filename, "%s/%s/", home_dir, config_gamesave_dir);
+                       mkdir(filename, 0777);
 #endif
-               sprintf(filename + strlen(filename), "%d%s", gamesave_num, mercenary_gamesavesuffix);
-               fp = fopen(filename, "r");
-               if (!fp) {
+                       sprintf(filename + strlen(filename), "%d%s", gamesave_num, mercenary_gamesavesuffix);
+                       fp = fopen(filename, "r");
+                       if (!fp) {
 fail:
-                       print_info("failed to load game from '%s'\n", filename);
-                       goto copy;
+                               print_info("failed to load game from '%s'\n", filename);
+                               osd_info("load game", "FAILED!");
+                               goto copy;
+                       }
+                       got = fread(game_save, sizeof(game_save[0]), 2, fp);
+                       fclose(fp);
+                       if (got != 2)
+                               goto fail;
+                       sprintf(filename, "#%d", gamesave_num);
+                       osd_info("loaded game", filename);
+               } else {
+                       const uint8_t *mission_data = get_mission_disk(mission_disk, gamesave_num);
+                       sprintf(filename, "disk %d #%d", mission_disk, gamesave_num);
+                       if (!mission_data) {
+                               print_info("failed to load mission %s\n", filename);
+                               osd_info("load mission", "FAILED!");
+                               goto copy;
+                       }
+                       memcpy(game_save, mission_data, 0x1820 << 2);
+                       osd_info("loaded mission", filename);
                }
-               got = fread(game_save, sizeof(game_save[0]), 2, fp);
-               fclose(fp);
-               if (got != 2)
-                       goto fail;
        }
 
 copy:
@@ -958,6 +1083,12 @@ copy:
 /* game writes track with saved game */
 static void disk_write(int track, int __attribute__((unused)) side, uint32_t data, uint16_t length)
 {
+       /* cannot save on mission disk */
+       if (mission_disk) {
+               osd_info("", "not applicable");
+               return;
+       }
+
        /* skip sync info that is provided by game and only relevant for a real disk track */
        data += 0x200;
        length -= 0x200;
@@ -997,9 +1128,12 @@ static void disk_write(int track, int __attribute__((unused)) side, uint32_t dat
                if (!fp) {
 fail:
                        print_error("failed to save game to '%s'\n", filename);
+                       osd_info("save game", "FAILED!");
                        return;
                }
                print_info("Game state saved to '%s'\n", filename);
+               sprintf(filename, "#%d", gamesave_num);
+               osd_info("saved game", filename);
                wrote = fwrite(game_save, sizeof(game_save[0]), 2, fp);
                fclose(fp);
                if (wrote != 2)
@@ -1007,7 +1141,7 @@ fail:
        }
 }
 
-static int shift = 0, ctrl = 0;
+static int shift = 0, ctrl = 0, alt = 0;
 
 static void keyboard_sdl(int down, enum keycode keycode)
 {
@@ -1022,10 +1156,7 @@ static void keyboard_sdl(int down, enum keycode keycode)
        if (ctrl && down) {
                switch (keycode) {
                case KEYCODE_h:
-                       if (help_view == help_views)
-                               help_view = 0;
-                       else
-                               help_view++;
+                       toggle_help();
                        break;
                case KEYCODE_v:
                        config_video_filter ^= 1;
@@ -1056,6 +1187,7 @@ static void keyboard_sdl(int down, enum keycode keycode)
                                config_fov = FOV_JOLLY;
                        }
                        osd_info("Benson size", (config_benson_size == 0.5) ? "half" : "normal");
+                       border_timer = ticks_sdl() + 1500;
                        break;
                case KEYCODE_i:
                        if (!intro_skipped)
@@ -1063,6 +1195,27 @@ static void keyboard_sdl(int down, enum keycode keycode)
                        else
                                osd_info("", "not applicable");
                        break;
+               case KEYCODE_1:
+                       if (get_mission_disk(1, 1)) { /* just to check support */
+                               mission_disk = 1;
+                               osd_info("mission disk", "insert 1");
+                       } else
+                               osd_info("", "not applicable");
+                       break;
+               case KEYCODE_2:
+                       if (get_mission_disk(2, 1)) { /* just to check support */
+                               mission_disk = 2;
+                               osd_info("mission disk", "insert 2");
+                       } else
+                               osd_info("", "not applicable");
+                       break;
+               case KEYCODE_0:
+                       if (get_mission_disk(1, 1)) { /* just to check support */
+                               mission_disk = 0;
+                               osd_info("mission disk", "removed");
+                       } else
+                               osd_info("", "not applicable");
+                       break;
                case KEYCODE_c:
                        if (config_ctrl_c)
                                quit = 1;
@@ -1084,8 +1237,12 @@ static void keyboard_sdl(int down, enum keycode keycode)
                        disp_fov:
                        {
                                char text[16];
-                               sprintf(text, "%.2f", config_fov);
+                               if (config_fov < 63.5 || config_fov > 64.5)
+                                       sprintf(text, "%.2f", config_fov);
+                               else
+                                       sprintf(text, "%.2f (default)", config_fov);
                                osd_info("FOV", text);
+                               border_timer = ticks_sdl() + 1500;
                        }
 #endif
                        break;
@@ -1104,11 +1261,14 @@ static void keyboard_sdl(int down, enum keycode keycode)
                return;
        }
 
+       if (help_view == 1 && down) {
+               help_view = 2;
+               return;
+       }
+
        if (keycode == KEYCODE_PAUSE && down) {
-               if (help_view == help_views)
-                       help_view = 0;
-               else
-                       help_view++;
+               toggle_help();
+               return;
        }
 
        /* in help view we must not forward keypresses */
@@ -1124,6 +1284,14 @@ static void keyboard_sdl(int down, enum keycode keycode)
                set_amiga_key(keycode, down);
                shift = down;
                break;
+       case KEYCODE_LALT:
+               set_amiga_key(keycode, down);
+               alt = down;
+               break;
+       case KEYCODE_RALT:
+               set_amiga_key(keycode, down);
+               alt = down;
+               break;
        case KEYCODE_LEFT:
                if (shift && down) {
                        set_amiga_key(keycode, down);
@@ -1149,7 +1317,8 @@ static void keyboard_sdl(int down, enum keycode keycode)
                        break;
                }
                set_amiga_key(keycode, 0);
-               set_joystick(-1, -1, down, -1, -1);
+               if (!alt || down)
+                       set_joystick(-1, -1, down, 0, -1);
                break;
        case KEYCODE_DOWN:
                if (shift && down) {
@@ -1158,7 +1327,8 @@ static void keyboard_sdl(int down, enum keycode keycode)
                        break;
                }
                set_amiga_key(keycode, 0);
-               set_joystick(-1, -1, -1, down, -1);
+               if (!alt || down)
+                       set_joystick(-1, -1, 0, down, -1);
                break;
        case KEYCODE_END:
                set_joystick(-1, -1, -1, -1, down);
@@ -1167,10 +1337,43 @@ static void keyboard_sdl(int down, enum keycode keycode)
                set_amiga_key(KEYCODE_HELP, down);
                break;
        default:
+               /* reset vr thrust */
+#ifdef HAVE_OVR
+               thrust_last = 0;
+#endif
                set_amiga_key(keycode, down);
        }
 }
 
+static double joystick_last_x = 0, joystick_last_y = 0;
+static int joystick_last_fire = 0;
+
+static void joystick_sdl(double x, double y, int fire)
+{
+       int left = -1, right = -1, up = -1, down = -1;
+
+       if (x <= -0.5 && joystick_last_x > -0.5)
+               left = 1;
+       if (x > -0.5 && joystick_last_x <= -0.5)
+               left = 0;
+       if (x >= 0.5 && joystick_last_x < 0.5)
+               right = 1;
+       if (x < 0.5 && joystick_last_x >= 0.5)
+               right = 0;
+       if (y <= -0.5 && joystick_last_y > -0.5)
+               up = 1;
+       if (y > -0.5 && joystick_last_y <= -0.5)
+               up = 0;
+       if (y >= 0.5 && joystick_last_y < 0.5)
+               down = 1;
+       if (y < 0.5 && joystick_last_y >= 0.5)
+               down = 0;
+       set_joystick(left, right, up, down, fire);
+       joystick_last_x = x;
+       joystick_last_y = y;
+       joystick_last_fire = fire;
+}
+
 void audio_sdl(float *data, int length)
 {
        int fill, s;
@@ -1196,12 +1399,16 @@ void audio_sdl(float *data, int length)
        sound_buffer_readp =(sound_buffer_readp + length) % sound_buffer_size;
 }
 
+/* used for geier counter */
 void sound_irq(void)
 {
        /* trigger and execute IRQ 4 = sound */
        execute_cpu(4, NULL);
 }
 
+extern uint8_t mercenary_splash_palette[][3];
+extern char mercenary_splash_pixels[];
+
 int main(int argc, char *argv[])
 {
        int rc;
@@ -1290,7 +1497,7 @@ int main(int argc, char *argv[])
        window_width = (config_debug_opengl) ? SCREEN_WIDTH / 3 * 2 : SCREEN_WIDTH;
        window_height = (config_debug_opengl) ? SCREEN_HEIGHT / 3 * 4 : SCREEN_HEIGHT;
 #endif
-       rc = init_sdl(argv[0], window_width, window_height, SOUND_SAMPLERATE, sdl_sound_chunk, keyboard_sdl, audio_sdl, resize_window, multisampling, vbl_sync, vr);
+       rc = init_sdl(mercenary_name, window_width, window_height, SOUND_SAMPLERATE, sdl_sound_chunk, keyboard_sdl, config_joystick, config_joystick_x, config_joystick_y, config_joystick_fire, joystick_sdl, audio_sdl, resize_window, multisampling, vbl_sync, vr);
        if (rc < 0)
                goto done;
 #ifdef HAVE_OVR
@@ -1312,11 +1519,16 @@ int main(int argc, char *argv[])
        rc = init_opengl_osd(1, OSD_WIDTH, OSD_HEIGHT);
        if (rc < 0)
                goto done;
-       help_osd[0] = text_alloc(IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, HELP_ALPHA);
+       help_osd[0] = text_alloc(IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, 0x00);
        if (!help_osd[0])
                goto done;
-       text_render(help_osd[0], IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, mercenary_name, HELP_ALPHA, 3, (double)(80 - strlen(mercenary_name)) / 2.0, 1, 1);
-       text_render(help_osd[0], IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2,
+       text_insert_image(help_osd[0], mercenary_splash_palette, mercenary_splash_pixels, 0xff);
+       help_views++;
+       help_osd[1] = text_alloc(IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, HELP_ALPHA);
+       if (!help_osd[1])
+               goto done;
+       text_render(help_osd[1], IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, mercenary_name, HELP_ALPHA, 3, (double)(80 - strlen(mercenary_name)) / 2.0, 1, 1);
+       text_render(help_osd[1], IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2,
                "Emulation:\n"
                "        Press `PAUSE' to toggle this help screen on or off.\n"
                "        Press `CTRL' + `F' to toggle between full screen / window mode.\n"
@@ -1327,8 +1539,9 @@ int main(int argc, char *argv[])
                "        Press `CTRL' + `A' to toggle audio filter on / off.\n"
                "        Press `CTRL' + `+' or `-' to change field-of-view (OpenGL).\n"
                "        Press `CTRL' + `I' to skip intro (approaching to Eris).\n"
+               "        Press `CTRL' + `1' or `2' to insert or `0' to remove mission disk.\n"
 #ifdef HAVE_OVR
-               "        Press `CTRL' + `O' (or both triggers) to reset observer position.\n"
+               "        Press `CTRL' + `O' to reset observer position.\n"
 #endif
                "\n"
                "Answer to a Question:\n"
@@ -1361,32 +1574,33 @@ int main(int argc, char *argv[])
                "        Press `INSERT' to loading and saving options.\n"
                "        Press `ENTER' to pause game, other key to continue.\n"
                ,HELP_ALPHA, 1, 2, 5, 0);
+               help_views++;
 #ifdef HAVE_OVR
-       help_osd[1] = text_alloc(IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, HELP_ALPHA);
-       if (!help_osd[1])
+       help_osd[2] = text_alloc(IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, HELP_ALPHA);
+       if (!help_osd[2])
                goto done;
-       text_render(help_osd[1], IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, mercenary_name, HELP_ALPHA, 3, (double)(80 - strlen(mercenary_name)) / 2.0, 1, 1);
-       text_render(help_osd[1], IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2,
+       text_render(help_osd[2], IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, mercenary_name, HELP_ALPHA, 3, (double)(80 - strlen(mercenary_name)) / 2.0, 1, 1);
+       text_render(help_osd[2], IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2,
                "Emulation using Controller:\n"
-               "        Press `Enter' button to toggle this help screen on or off.\n"
-               "        To have a virtual keyboard, point below benson (control pannel).\n"
+               "        Press `A' or 'X' button to toggle this help screen on or off.\n"
+               "        Press 'B' or 'Y' button to reset observer position. (importaint!)\n"
+               "        To have a virtual keyboard, point right hand below control pannel.\n"
                "        Point to a key on keyboard. The key will highlight.\n"
-               "        Pull `Trigger' on right controller enter the highlighted key.\n"
-               "        Pull `Trigger' on both controllers to reset observer position.\n"
+               "        Pull `Trigger' on right controller to enter the highlighted key.\n"
                "\n"
                "Walking / Driving / Flying using Controller:\n"
                "        Use thumb stick on right controller, to move player / craft.\n"
-               "        Point right controller towards the direction to walk to.\n"
-               "        Press thumb stick to change orientation to that direction.\n"
+               "        Move thumb stick forth and back to walk, left and right to rotate.\n"
+               "        Pull `Trigger' on right controller to change orientation.\n"
                "        Press `X' button to board, `Y' button to leave.\n"
                "        Move thumb stick on left controller to drive/fly forward or backward.\n"
-               "        Press thumb stick on left controller stop craft.\n"
-               "        Press thumb stick on left controller for escape sequence, after stop.\n"
+               "        Pull `Trigger' on left controller stop craft.\n"
+               "        Press thumb stick on left controller for escape sequence and stop it.\n"
                "        Pull `Trigger' on right controller to fire.\n"
                "\n"
-               "For all other game function, use the emulated keyboards!\n"
+               "For all other game function, use the virtual or real keyboard!\n"
                ,HELP_ALPHA, 1, 2, 5, 0);
-       help_views = 2;
+               help_views++;
 #endif
        info_osd = text_alloc(OSD_WIDTH, OSD_HEIGHT, 0x00);
        if (!info_osd)
@@ -1421,10 +1635,11 @@ done:
                free(sound_buffer);
        if (image)
                free(image);
-       if (help_osd[0])
-               free(help_osd[0]);
-       if (help_osd[1])
-               free(help_osd[1]);
+       while (help_views) {
+               help_views--;
+               if (help_osd[help_views])
+                       free(help_osd[help_views]);
+       }
        if (info_osd)
                free(info_osd);