OVR: Make adjustments to the game's input using controllers
[mercenary-reloaded.git] / src / mercenary / main.c
index aea09f1..d9fb1c7 100644 (file)
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include "../../include/keycodes.h"
 #include "../libsdl/sdl.h"
 #ifdef HAVE_OVR
 #include "../libovr/ovr.h"
+#include "../libovr/keyboard.h"
 #endif
 #include "../libsdl/opengl.h"
 #include "../libsdl/print.h"
 #define FOV_JOLLY      80.0
 #define FOV_MAX                170.0
 
+#define STICK_WALK_THRESHOLD 0.3 /* move the stick until the player moves */
+#define STICK_ROTATE_THRESHOLD 0.3 /* move the stick until the player rotates */
+#define STICK_THRUST_THRESHOLD 0.1 /* move the stick until the craft moves */
+
 static int config_ctrl_c = 0;
 static int config_amiga_speed = 0; /* fast speed */
 static double config_fps = 16.0;
@@ -56,15 +63,27 @@ static int config_video_filter = 1;
 static int config_audio_filter = 1;
 static int config_render = 1; /* opengl render */
 static int config_skip_intro = 0;
-static int config_multisampling = 16;
-static double config_fov = FOV_NOVAGEN;
-static double config_monitor_distance = 31.5; /* inch */
+static int config_multisampling = 8;
+#ifdef HAVE_OVR
+static double config_benson_size = 0.5;
+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 = 41.5; /* inch */
+#ifdef HAVE_OVR
+static double config_keyboard_distance = 40.5; /* inch */
+static double config_keyboard_height = -26.0; /* inch */
+#endif
 static int config_debug_transparent = 0;
 static int config_debug_opengl = 0;
 /* render improvements */
 static int config_improve_extend_roads = 1;    /* set to 1 to extend roads */
 static int config_improve_smooth_planets = 1;  /* set to 1 to rotate planets smoothly */
+static int config_improve_stars_rotation = 1;  /* set to 1 to improve star rendering */
+static int config_improve_fix_sky_rotation = 0;        /* set to 1 to fix sky rotation */
+static int config_improve_round_planets = 0;   /* set to 1 to make planets exactly round */
 
 #define CPU_SPEED      7093790.0;
 
@@ -89,12 +108,13 @@ static int config_improve_smooth_planets = 1;      /* set to 1 to rotate planets smoo
 static uint8_t *memory = NULL;
 static uint8_t *stop_event = NULL;
 static uint8_t *image = NULL;
-static uint8_t *help_osd = NULL;
+static uint8_t *help_osd[2] = { 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   672
+#define SCREEN_WIDTH   1344
 #define SCREEN_HEIGHT  800
 #else
 #define SCREEN_WIDTH   (320*3)
@@ -130,6 +150,7 @@ int parse_args(int argc, char *argv[])
 
        while (argc > i) {
                if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
+                       /*          |                                                                             | */
                        print_info("Usage: %s\n", argv[0]);
                        print_info(" -s --render-speed original | fast\n");
                        print_info("        Set speed of rendering to original Amiga or fast speed.\n");
@@ -149,6 +170,28 @@ 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("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");
+                       print_info("        resolution, but for OpenGL we want equal width all the way.\n");
+                       print_info("        This requires OpenGL rendering. (default = %d)\n", config_improve_extend_roads);
+                       print_info("    --smooth-planets 1 | 0\n");
+                       print_info("        Planet's rotation uses integer value, so they judder in the original\n");
+                       print_info("        game rendering. This option requires OpenGL rendering. (default = %d)\n", config_improve_smooth_planets);
+                       print_info("    --stars-rotation 1 | 0\n");
+                       print_info("        Stars are rendered originally by using a fast routine. Use this option\n");
+                       print_info("        to make them render on a sphere. They also rotate with the sky then.\n");
+                       print_info("        This requires OpenGL rendering and is default with VR. (default = %d)\n", config_improve_stars_rotation);
+                       print_info("    --fix-sky-rotation 1 | 0\n");
+                       print_info("        Sky rotates in the wrong direction, if flying over planet. This is\n");
+                       print_info("        fixed by rotating all sky object by 180 degrees. The original (wrong)\n");
+                       print_info("        rotation is default, because it preserves the game's spirit!\n");
+                       print_info("        This option requires OpenGL rendering. (default = %d)\n", config_improve_fix_sky_rotation);
+                       print_info("    --round-planets 1 | 0\n");
+                       print_info("        The original game renders planets and explosion debris horizontally\n");
+                       print_info("        stretched. This compensates the vertical stretch of NTSC vs PAL video.\n");
+                       print_info("        The original (stretched) sphere makes the game authentic.\n");
+                       print_info("        This option requires OpenGL rendering. (default = %d)\n", config_improve_round_planets);
                        print_info("Debug options:\n");
                        print_info(" -o --debug-opengl\n");
                        print_info("        Use split screen to display both Amiga and OpenGL rendering.\n");
