comparison 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
comparison
equal deleted inserted replaced
417:acdd6c5240fe 418:dbf4e1c86f3c
70 SDL_CondSignal(audio_ready); 70 SDL_CondSignal(audio_ready);
71 SDL_UnlockMutex(audio_mutex); 71 SDL_UnlockMutex(audio_mutex);
72 SDL_CloseAudio(); 72 SDL_CloseAudio();
73 } 73 }
74 74
75 SDL_Joystick * joysticks[MAX_JOYSTICKS];
76 int num_joysticks;
77
75 void render_init(int width, int height, char * title, uint32_t fps) 78 void render_init(int width, int height, char * title, uint32_t fps)
76 { 79 {
77 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { 80 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
78 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); 81 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
79 exit(1); 82 exit(1);
80 } 83 }
81 atexit(SDL_Quit); 84 atexit(SDL_Quit);
82 atexit(render_close_audio); 85 atexit(render_close_audio);
143 } 146 }
144 buffer_samples = actual.samples; 147 buffer_samples = actual.samples;
145 sample_rate = actual.freq; 148 sample_rate = actual.freq;
146 printf("Initialized audio at frequency %d with a %d sample buffer\n", actual.freq, actual.samples); 149 printf("Initialized audio at frequency %d with a %d sample buffer\n", actual.freq, actual.samples);
147 SDL_PauseAudio(0); 150 SDL_PauseAudio(0);
151 num_joysticks = SDL_NumJoysticks();
152 if (num_joysticks > MAX_JOYSTICKS) {
153 num_joysticks = MAX_JOYSTICKS;
154 }
155 for (int i = 0; i < num_joysticks; i++) {
156 printf("Joystick %d: %s\n", i, SDL_JoystickName(i));
157 SDL_Joystick * joy = joysticks[i] = SDL_JoystickOpen(i);
158 if (joy) {
159 printf("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy));
160 }
161 }
162 SDL_JoystickEventState(SDL_ENABLE);
148 } 163 }
149 164
150 uint16_t blankbuf[320*240]; 165 uint16_t blankbuf[320*240];
151 166
152 void render_context(vdp_context * context) 167 void render_context(vdp_context * context)
279 { 294 {
280 context->framebuf = context->framebuf == context->oddbuf ? context->evenbuf : context->oddbuf; 295 context->framebuf = context->framebuf == context->oddbuf ? context->evenbuf : context->oddbuf;
281 } 296 }
282 } 297 }
283 298
299 int render_joystick_num_buttons(int joystick)
300 {
301 if (joystick >= num_joysticks) {
302 return 0;
303 }
304 return SDL_JoystickNumButtons(joysticks[joystick]);
305 }
306
307 int render_joystick_num_hats(int joystick)
308 {
309 if (joystick >= num_joysticks) {
310 return 0;
311 }
312 return SDL_JoystickNumHats(joysticks[joystick]);
313 }
314
284 void render_wait_quit(vdp_context * context) 315 void render_wait_quit(vdp_context * context)
285 { 316 {
286 SDL_Event event; 317 SDL_Event event;
287 while(SDL_WaitEvent(&event)) { 318 while(SDL_WaitEvent(&event)) {
288 switch (event.type) { 319 switch (event.type) {
327 handle_keydown(event->key.keysym.sym); 358 handle_keydown(event->key.keysym.sym);
328 break; 359 break;
329 case SDL_KEYUP: 360 case SDL_KEYUP:
330 handle_keyup(event->key.keysym.sym); 361 handle_keyup(event->key.keysym.sym);
331 break; 362 break;
363 case SDL_JOYBUTTONDOWN:
364 handle_joydown(event->jbutton.which, event->jbutton.button);
365 break;
366 case SDL_JOYBUTTONUP:
367 handle_joyup(event->jbutton.which, event->jbutton.button);
368 break;
369 case SDL_JOYHATMOTION:
370 handle_joy_dpad(event->jbutton.which, event->jhat.hat, event->jhat.value);
371 break;
332 case SDL_QUIT: 372 case SDL_QUIT:
333 puts(""); 373 puts("");
334 exit(0); 374 exit(0);
335 } 375 }
336 return 0; 376 return 0;