Mercurial > repos > simple16
diff src/system_sdl.c @ 28:c677507682e3
Untested controller implementation
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 03 Apr 2016 00:33:54 -0700 |
parents | 4c9dbfa30a66 |
children | 3b7910575a00 |
line wrap: on
line diff
--- a/src/system_sdl.c Fri Apr 01 21:51:46 2016 -0700 +++ b/src/system_sdl.c Sun Apr 03 00:33:54 2016 -0700 @@ -1,6 +1,7 @@ #include <SDL.h> #include <stdint.h> #include <stdlib.h> +#include "controller.h" static SDL_Window *window; @@ -69,9 +70,12 @@ return buffer_size; } +static int num_controllers; +static SDL_GameController **game_controllers; + int system_init(int width, int height, int desired_sample_rate) { - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER) < 0) { fprintf(stderr, "Failed to init SDL: %s\n", SDL_GetError()); return 0; } @@ -116,6 +120,18 @@ atexit(close_audio); SDL_PauseAudio(0); + num_controllers = SDL_NumJoysticks(); + game_controllers = calloc(num_controllers, sizeof(*game_controllers)); + + for (int i = 0; i < num_controllers; i++) + { + if (!SDL_IsGameController(i)) { + printf("Joystick %d: %s is not recognized as a controller by SDL2\n", i, SDL_JoystickNameForIndex(i)); + continue; + } + game_controllers[i] = SDL_GameControllerOpen(i); + } + return 1; error: @@ -142,6 +158,36 @@ SDL_RenderPresent(renderer); } +//SDL2 provides this, but only in 2.0.4+ +SDL_GameController *controller_by_id(SDL_JoystickID which) +{ + for (int i = 0; i < num_controllers; i++) + { + if (!game_controllers[i]) { + continue; + } + if (SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(game_controllers[i])) == which) { + return game_controllers[i]; + } + } + return NULL; +} + +const uint16_t mapping[SDL_CONTROLLER_BUTTON_MAX] = { + [SDL_CONTROLLER_BUTTON_LEFTSHOULDER] = 0x001, + [SDL_CONTROLLER_BUTTON_Y] = 0x002, + [SDL_CONTROLLER_BUTTON_X] = 0x004, + [SDL_CONTROLLER_BUTTON_RIGHTSHOULDER] = 0x008, + [SDL_CONTROLLER_BUTTON_B] = 0x010, + [SDL_CONTROLLER_BUTTON_A] = 0x020, + [SDL_CONTROLLER_BUTTON_BACK] = 0x040, + [SDL_CONTROLLER_BUTTON_START] = 0x080, + [SDL_CONTROLLER_BUTTON_DPAD_RIGHT] = 0x100, + [SDL_CONTROLLER_BUTTON_DPAD_LEFT] = 0x200, + [SDL_CONTROLLER_BUTTON_DPAD_DOWN] = 0x400, + [SDL_CONTROLLER_BUTTON_DPAD_UP] = 0x800, +}; + void system_poll_events() { SDL_Event event; @@ -152,6 +198,14 @@ case SDL_QUIT: exit(0); break; + case SDL_CONTROLLERBUTTONDOWN: + controller_pressed(event.cbutton.which, mapping[event.cbutton.button]); + break; + case SDL_CONTROLLERBUTTONUP: + controller_released(event.cbutton.which, mapping[event.cbutton.button]); + break; + //TODO: Keyboard input + //TODO: Controller hotplug } } }