@@ -251,6 +294,36 @@ illegal_parameter:
                if (!strcmp(argv[i], "-i") || !strcmp(argv[i], "--skip-intro")) {
                        config_skip_intro = 1;
                } else
+               if (!strcmp(argv[i], "--extend-roads")) {
+                       i++;
+                       if (argc == i)
+                               goto missing_parameter;
+                       config_improve_extend_roads = atoi(argv[i]);
+               } else
+               if (!strcmp(argv[i], "--smooth-planets")) {
+                       i++;
+                       if (argc == i)
+                               goto missing_parameter;
+                       config_improve_smooth_planets = atoi(argv[i]);
+               } else
+               if (!strcmp(argv[i], "--stars-rotation")) {
+                       i++;
+                       if (argc == i)
+                               goto missing_parameter;
+                       config_improve_stars_rotation = atoi(argv[i]);
+               } else
+               if (!strcmp(argv[i], "--fix-sky-rotation")) {
+                       i++;
+                       if (argc == i)
+                               goto missing_parameter;
+                       config_improve_fix_sky_rotation = atoi(argv[i]);
+               } else
+               if (!strcmp(argv[i], "--round-planets")) {
+                       i++;
+                       if (argc == i)
+                               goto missing_parameter;
+                       config_improve_round_planets = atoi(argv[i]);
+               } else
                if (!strcmp(argv[i], "-o") || !strcmp(argv[i], "--debug-opengl")) {
                        config_debug_opengl = 1;
                } else
@@ -269,12 +342,272 @@ illegal_parameter:
        return 0;
 }
 
+static void osd_info(const char *param, const char *value)
+{
+       char line[41] = "                                        ";
+
+       if (param[0]) {
+               strncpy(line + 21 - strlen(param), param, strlen(param));
+               line[22] = ':';
+       }
+       if (strlen(value) > 15) {
+               print_error("string too long\n");
+               return;
+       }
+       strncpy(line + 25, value, strlen(value));
+       text_render(info_osd, OSD_WIDTH, OSD_HEIGHT, line, 0x00, 4, 0, 0, 1);
+       osd_timer = ticks_sdl() + 2500;
+}
+
 static void resize_window(int width, int height)
 {
        window_width = width;
        window_height = height;
 }
 
