comparison 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
comparison
equal deleted inserted replaced
27:351a0d0cce3b 28:c677507682e3
1 #include <SDL.h> 1 #include <SDL.h>
2 #include <stdint.h> 2 #include <stdint.h>
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include "controller.h"
4 5
5 6
6 static SDL_Window *window; 7 static SDL_Window *window;
7 static SDL_Renderer *renderer; 8 static SDL_Renderer *renderer;
8 static SDL_Texture *texture; 9 static SDL_Texture *texture;
67 int system_buffer_size() 68 int system_buffer_size()
68 { 69 {
69 return buffer_size; 70 return buffer_size;
70 } 71 }
71 72
73 static int num_controllers;
74 static SDL_GameController **game_controllers;
75
72 int system_init(int width, int height, int desired_sample_rate) 76 int system_init(int width, int height, int desired_sample_rate)
73 { 77 {
74 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { 78 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER) < 0) {
75 fprintf(stderr, "Failed to init SDL: %s\n", SDL_GetError()); 79 fprintf(stderr, "Failed to init SDL: %s\n", SDL_GetError());
76 return 0; 80 return 0;
77 } 81 }
78 atexit(SDL_Quit); 82 atexit(SDL_Quit);
79 window = SDL_CreateWindow("simple16", 0, 0, width, height, 0); 83 window = SDL_CreateWindow("simple16", 0, 0, width, height, 0);
114 sample_rate = actual.freq; 118 sample_rate = actual.freq;
115 buffer_size = actual.samples; 119 buffer_size = actual.samples;
116 atexit(close_audio); 120 atexit(close_audio);
117 SDL_PauseAudio(0); 121 SDL_PauseAudio(0);
118 122
123 num_controllers = SDL_NumJoysticks();
124 game_controllers = calloc(num_controllers, sizeof(*game_controllers));
125
126 for (int i = 0; i < num_controllers; i++)
127 {
128 if (!SDL_IsGameController(i)) {
129 printf("Joystick %d: %s is not recognized as a controller by SDL2\n", i, SDL_JoystickNameForIndex(i));
130 continue;
131 }
132 game_controllers[i] = SDL_GameControllerOpen(i);
133 }
134
119 return 1; 135 return 1;
120 136
121 error: 137 error:
122 SDL_DestroyRenderer(renderer); 138 SDL_DestroyRenderer(renderer);
123 renderer_error: 139 renderer_error:
139 { 155 {
140 SDL_UnlockTexture(texture); 156 SDL_UnlockTexture(texture);
141 SDL_RenderCopy(renderer, texture, NULL, NULL); 157 SDL_RenderCopy(renderer, texture, NULL, NULL);
142 SDL_RenderPresent(renderer); 158 SDL_RenderPresent(renderer);
143 } 159 }
160
161 //SDL2 provides this, but only in 2.0.4+
162 SDL_GameController *controller_by_id(SDL_JoystickID which)
163 {
164 for (int i = 0; i < num_controllers; i++)
165 {
166 if (!game_controllers[i]) {
167 continue;
168 }
169 if (SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(game_controllers[i])) == which) {
170 return game_controllers[i];
171 }
172 }
173 return NULL;
174 }
175
176 const uint16_t mapping[SDL_CONTROLLER_BUTTON_MAX] = {
177 [SDL_CONTROLLER_BUTTON_LEFTSHOULDER] = 0x001,
178 [SDL_CONTROLLER_BUTTON_Y] = 0x002,
179 [SDL_CONTROLLER_BUTTON_X] = 0x004,
180 [SDL_CONTROLLER_BUTTON_RIGHTSHOULDER] = 0x008,
181 [SDL_CONTROLLER_BUTTON_B] = 0x010,
182 [SDL_CONTROLLER_BUTTON_A] = 0x020,
183 [SDL_CONTROLLER_BUTTON_BACK] = 0x040,
184 [SDL_CONTROLLER_BUTTON_START] = 0x080,
185 [SDL_CONTROLLER_BUTTON_DPAD_RIGHT] = 0x100,
186 [SDL_CONTROLLER_BUTTON_DPAD_LEFT] = 0x200,
187 [SDL_CONTROLLER_BUTTON_DPAD_DOWN] = 0x400,
188 [SDL_CONTROLLER_BUTTON_DPAD_UP] = 0x800,
189 };
144 190
145 void system_poll_events() 191 void system_poll_events()
146 { 192 {
147 SDL_Event event; 193 SDL_Event event;
148 while(SDL_PollEvent(&event)) 194 while(SDL_PollEvent(&event))
150 switch (event.type) 196 switch (event.type)
151 { 197 {
152 case SDL_QUIT: 198 case SDL_QUIT:
153 exit(0); 199 exit(0);
154 break; 200 break;
155 } 201 case SDL_CONTROLLERBUTTONDOWN:
156 } 202 controller_pressed(event.cbutton.which, mapping[event.cbutton.button]);
157 } 203 break;
158 204 case SDL_CONTROLLERBUTTONUP:
205 controller_released(event.cbutton.which, mapping[event.cbutton.button]);
206 break;
207 //TODO: Keyboard input
208 //TODO: Controller hotplug
209 }
210 }
211 }
212