Keep joystick movement until rendering was performed.
[mercenary-reloaded.git] / src / mercenary / main.c
index 520dc8f..f944dd0 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"
@@ -65,7 +65,7 @@ static int config_render = 1; /* opengl render */
 static int config_skip_intro = 0;
 static int config_multisampling = 8;
 #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;
@@ -73,7 +73,7 @@ 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_distance = 38.5; /* inch */
 static double config_keyboard_height = -26.0; /* inch */
 #endif
 static int config_debug_transparent = 0;
@@ -112,7 +112,7 @@ static uint8_t *help_osd[3] = { NULL, NULL, NULL };
 static uint8_t *info_osd = NULL;
 static int help_view = 1;
 static int help_views = 0;
-static int32_t osd_timer = 0;
+static int32_t osd_timer = 0, border_timer = 0;
 #ifdef HAVE_OVR
 #define SCREEN_WIDTH   1344
 #define SCREEN_HEIGHT  800
@@ -139,6 +139,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;
 
@@ -374,6 +375,8 @@ 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
@@ -762,6 +765,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 */
@@ -800,6 +805,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 */
@@ -953,27 +970,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:
@@ -984,6 +1018,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;
@@ -1023,9 +1063,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)
@@ -1033,7 +1076,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)
 {
@@ -1079,6 +1122,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)
@@ -1086,6 +1130,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;
@@ -1107,8 +1172,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;
@@ -1150,6 +1219,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);
@@ -1175,7 +1252,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) {
@@ -1184,7 +1262,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);
@@ -1222,6 +1301,7 @@ 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 */
@@ -1361,6 +1441,7 @@ 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"
 #endif