+#ifdef HAVE_OVR
+static int __attribute__((unused)) button_a_last = 0, button_b_last = 0, button_x_last = 0, button_y_last = 0, button_menu_last = 0, button_left_trigger_last = 0, button_right_trigger_last = 0, button_left_thumb_last = 0, button_right_thumb_last = 0;
+static double __attribute__((unused)) stick_left_x_last = 0.0, stick_left_y_last = 0.0, stick_right_x_last = 0.0, stick_right_y_last = 0.0;
+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 void handle_vr_poses(void)
+{
+       int button_a = 0, button_b = 0, button_x = 0, button_y = 0, button_menu = 0, button_left_trigger = 0, button_right_trigger = 0, button_left_thumb = 0, button_right_thumb = 0;
+       double hand_right_x = 0.0, hand_right_y = 0.0, hand_right_z = 0.0;
+       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 = 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) {
+               /* menu toggle */
+               if (help_view == help_views)
+                       help_view = 0;
+               else
+                       help_view++;
+       }
+       if (button_left_thumb) {
+               /* 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);
+                       /* important: get rid of old state */
+                       thrust = 0;
+               }
+               if (button_y && !button_y_last) {
+                       /* 'leave' pressed */
+                       set_amiga_key(KEYCODE_l, 1);
+                       set_amiga_key(KEYCODE_l, 0);
+               }
+               if (button_right_trigger && !button_right_trigger_last) {
+                       /* trigger pressed */
+                       if (keyboard_on) {
+                               if (vr_key) {
+                                       vr_key_pressed = vr_key;
+                                       set_amiga_key(vr_key_pressed, 1);
+                               }
+                       } else
+                       if (mercenary_get_info_walking()) {
+                               /* change orientation */
+                               double roll, pitch, yaw;
+                               mercenary_get_orientation(&roll, &pitch, &yaw);
+                               mercenary_set_orientation(yaw + hand_right_yaw);
+                       } else {
+                               /* fire button */
+                               set_joystick(-1, -1, -1, -1, 1);
+                       }
+               }
+               if (!button_right_trigger && button_right_trigger_last) {
+                       /* trigger released */
+                       set_joystick(-1, -1, -1, -1, 0);
+                       if (vr_key_pressed) {
+                               set_amiga_key(vr_key_pressed, 0);
+                               vr_key_pressed = 0;
+                       }
+               }
+               /* joystick */
+               /* reset moving state, so the game is not influenced before we want to */
+               mercenary_vr_move(0, NULL, NULL, 256, 256);
+               joystick_set_x = 0;
+               joystick_set_y = 0;
+               if (!mercenary_get_info_walking()) {
+                       /* turn right and left when not walking */
+                       if (stick_right_x > STICK_ROTATE_THRESHOLD) {
+                               set_joystick(0, 1, -1, -1, -1);
+                               joystick_set_x = 1;
+                       } else
+                       if (stick_right_x < -STICK_ROTATE_THRESHOLD) {
+                               set_joystick(1, 0, -1, -1, -1);
+                               joystick_set_x = 1;
+                       }
+                       /* pitch craft */
+                       if (stick_right_y > STICK_WALK_THRESHOLD) {
+                               set_joystick(-1, -1, 1, 0, -1);
+                               joystick_set_y = 1;
+                       } else
+                       if (stick_right_y < -STICK_WALK_THRESHOLD) {
+                               set_joystick(-1, -1, 0, 1, -1);
+                               joystick_set_y = 1;
+                       }
+                       if (joystick_set_x || joystick_set_y) {
+                               mercenary_vr_move(0, NULL, NULL,
+                                       256,
+                                       (int)(256.0 * fabs(stick_right_y) - STICK_WALK_THRESHOLD / (1.0 - STICK_WALK_THRESHOLD) + 0.5));
+                       }
+               } else {
+                       double roll, pitch, yaw;
+                       double dist, 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) {
+                               /* 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;
+                               dir = atan2(stick_right_x, stick_right_y);
+                               /* 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 */
+                               if (dir > M_PI / 2.0 || dir < -M_PI / 2.0) {
+                                       /* go backwards */
+                                       set_joystick(-1, -1, 0, 1, -1);
+                                       joystick_set_y = 1;
+                               } else {
+                                       /* go forward */
+                                       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;
+                               /* 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);
+                               move_east[1] = (int32_t)((east * 0.5) + 0.5);
+                               move_north[1] = (int32_t)((north * 0.5) + 0.5);
+                               move_east[2] = (int32_t)((east * 0.75) + 0.5);
+                               move_north[2] = (int32_t)((north * 0.75) + 0.5);
+                               move_east[3] = (int32_t)(east + 0.5);
+                               move_north[3] = (int32_t)(north + 0.5);
+                               /* calculate the delta between each of the 4 step */
+                               move_east[3] -= move_east[2];
+                               move_north[3] -= move_north[2];
+                               move_east[2] -= move_east[1];
+                               move_north[2] -= move_north[1];
+                               move_east[1] -= move_east[0];
+                               move_north[1] -= move_north[0];
+                               /* the game takes 4 steps to move the player */
+                               mercenary_vr_move(1, move_east, move_north, 256, 256);
+                       }
+               }
+               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 */
+               /* button to toggle between stop and escape */
+               if (button_left_trigger && !button_left_trigger_last) {
+                       /* 'escape' pressed */
+                       if (thrust_last == 0)
+                               thrust = -99;
+                       else
+                               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);
+               else
+               if (stick_left_y < -STICK_THRUST_THRESHOLD)
+                       increment = (stick_left_y + STICK_THRUST_THRESHOLD) / (1.0 - STICK_THRUST_THRESHOLD);
+               else
+                       increment = 0;
+               current_time = ticks_sdl();
+               if (increment) {
+                       diff = current_time - last_time;
+                       inc_count += increment * diff;
+                       if (inc_count > 150.0 && thrust >= -10 && thrust < 10) {
+                               thrust++;
+                               inc_count = 0.0;
+                       }
+                       if (inc_count < -150.0 && thrust <= 10 && thrust > -10) {
+                               thrust--;
+                               inc_count = 0.0;
+                       }
+               } else {
+                       inc_count = 0;
+               }
+               last_time = current_time;
+               /* send thrust change as keycodes */
+               if (thrust_last != thrust) {
+                       if (thrust >= 10) {
+                               set_amiga_key(KEYCODE_0, 1);
+                               set_amiga_key(KEYCODE_0, 0);
+                       } else
+                       if (thrust <= -10 && thrust > -99) {
+                               set_amiga_key(KEYCODE_F10, 1);
+                               set_amiga_key(KEYCODE_F10, 0);
+                       } else
+                       if (thrust <= -99) {
+                               set_amiga_key(KEYCODE_ESCAPE, 1);
+                               set_amiga_key(KEYCODE_ESCAPE, 0);
+                       } else
+                       if (thrust > 0) {
+                               set_amiga_key(KEYCODE_1 + thrust - 1, 1);
+                               set_amiga_key(KEYCODE_1 + thrust - 1, 0);
+                       } else
+                       if (thrust < 0) {
+                               set_amiga_key(KEYCODE_F1 - thrust - 1, 1);
+                               set_amiga_key(KEYCODE_F1 - thrust - 1, 0);
+                       } else {
+                               set_amiga_key(KEYCODE_SPACE, 1);
+                               set_amiga_key(KEYCODE_SPACE, 0);
+                       }
+               }
+       }
+       button_a_last = button_a;
+       button_b_last = button_b;
+       button_x_last = button_x;
+       button_y_last = button_y;
+       button_menu_last = button_menu;
+       button_left_trigger_last = button_left_trigger;
+       button_right_trigger_last = button_right_trigger;
+       button_left_thumb_last = button_left_thumb;
+       button_right_thumb_last = button_right_thumb;
+       stick_left_x_last = stick_left_x;
+       stick_left_y_last = stick_left_y;
+       stick_right_x_last = stick_right_x;
+       stick_right_y_last = stick_right_y;
+       thrust_last = thrust;
+       joystick_set_x_last = joystick_set_x;
+       joystick_set_y_last = joystick_set_y;
+
+       /* we must handle keyboard after toggeling keyboard_on,
+        * so that keyboard_on will not change until keyboard is rendered */
+       vr_key = 0;
+       keyboard_on = handle_vr_keyboard(hand_right_x, hand_right_y, hand_right_z, hand_right_yaw, hand_right_pitch, &vr_key);
+}
+#endif
+
 static void skip_intro(void)
 {
        int event;
@@ -309,6 +642,14 @@ static void skip_intro(void)
 
 static void special_event(int event)
 {
+#ifdef HAVE_OVR
+       /* handle VR events */
+       if (event == STOP_AT_PATCH_VR) {
+               mercenary_patch_vr();
+               return;
+       }
+#endif
+       /* handle events to improve rendering */
        if (render_improved)
                render_capture_event(event);
 }
@@ -324,12 +665,22 @@ static void main_loop(void)
        double render_delay = 0.0;
        uint32_t palette_address;
        uint16_t palette[16];
+       int all_white = 0;
+#ifdef HAVE_OVR
        int eye;
+       int vr = 1;
+#else
+       int vr = 0;
+#endif
 
        last_time = ticks_sdl();
 
        /* render result on window */
        while (!quit) {
+#ifdef HAVE_OVR
+               /* get vr poses */
+               handle_vr_poses();
+#endif
                /* if we are in interstellar fligt, we use 50 Hz */
                /* if we are approaching to Eris Space Port, we use 10 Hz */
                /* else we use whatever frame rate the user wants */
@@ -368,7 +719,7 @@ static void main_loop(void)
                        frame_render = 0;
                        /* start capturing for improved graphics */
                        if (render_improved)
-                               render_capture_start(config_fov, config_improve_extend_roads, config_improve_smooth_planets, config_debug_transparent);
+                               render_capture_start(config_fov, config_improve_extend_roads, config_improve_smooth_planets, config_improve_stars_rotation, config_improve_fix_sky_rotation, config_improve_round_planets, config_debug_transparent);
 
                        /* execute until the rendered image is ready (wait for VBL) */
                        cycle_count = 0;
@@ -394,19 +745,16 @@ static void main_loop(void)
                /* STEP 2: transfer legacy image (or just benson) in memory to OpenGL texture */
 #ifdef HAVE_OVR
                begin_render_ovr();
+
                for (eye = 0; eye < 2; eye++) {
-                       /* viewport and frustum is set here */
-                       begin_render_ovr_eye(eye);
+                       double camera_x, camera_y, camera_z;
+                       /* begin of rendering eye, viewport and frustum is set here */
+                       begin_render_ovr_eye(eye, &camera_x, &camera_y, &camera_z);
 #else
-               eye = 0;
                {
 #endif
                        /* clear screen */
-#ifdef HAVE_OVR
-                       opengl_clear(1);
-#else
-                       opengl_clear(0);
-#endif
+                       opengl_clear(vr);
                        /* render benson + osd ontop of improved opengl rendering, if enabled */
                        if (render_improved) {
 #ifndef HAVE_OVR
@@ -416,18 +764,17 @@ static void main_loop(void)
                                /* render improved graphics, interpolate, if required,
                                 * if no item list is available, for legacy rendering
                                 */
-#ifdef HAVE_OVR
-                               rc = render_all_items((config_amiga_speed) ? 1.0 : frame_time, 1);
-#else
-                               rc = render_all_items((config_amiga_speed) ? 1.0 : frame_time, 0);
-#endif
+                               if (all_white)
+                                       rc = render_all_white(vr);
+                               else
+                                       rc = render_all_items((config_amiga_speed) ? 1.0 : frame_time, vr);
                                if (rc)
                                        goto goto_legacy;
-                               opengl_blit_image(image, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, 1, config_fov, config_monitor_distance, benson_size);
+                               opengl_blit_image(image, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, 1, config_fov, config_monitor_distance, benson_size, vr);
                                if (help_view)
-                                       opengl_blit_osd(0, help_osd, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, config_fov, config_monitor_distance, benson_size, 1.0, 1.0, 0.0, 0.0);
+                                       opengl_blit_osd(0, help_osd[help_view - 1], config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, (vr) ? FOV_NOVAGEN : config_fov, config_monitor_distance, (vr) ? 1.0 : benson_size, 1.0, 1.0, 0.0, 0.0);
                                if (osd_timer) {
-                                       opengl_blit_osd(1, info_osd, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, config_fov, config_monitor_distance, benson_size, 0.5, 0.04, 0.5, -0.95);
+                                       opengl_blit_osd(1, info_osd, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, (vr) ? FOV_NOVAGEN : config_fov, config_monitor_distance, (vr) ? 1.0 : benson_size, 0.5, 0.04, 0.5, -0.95);
                                        if (osd_timer - (int32_t)ticks_sdl() < 0)
                                                osd_timer = 0;
                                }
@@ -443,18 +790,23 @@ static void main_loop(void)
                                        emul_video(image, memory, palette, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_DIWSTART, chipreg, 0, BENSON_AT_LINE, double_pixel_size);
 #ifndef HAVE_OVR
                                /* viewport and frustum is set here */
-                               opengl_viewport(window_width, window_height, (debug_opengl) ? 1 : 0, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, config_fov, 1.0);
+                               opengl_viewport(window_width, window_height, (debug_opengl) ? 1 : 0, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, FOV_NOVAGEN, 1.0);
 #endif
-                               opengl_blit_image(image, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, 0, config_fov, config_monitor_distance, 1.0);
+                               opengl_blit_image(image, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, 0, FOV_NOVAGEN, config_monitor_distance, 1.0, 0);
                                if (help_view)
-                                       opengl_blit_osd(0, help_osd, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, config_fov, config_monitor_distance, 1.0, 1.0, 1.0, 0.0, 0.0);
+                                       opengl_blit_osd(0, help_osd[help_view - 1], config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, FOV_NOVAGEN, config_monitor_distance, 1.0, 1.0, 1.0, 0.0, 0.0);
                                if (osd_timer) {
-                                       opengl_blit_osd(1, info_osd, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, config_fov, config_monitor_distance, 1.0, 0.5, 0.04, 0.5, -0.95);
+                                       opengl_blit_osd(1, info_osd, config_video_filter, (double_pixel_size) ? BENSON_AT_LINE * 2 : BENSON_AT_LINE, FOV_NOVAGEN, config_monitor_distance, 1.0, 0.5, 0.04, 0.5, -0.95);
                                        if (osd_timer - (int32_t)ticks_sdl() < 0)
                                                osd_timer = 0;
                                }
                        }
 #ifdef HAVE_OVR
+                       /* render keyboard */
+                       if (keyboard_on)
+                               render_vr_keyboard(camera_x, camera_y);
+
+                       /* end of rendering eye */
                        end_render_ovr_eye(eye);
                }
                /* at this point we are ready with our image, so we display */
@@ -495,7 +847,7 @@ static void main_loop(void)
                                /* transfer benson without game view
                                 * because we only got benson refreshed during VBL IRQ
                                 */
-                               emul_video(image, memory, palette, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_DIWSTART, chipreg, BENSON_AT_LINE, IMAGE_HEIGHT, double_pixel_size);
+                               all_white = emul_video(image, memory, palette, IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_DIWSTART, chipreg, BENSON_AT_LINE, IMAGE_HEIGHT, double_pixel_size);
                                /* render sound to sound buffer
                                 * buffer pointer read and write is atomic, so no locking required!
                                 */
@@ -659,23 +1011,6 @@ fail:
        }
 }
 
-static void osd_info(const char *param, const char *value)
-{
-       char line[41] = "                                        ";
-
-       if (param[0]) {
-               strncpy(line + 21 - strlen(param), param, strlen(param));
-               line[22] = ':';
-       }
-       if (strlen(value) > 15) {
-               print_error("string too long\n");
-               return;
-       }
-       strncpy(line + 25, value, strlen(value));
-       text_render(info_osd, OSD_WIDTH, OSD_HEIGHT, line, 0x00, 4, 0, 0, 1);
-       osd_timer = ticks_sdl() + 2500;
-}
-
 static int shift = 0, ctrl = 0;
 
 static void keyboard_sdl(int down, enum keycode keycode)
@@ -691,7 +1026,10 @@ static void keyboard_sdl(int down, enum keycode keycode)
        if (ctrl && down) {
                switch (keycode) {
                case KEYCODE_h:
-                       help_view ^= 1;
+                       if (help_view == help_views)
+                               help_view = 0;
+                       else
+                               help_view++;
                        break;
                case KEYCODE_v:
                        config_video_filter ^= 1;
@@ -733,15 +1071,18 @@ static void keyboard_sdl(int down, enum keycode keycode)
                        if (config_ctrl_c)
                                quit = 1;
                        break;
-               case KEYCODE_n:
+               case KEYCODE_o:
 #ifdef HAVE_OVR
-                       normalize_observer_ovr();
-                       osd_info("", "change height");
+                       reset_observer_ovr();
+                       osd_info("", "reset observer");
 #else
                        osd_info("", "not applicable");
 #endif
                        break;
                case KEYCODE_KP_PLUS:
+#ifdef HAVE_OVR
+                       osd_info("", "not applicable");
+#else
                        if (config_fov / 1.2 >= FOV_MIN)
                                config_fov /= 1.2;
                        disp_fov:
@@ -750,149 +1091,87 @@ static void keyboard_sdl(int down, enum keycode keycode)
                                sprintf(text, "%.2f", config_fov);
                                osd_info("FOV", text);
                        }
+#endif
                        break;
                case KEYCODE_KP_MINUS:
+#ifdef HAVE_OVR
+                       osd_info("", "not applicable");
+#else
                        if (config_fov * 1.2 <= FOV_MAX)
                                config_fov *= 1.2;
                        goto disp_fov;
+#endif
+                       break;
                default: break;
                }
                /* do not pass keys to game while holding CTRL */
                return;
        }
 
