Skip to content

Commit 42f1ba5

Browse files
committed
fix #162 analog joystick controls not working properly
1 parent cbe0c07 commit 42f1ba5

File tree

2 files changed

+100
-102
lines changed

2 files changed

+100
-102
lines changed

src/config.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ config_params_s init_config() {
6464
c.gamepad_quit = SDL_CONTROLLER_BUTTON_RIGHTSTICK;
6565
c.gamepad_reset = SDL_CONTROLLER_BUTTON_LEFTSTICK;
6666

67-
c.gamepad_analog_threshold = 32766;
67+
c.gamepad_analog_threshold = 30000;
6868
c.gamepad_analog_invert = 0;
6969
c.gamepad_analog_axis_updown = SDL_CONTROLLER_AXIS_LEFTY;
7070
c.gamepad_analog_axis_leftright = SDL_CONTROLLER_AXIS_LEFTX;
@@ -131,8 +131,7 @@ void write_config(config_params_s *conf) {
131131
conf->key_jazz_inc_velocity);
132132
snprintf(ini_values[initPointer++], LINELEN, "key_jazz_dec_velocity=%d\n",
133133
conf->key_jazz_dec_velocity);
134-
snprintf(ini_values[initPointer++], LINELEN, "key_toggle_audio=%d\n",
135-
conf->key_toggle_audio);
134+
snprintf(ini_values[initPointer++], LINELEN, "key_toggle_audio=%d\n", conf->key_toggle_audio);
136135
snprintf(ini_values[initPointer++], LINELEN, "[gamepad]\n");
137136
snprintf(ini_values[initPointer++], LINELEN, "gamepad_up=%d\n", conf->gamepad_up);
138137
snprintf(ini_values[initPointer++], LINELEN, "gamepad_left=%d\n", conf->gamepad_left);

src/input.c

Lines changed: 98 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include <SDL.h>
55
#include <stdio.h>
66

7-
#include "SDL_log.h"
8-
#include "SDL_timer.h"
97
#include "config.h"
108
#include "input.h"
119
#include "render.h"
@@ -272,36 +270,36 @@ static int get_game_controller_button(config_params_s *conf, SDL_GameController
272270
// Check digital buttons
273271
if (SDL_GameControllerGetButton(controller, button_mappings[button])) {
274272
return 1;
275-
} else {
276-
// If digital button isn't pressed, check the corresponding analog control
277-
switch (button) {
278-
case INPUT_UP:
279-
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_updown) <
280-
-conf->gamepad_analog_threshold;
281-
case INPUT_DOWN:
282-
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_updown) >
283-
conf->gamepad_analog_threshold;
284-
case INPUT_LEFT:
285-
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_leftright) <
286-
-conf->gamepad_analog_threshold;
287-
case INPUT_RIGHT:
288-
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_leftright) >
289-
conf->gamepad_analog_threshold;
290-
case INPUT_OPT:
291-
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_opt) >
292-
conf->gamepad_analog_threshold;
293-
case INPUT_EDIT:
294-
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_edit) >
295-
conf->gamepad_analog_threshold;
296-
case INPUT_SELECT:
297-
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_select) >
298-
conf->gamepad_analog_threshold;
299-
case INPUT_START:
300-
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_start) >
301-
conf->gamepad_analog_threshold;
302-
default:
303-
return 0;
304-
}
273+
}
274+
275+
// If digital button isn't pressed, check the corresponding analog control
276+
switch (button) {
277+
case INPUT_UP:
278+
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_updown) <
279+
-conf->gamepad_analog_threshold;
280+
case INPUT_DOWN:
281+
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_updown) >
282+
conf->gamepad_analog_threshold;
283+
case INPUT_LEFT:
284+
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_leftright) <
285+
-conf->gamepad_analog_threshold;
286+
case INPUT_RIGHT:
287+
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_leftright) >
288+
conf->gamepad_analog_threshold;
289+
case INPUT_OPT:
290+
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_opt) >
291+
conf->gamepad_analog_threshold;
292+
case INPUT_EDIT:
293+
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_edit) >
294+
conf->gamepad_analog_threshold;
295+
case INPUT_SELECT:
296+
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_select) >
297+
conf->gamepad_analog_threshold;
298+
case INPUT_START:
299+
return SDL_GameControllerGetAxis(controller, conf->gamepad_analog_axis_start) >
300+
conf->gamepad_analog_threshold;
301+
default:
302+
return 0;
305303
}
306304
return 0;
307305
}
@@ -318,7 +316,7 @@ static int handle_game_controller_buttons(config_params_s *conf) {
318316
// Cycle through every active game controller
319317
for (int gc = 0; gc < num_joysticks; gc++) {
320318
// Cycle through all M8 buttons
321-
for (int button = 0; button < (input_buttons_t)INPUT_MAX; button++) {
319+
for (int button = 0; button < INPUT_MAX; button++) {
322320
// If the button is active, add the keycode to the variable containing
323321
// active keys
324322
if (get_game_controller_button(conf, game_controllers[gc], button)) {
@@ -356,87 +354,88 @@ void handle_sdl_events(config_params_s *conf) {
356354
key = (input_msg_s){special, msg_reset_display};
357355
}
358356

359-
SDL_PollEvent(&event);
357+
while (SDL_PollEvent(&event)) {
360358

361-
switch (event.type) {
359+
switch (event.type) {
362360

363-
// Reinitialize game controllers on controller add/remove/remap
364-
case SDL_CONTROLLERDEVICEADDED:
365-
case SDL_CONTROLLERDEVICEREMOVED:
366-
initialize_game_controllers();
367-
break;
361+
// Reinitialize game controllers on controller add/remove/remap
362+
case SDL_CONTROLLERDEVICEADDED:
363+
case SDL_CONTROLLERDEVICEREMOVED:
364+
initialize_game_controllers();
365+
break;
368366

369-
// Handle SDL quit events (for example, window close)
370-
case SDL_QUIT:
371-
key = (input_msg_s){special, msg_quit};
372-
break;
367+
// Handle SDL quit events (for example, window close)
368+
case SDL_QUIT:
369+
key = (input_msg_s){special, msg_quit};
370+
break;
373371

374-
case SDL_WINDOWEVENT:
375-
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
376-
static uint32_t ticks_window_resized = 0;
377-
if (SDL_GetTicks() - ticks_window_resized > 500) {
378-
SDL_Log("Resizing window...");
379-
key = (input_msg_s){special, msg_reset_display};
380-
ticks_window_resized = SDL_GetTicks();
372+
case SDL_WINDOWEVENT:
373+
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
374+
static uint32_t ticks_window_resized = 0;
375+
if (SDL_GetTicks() - ticks_window_resized > 500) {
376+
SDL_Log("Resizing window...");
377+
key = (input_msg_s){special, msg_reset_display};
378+
ticks_window_resized = SDL_GetTicks();
379+
}
381380
}
382-
}
383-
break;
384-
385-
// Keyboard events. Special events are handled within SDL_KEYDOWN.
386-
case SDL_KEYDOWN:
387-
388-
if (event.key.repeat > 0) {
389381
break;
390-
}
391382

392-
// ALT+ENTER toggles fullscreen
393-
if (event.key.keysym.sym == SDLK_RETURN && (event.key.keysym.mod & KMOD_ALT) > 0) {
394-
toggle_fullscreen();
395-
break;
396-
}
383+
// Keyboard events. Special events are handled within SDL_KEYDOWN.
384+
case SDL_KEYDOWN:
397385

398-
// ALT+F4 quits program
399-
else if (event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) > 0) {
400-
key = (input_msg_s){special, msg_quit};
401-
break;
402-
}
386+
if (event.key.repeat > 0) {
387+
break;
388+
}
403389

404-
// ESC = toggle keyjazz
405-
else if (event.key.keysym.sym == SDLK_ESCAPE) {
406-
display_keyjazz_overlay(toggle_input_keyjazz(), keyjazz_base_octave, keyjazz_velocity);
407-
}
390+
// ALT+ENTER toggles fullscreen
391+
if (event.key.keysym.sym == SDLK_RETURN && (event.key.keysym.mod & KMOD_ALT) > 0) {
392+
toggle_fullscreen();
393+
break;
394+
}
408395

409-
// Normal keyboard inputs
410-
case SDL_KEYUP:
411-
key = handle_normal_keys(&event, conf, 0);
396+
// ALT+F4 quits program
397+
else if (event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) > 0) {
398+
key = (input_msg_s){special, msg_quit};
399+
break;
400+
}
412401

413-
if (keyjazz_enabled)
414-
key = handle_keyjazz(&event, key.value, conf);
415-
break;
402+
// ESC = toggle keyjazz
403+
else if (event.key.keysym.sym == SDLK_ESCAPE) {
404+
display_keyjazz_overlay(toggle_input_keyjazz(), keyjazz_base_octave, keyjazz_velocity);
405+
}
416406

417-
default:
418-
break;
419-
}
407+
// Normal keyboard inputs
408+
case SDL_KEYUP:
409+
key = handle_normal_keys(&event, conf, 0);
420410

421-
switch (key.type) {
422-
case normal:
423-
if (event.type == SDL_KEYDOWN) {
424-
keycode |= key.value;
425-
} else {
426-
keycode &= ~key.value;
411+
if (keyjazz_enabled)
412+
key = handle_keyjazz(&event, key.value, conf);
413+
break;
414+
415+
default:
416+
break;
427417
}
428-
break;
429-
case keyjazz:
430-
// Do not allow pressing multiple keys with keyjazz
431-
case special:
432-
if (event.type == SDL_KEYDOWN) {
433-
keycode = key.value;
434-
} else {
435-
keycode = 0;
418+
419+
switch (key.type) {
420+
case normal:
421+
if (event.type == SDL_KEYDOWN) {
422+
keycode |= key.value;
423+
} else {
424+
keycode &= ~key.value;
425+
}
426+
break;
427+
case keyjazz:
428+
// Do not allow pressing multiple keys with keyjazz
429+
case special:
430+
if (event.type == SDL_KEYDOWN) {
431+
keycode = key.value;
432+
} else {
433+
keycode = 0;
434+
}
435+
break;
436+
default:
437+
break;
436438
}
437-
break;
438-
default:
439-
break;
440439
}
441440
}
442441

0 commit comments

Comments
 (0)