comparison render_sdl.c @ 1860:bdca98187c9f

Reorder controllers based on which one receives player input first
author Michael Pavone <pavone@retrodev.com>
date Fri, 03 May 2019 18:40:13 -0700
parents 419b458f93cd
children fc05f49075c2
comparison
equal deleted inserted replaced
1859:52f136052ab0 1860:bdca98187c9f
444 src->last_right = right; 444 src->last_right = right;
445 } 445 }
446 446
447 static SDL_Joystick * joysticks[MAX_JOYSTICKS]; 447 static SDL_Joystick * joysticks[MAX_JOYSTICKS];
448 static int joystick_sdl_index[MAX_JOYSTICKS]; 448 static int joystick_sdl_index[MAX_JOYSTICKS];
449 static uint8_t joystick_index_locked[MAX_JOYSTICKS];
449 450
450 int render_width() 451 int render_width()
451 { 452 {
452 return main_width; 453 return main_width;
453 } 454 }
887 } 888 }
888 } 889 }
889 return -1; 890 return -1;
890 } 891 }
891 892
893 static int lowest_unlocked_joystick_index(void)
894 {
895 for (int i = 0; i < MAX_JOYSTICKS; i++) {
896 if (!joystick_index_locked[i]) {
897 return i;
898 }
899 }
900 return -1;
901 }
902
892 SDL_Joystick *render_get_joystick(int index) 903 SDL_Joystick *render_get_joystick(int index)
893 { 904 {
894 if (index >= MAX_JOYSTICKS) { 905 if (index >= MAX_JOYSTICKS) {
895 return NULL; 906 return NULL;
896 } 907 }
920 static uint32_t overscan_bot[NUM_VID_STD] = {1, 17}; 931 static uint32_t overscan_bot[NUM_VID_STD] = {1, 17};
921 static uint32_t overscan_left[NUM_VID_STD] = {13, 13}; 932 static uint32_t overscan_left[NUM_VID_STD] = {13, 13};
922 static uint32_t overscan_right[NUM_VID_STD] = {14, 14}; 933 static uint32_t overscan_right[NUM_VID_STD] = {14, 14};
923 static vid_std video_standard = VID_NTSC; 934 static vid_std video_standard = VID_NTSC;
924 static uint8_t need_ui_fb_resize; 935 static uint8_t need_ui_fb_resize;
936
937 int lock_joystick_index(int joystick, int desired_index)
938 {
939 if (desired_index < 0) {
940 desired_index = lowest_unlocked_joystick_index();
941 if (desired_index < 0 || desired_index >= joystick) {
942 return joystick;
943 }
944 }
945 SDL_Joystick *tmp_joy = joysticks[joystick];
946 int tmp_index = joystick_sdl_index[joystick];
947 joysticks[joystick] = joysticks[desired_index];
948 joystick_sdl_index[joystick] = joystick_sdl_index[desired_index];
949 joystick_index_locked[joystick] = joystick_sdl_index[desired_index];
950 joysticks[desired_index] = tmp_joy;
951 joystick_sdl_index[desired_index] = tmp_index;
952 joystick_index_locked[desired_index] = 1;
953 return desired_index;
954 }
925 955
926 static int32_t handle_event(SDL_Event *event) 956 static int32_t handle_event(SDL_Event *event)
927 { 957 {
928 if (custom_event_handler) { 958 if (custom_event_handler) {
929 custom_event_handler(event); 959 custom_event_handler(event);
937 break; 967 break;
938 case SDL_JOYBUTTONDOWN: 968 case SDL_JOYBUTTONDOWN:
939 handle_joydown(find_joystick_index(event->jbutton.which), event->jbutton.button); 969 handle_joydown(find_joystick_index(event->jbutton.which), event->jbutton.button);
940 break; 970 break;
941 case SDL_JOYBUTTONUP: 971 case SDL_JOYBUTTONUP:
942 handle_joyup(find_joystick_index(event->jbutton.which), event->jbutton.button); 972 handle_joyup(lock_joystick_index(find_joystick_index(event->jbutton.which), -1), event->jbutton.button);
943 break; 973 break;
944 case SDL_JOYHATMOTION: 974 case SDL_JOYHATMOTION:
945 handle_joy_dpad(find_joystick_index(event->jhat.which), event->jhat.hat, event->jhat.value); 975 handle_joy_dpad(lock_joystick_index(find_joystick_index(event->jhat.which), -1), event->jhat.hat, event->jhat.value);
946 break; 976 break;
947 case SDL_JOYAXISMOTION: 977 case SDL_JOYAXISMOTION:
948 handle_joy_axis(find_joystick_index(event->jaxis.which), event->jaxis.axis, event->jaxis.value); 978 handle_joy_axis(lock_joystick_index(find_joystick_index(event->jaxis.which), -1), event->jaxis.axis, event->jaxis.value);
949 break; 979 break;
950 case SDL_JOYDEVICEADDED: 980 case SDL_JOYDEVICEADDED:
951 if (event->jdevice.which < MAX_JOYSTICKS) { 981 if (event->jdevice.which < MAX_JOYSTICKS) {
952 int index = lowest_unused_joystick_index(); 982 int index = lowest_unused_joystick_index();
953 if (index >= 0) { 983 if (index >= 0) {
954 SDL_Joystick * joy = joysticks[index] = SDL_JoystickOpen(event->jdevice.which); 984 SDL_Joystick * joy = joysticks[index] = SDL_JoystickOpen(event->jdevice.which);
955 joystick_sdl_index[index] = event->jdevice.which; 985 joystick_sdl_index[index] = event->jdevice.which;
986 joystick_index_locked[index] = 0;
956 if (joy) { 987 if (joy) {
957 debug_message("Joystick %d added: %s\n", index, SDL_JoystickName(joy)); 988 debug_message("Joystick %d added: %s\n", index, SDL_JoystickName(joy));
958 debug_message("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy)); 989 debug_message("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
959 handle_joy_added(index); 990 handle_joy_added(index);
960 } 991 }