-       if (keycode == KEYCODE_PAUSE && down)
-               help_view ^= 1;
+       if (keycode == KEYCODE_PAUSE && down) {
+               if (help_view == help_views)
+                       help_view = 0;
+               else
+                       help_view++;
+       }
 
-       /* in help view we don't need to forward keypresses */
+       /* in help view we must not forward keypresses */
        if (help_view)
                return;
 
        switch (keycode) {
        case KEYCODE_LSHIFT:
-               set_key("LSH", down);
+               set_amiga_key(keycode, down);
                shift = down;
                break;
        case KEYCODE_RSHIFT:
-               set_key("RSH", down);
+               set_amiga_key(keycode, down);
                shift = down;
                break;
        case KEYCODE_LEFT:
                if (shift && down) {
-                       set_key("LF", down);
+                       set_amiga_key(keycode, down);
                        set_joystick(-1, -1, -1, -1, -1);
                        break;
                }
-               set_key("LF", 0);
+               set_amiga_key(keycode, 0);
                set_joystick(down, -1, -1, -1, -1);
                break;
        case KEYCODE_RIGHT:
                if (shift && down) {
-                       set_key("RT", down);
+                       set_amiga_key(keycode, down);
                        set_joystick(-1, -1, -1, -1, -1);
                        break;
                }
-               set_key("RT", 0);
+               set_amiga_key(keycode, 0);
                set_joystick(-1, down, -1, -1, -1);
                break;
        case KEYCODE_UP:
                if (shift && down) {
-                       set_key("UP", down);
+                       set_amiga_key(keycode, down);
                        set_joystick(-1, -1, -1, -1, -1);
                        break;
                }
-               set_key("UP", 0);
+               set_amiga_key(keycode, 0);
                set_joystick(-1, -1, down, -1, -1);
                break;
        case KEYCODE_DOWN:
                if (shift && down) {
-                       set_key("DN", down);
+                       set_amiga_key(keycode, down);
                        set_joystick(-1, -1, -1, -1, -1);
                        break;
                }
-               set_key("DN", 0);
+               set_amiga_key(keycode, 0);
                set_joystick(-1, -1, -1, down, -1);
                break;
        case KEYCODE_END:
                set_joystick(-1, -1, -1, -1, down);
                break;
-
-       case KEYCODE_a: set_key("A", down); break;
-       case KEYCODE_b: set_key("B", down); break;
-       case KEYCODE_c: set_key("C", down); break;
-       case KEYCODE_d: set_key("D", down); break;
-       case KEYCODE_e: set_key("E", down); break;
-       case KEYCODE_f: set_key("F", down); break;
-       case KEYCODE_g: set_key("G", down); break;
-       case KEYCODE_h: set_key("H", down); break;
-       case KEYCODE_i: set_key("I", down); break;
-       case KEYCODE_j: set_key("J", down); break;
-       case KEYCODE_k: set_key("K", down); break;
-       case KEYCODE_l: set_key("L", down); break;
-       case KEYCODE_m: set_key("M", down); break;
-       case KEYCODE_n: set_key("N", down); break;
-       case KEYCODE_o: set_key("O", down); break;
-       case KEYCODE_p: set_key("P", down); break;
-       case KEYCODE_q: set_key("Q", down); break;
-       case KEYCODE_r: set_key("R", down); break;
-       case KEYCODE_s: set_key("S", down); break;
-       case KEYCODE_t: set_key("T", down); break;
-       case KEYCODE_u: set_key("U", down); break;
-       case KEYCODE_v: set_key("V", down); break;
-       case KEYCODE_w: set_key("W", down); break;
-       case KEYCODE_x: set_key("X", down); break;
-       case KEYCODE_y: set_key("Y", down); break;
-       case KEYCODE_z: set_key("Z", down); break;
-
-       case KEYCODE_0: set_key("0", down); break;
-       case KEYCODE_1: set_key("1", down); break;
-       case KEYCODE_2: set_key("2", down); break;
-       case KEYCODE_3: set_key("3", down); break;
-       case KEYCODE_4: set_key("4", down); break;
-       case KEYCODE_5: set_key("5", down); break;
-       case KEYCODE_6: set_key("6", down); break;
-       case KEYCODE_7: set_key("7", down); break;
-       case KEYCODE_8: set_key("8", down); break;
-       case KEYCODE_9: set_key("9", down); break;
-
-       case KEYCODE_KP_0: set_key("NP0", down); break;
-       case KEYCODE_KP_1: set_key("NP1", down); break;
-       case KEYCODE_KP_2: set_key("NP2", down); break;
-       case KEYCODE_KP_3: set_key("NP3", down); break;
-       case KEYCODE_KP_4: set_key("NP4", down); break;
-       case KEYCODE_KP_5: set_key("NP5", down); break;
-       case KEYCODE_KP_6: set_key("NP6", down); break;
-       case KEYCODE_KP_7: set_key("NP7", down); break;
-       case KEYCODE_KP_8: set_key("NP8", down); break;
-       case KEYCODE_KP_9: set_key("NP9", down); break;
-       case KEYCODE_KP_DIVIDE: set_key("NPDIV", down); break;
-       case KEYCODE_KP_MULTIPLY: set_key("NPMUL", down); break;
-       case KEYCODE_KP_MINUS: set_key("NPSUB", down); break;
-       case KEYCODE_KP_PLUS: set_key("NPADD", down); break;
-       case KEYCODE_KP_PERIOD: set_key("NPDEL", down); break;
-       // NPLPAREN and NPRPAREN are not emulated
-
-       case KEYCODE_F1: set_key("F1", down); break;
-       case KEYCODE_F2: set_key("F2", down); break;
-       case KEYCODE_F3: set_key("F3", down); break;
-       case KEYCODE_F4: set_key("F4", down); break;
-       case KEYCODE_F5: set_key("F5", down); break;
-       case KEYCODE_F6: set_key("F6", down); break;
-       case KEYCODE_F7: set_key("F7", down); break;
-       case KEYCODE_F8: set_key("F8", down); break;
-       case KEYCODE_F9: set_key("F9", down); break;
-       case KEYCODE_F10: set_key("F10", down); break;
-
-       case KEYCODE_SPACE: set_key("SPC", down); break;
-       case KEYCODE_BACKSPACE: set_key("BS", down); break;
-       case KEYCODE_TAB: set_key("TAB", down); break;
-       case KEYCODE_KP_ENTER: set_key("ENT", down); break;
-       case KEYCODE_RETURN: set_key("RET", down); break;
-       case KEYCODE_ESCAPE: set_key("ESC", down); break;
-       case KEYCODE_DELETE: set_key("DEL", down); break;
-       case KEYCODE_INSERT: set_key("HELP", down); break;
-
-       default: break;
+       case KEYCODE_INSERT:
+               set_amiga_key(KEYCODE_HELP, down);
+               break;
+       default:
+               set_amiga_key(keycode, down);
        }
 }
 
