Joystick support
[mercenary-reloaded.git] / src / mercenary / main.c
index 6931b29..d4ac4ee 100644 (file)
@@ -64,6 +64,7 @@ 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.7;
 static double config_fov = FOV_JOLLY;
@@ -171,6 +172,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");
@@ -295,6 +302,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)
@@ -1309,6 +1344,35 @@ static void keyboard_sdl(int down, enum keycode keycode)
        }
 }
 
+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;
@@ -1432,7 +1496,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(argv[0], 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