diff render_sdl.c @ 418:dbf4e1c86f3c

Implement basic joystick support
author Mike Pavone <pavone@retrodev.com>
date Sun, 23 Jun 2013 22:27:12 -0700
parents 29c1a0dcf683
children d0cacb4ade0b
line wrap: on
line diff
--- a/render_sdl.c	Sun Jun 23 12:27:11 2013 -0700
+++ b/render_sdl.c	Sun Jun 23 22:27:12 2013 -0700
@@ -72,9 +72,12 @@
 	SDL_CloseAudio();
 }
 
+SDL_Joystick * joysticks[MAX_JOYSTICKS];
+int num_joysticks;
+
 void render_init(int width, int height, char * title, uint32_t fps)
 {
-	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
+	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
         fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
         exit(1);
     }
@@ -145,6 +148,18 @@
     sample_rate = actual.freq;
     printf("Initialized audio at frequency %d with a %d sample buffer\n", actual.freq, actual.samples);
     SDL_PauseAudio(0);
+    num_joysticks = SDL_NumJoysticks();
+    if (num_joysticks > MAX_JOYSTICKS) {
+    	num_joysticks = MAX_JOYSTICKS;
+    }
+    for (int i = 0; i < num_joysticks; i++) {
+    	printf("Joystick %d: %s\n", i, SDL_JoystickName(i));
+    	SDL_Joystick * joy = joysticks[i] = SDL_JoystickOpen(i);
+    	if (joy) {
+    		printf("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
+    	}
+    }
+    SDL_JoystickEventState(SDL_ENABLE);
 }
 
 uint16_t blankbuf[320*240];
@@ -281,6 +296,22 @@
     }
 }
 
+int render_joystick_num_buttons(int joystick)
+{
+	if (joystick >= num_joysticks) {
+		return 0;
+	}
+	return SDL_JoystickNumButtons(joysticks[joystick]);
+}
+
+int render_joystick_num_hats(int joystick)
+{
+	if (joystick >= num_joysticks) {
+		return 0;
+	}
+	return SDL_JoystickNumHats(joysticks[joystick]);
+}
+
 void render_wait_quit(vdp_context * context)
 {
 	SDL_Event event;
@@ -329,6 +360,15 @@
 	case SDL_KEYUP:
 		handle_keyup(event->key.keysym.sym);
 		break;
+	case SDL_JOYBUTTONDOWN:
+		handle_joydown(event->jbutton.which, event->jbutton.button);
+		break;
+	case SDL_JOYBUTTONUP:
+		handle_joyup(event->jbutton.which, event->jbutton.button);
+		break;
+	case SDL_JOYHATMOTION:
+		handle_joy_dpad(event->jbutton.which, event->jhat.hat, event->jhat.value);
+		break;
 	case SDL_QUIT:
 		puts("");
 		exit(0);