@@ -1004,20 +1283,25 @@ int main(int argc, char *argv[])
        /* init SDL and OpenGL */
 #ifdef HAVE_OVR
        int vbl_sync = 0;
-       int rift = 1;
+       int vr = 1;
+       int multisampling = 0;
        window_width = SCREEN_WIDTH;
        window_height = SCREEN_HEIGHT;
 #else
        int vbl_sync = 1;
-       int rift = 0;
+       int vr = 0;
+       int multisampling = config_multisampling;
        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, config_multisampling, vbl_sync, rift);
+       rc = init_sdl(argv[0], window_width, window_height, SOUND_SAMPLERATE, sdl_sound_chunk, keyboard_sdl, audio_sdl, resize_window, multisampling, vbl_sync, vr);
        if (rc < 0)
                goto done;
 #ifdef HAVE_OVR
-       rc = init_ovr();
+       rc = init_ovr(config_multisampling);
+       if (rc < 0)
+               goto done;
+       rc = init_vr_keyboard(config_keyboard_height, config_keyboard_distance);
        if (rc < 0)
                goto done;
 #endif
@@ -1032,11 +1316,11 @@ int main(int argc, char *argv[])
        rc = init_opengl_osd(1, OSD_WIDTH, OSD_HEIGHT);
        if (rc < 0)
                goto done;
