Browse code

Added joystick support

neauoire authored on 27/12/2021 19:44:57
Showing 1 changed files
... ...
@@ -163,6 +163,7 @@ redraw(Uxn *u)
163 163
 static int
164 164
 init(void)
165 165
 {
166
+	SDL_Joystick *gGameController;
166 167
 	SDL_AudioSpec as;
167 168
 	SDL_zero(as);
168 169
 	as.freq = SAMPLE_FREQUENCY;
... ...
@@ -171,7 +172,7 @@ init(void)
171 172
 	as.callback = audio_callback;
172 173
 	as.samples = 512;
173 174
 	as.userdata = NULL;
174
-	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER) < 0) {
175
+	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
175 176
 		error("sdl", SDL_GetError());
176 177
 		if(SDL_Init(SDL_INIT_VIDEO) < 0)
177 178
 			return error("sdl", SDL_GetError());
... ...
@@ -186,13 +187,17 @@ init(void)
186 187
 	gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
187 188
 	if(gRenderer == NULL)
188 189
 		return error("sdl_renderer", SDL_GetError());
190
+	if(SDL_NumJoysticks()) {
191
+		gGameController = SDL_JoystickOpen(0);
192
+		if(gGameController == NULL)
193
+			return error("sdl_joystick", SDL_GetError());
194
+	}
189 195
 	stdin_event = SDL_RegisterEvents(1);
190 196
 	audio0_event = SDL_RegisterEvents(POLYPHONY);
191 197
 	SDL_CreateThread(stdin_handler, "stdin", NULL);
192 198
 	SDL_StartTextInput();
193 199
 	SDL_ShowCursor(SDL_DISABLE);
194 200
 	SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
195
-	SDL_GameControllerEventState(SDL_ENABLE);
196 201
 	return 1;
197 202
 }
198 203
 
... ...
@@ -409,20 +414,19 @@ get_button(SDL_Event *event)
409 414
 }
410 415
 
411 416
 static Uint8
412
-get_button_dpad(SDL_Event *e)
413
-{
414
-	switch(e->cbutton.button) {
415
-	case SDL_CONTROLLER_BUTTON_A: return 0x01;
416
-	case SDL_CONTROLLER_BUTTON_B: return 0x02;
417
-	case SDL_CONTROLLER_BUTTON_X: return 0x04;
418
-	case SDL_CONTROLLER_BUTTON_Y:
419
-	case SDL_CONTROLLER_BUTTON_START: return 0x08;
420
-	case SDL_CONTROLLER_BUTTON_DPAD_UP: return 0x10;
421
-	case SDL_CONTROLLER_BUTTON_DPAD_DOWN: return 0x20;
422
-	case SDL_CONTROLLER_BUTTON_DPAD_LEFT: return 0x40;
423
-	case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: return 0x80;
424
-	}
425
-	return 0x00;
417
+get_button_joystick(SDL_Event *event)
418
+{
419
+	return 0x01 << (event->jbutton.button & 0x3);
420
+}
421
+
422
+static Uint8
423
+get_vector_joystick(SDL_Event *event)
424
+{
425
+	if(event->jaxis.value < -3200)
426
+		return 1;
427
+	if(event->jaxis.value > 3200)
428
+		return 2;
429
+	return 0;
426 430
 }
427 431
 
428 432
 static Uint8
... ...
@@ -509,10 +513,17 @@ run(Uxn *u)
509 513
 				controller_up(devctrl, get_button(&event));
510 514
 			else if(event.type == SDL_TEXTINPUT)
511 515
 				controller_key(devctrl, event.text.text[0]);
512
-			else if(event.type == SDL_CONTROLLERBUTTONDOWN)
513
-				controller_down(devctrl, get_button_dpad(&event));
514
-			else if(event.type == SDL_CONTROLLERBUTTONUP)
515
-				controller_up(devctrl, get_button_dpad(&event));
516
+			else if(event.type == SDL_JOYBUTTONDOWN)
517
+				controller_down(devctrl, get_button_joystick(&event));
518
+			else if(event.type == SDL_JOYBUTTONUP)
519
+				controller_up(devctrl, get_button_joystick(&event));
520
+			else if(event.type == SDL_JOYAXISMOTION) {
521
+				Uint8 vec = get_vector_joystick(&event);
522
+				if(!vec)
523
+					controller_up(devctrl, (0x03 << (!event.jaxis.axis * 2)) << 4);
524
+				else
525
+					controller_down(devctrl, (0x01 << ((vec + !event.jaxis.axis * 2) - 1)) << 4);
526
+			}
516 527
 			/* Console */
517 528
 			else if(event.type == stdin_event)
518 529
 				console_input(u, event.cbutton.button);