-       help_osd = text_alloc(IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, HELP_ALPHA);
-       if (!help_osd)
+       help_osd[0] = text_alloc(IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, HELP_ALPHA);
+       if (!help_osd[0])
                goto done;
-       text_render(help_osd, IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2, mercenary_name, HELP_ALPHA, 3, (double)(80 - strlen(mercenary_name)) / 2.0, 1, 1);
-       text_render(help_osd, IMAGE_WIDTH * 2, IMAGE_HEIGHT * 2,
+       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,
                "Emulation:\n"
                "        Press `PAUSE' to toggle this help screen on or off.\n"
                "        Press `CTRL' + `F' to toggle between full screen / window mode.\n"
@@ -1048,7 +1332,7 @@ int main(int argc, char *argv[])
                "        Press `CTRL' + `+' or `-' to change field-of-view (OpenGL).\n"
                "        Press `CTRL' + `I' to skip intro (approaching to Eris).\n"
 #ifdef HAVE_OVR
-               "        Press `CTRL' + `N' to normalize player position.\n"
+               "        Press `CTRL' + `O' (or both triggers) to reset observer position.\n"
 #endif
                "\n"
                "Answer to a Question:\n"
@@ -1081,6 +1365,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);
+#ifdef HAVE_OVR
+       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 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"
+               "        Point to a key on keyboard. The key will highlight.\n"
+               "        Pull `Trigger' on right controller to enter the highlighted key.\n"
+               "        Press thumb stick on left controller to reset observer position.\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"
+               "        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"
+               "        Pull `Trigger' on left controller stop craft.\n"
+               "        Pull `Trigger' on left controller for escape sequence, after stop.\n"
+               "        Pull `Trigger' on right controller to fire.\n"
+               "\n"
+               "For all other game function, use the virtual or real keyboard!\n"
+               ,HELP_ALPHA, 1, 2, 5, 0);
+       help_views = 2;
+#endif
        info_osd = text_alloc(OSD_WIDTH, OSD_HEIGHT, 0x00);
        if (!info_osd)
                goto done;
@@ -1114,8 +1425,10 @@ done:
                free(sound_buffer);
        if (image)
                free(image);
-       if (help_osd)
-               free(help_osd);
+       if (help_osd[0])
+               free(help_osd[0]);
+       if (help_osd[1])
+               free(help_osd[1]);
        if (info_osd)
                free(info_osd);