From 29e82fd4aaad6ffa580be222311d7a092509e75c Mon Sep 17 00:00:00 2001 From: Bradley Bell Date: Tue, 5 Mar 2002 12:13:33 +0000 Subject: [PATCH] SDL joystick stuff mostly done --- arch/sdl/joy.c | 175 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 154 insertions(+), 21 deletions(-) diff --git a/arch/sdl/joy.c b/arch/sdl/joy.c index 69002faa..2e5e71fd 100644 --- a/arch/sdl/joy.c +++ b/arch/sdl/joy.c @@ -1,12 +1,15 @@ /* * $Source: /cvs/cvsroot/d2x/arch/sdl/joy.c,v $ - * $Revision: 1.2 $ + * $Revision: 1.3 $ * $Author: bradleyb $ - * $Date: 2001-12-03 02:43:02 $ + * $Date: 2002-03-05 12:13:33 $ * * SDL joystick support * * $Log: not supported by cvs2svn $ + * Revision 1.2 2001/12/03 02:43:02 bradleyb + * lots of makefile fixes, and sdl joystick stuff + * * Revision 1.1 2001/10/24 09:25:05 bradleyb * Moved input stuff to arch subdirs, as in d1x. * @@ -16,6 +19,8 @@ * */ +#define JOYSTICK_DEBUG + #ifdef HAVE_CONFIG_H #include #endif @@ -26,6 +31,7 @@ #include "error.h" #include "timer.h" #include "console.h" +#include "event.h" #define MAX_JOYSTICKS 16 #define MAX_AXES 32 @@ -36,18 +42,26 @@ char joy_present = 0; int num_joysticks = 0; +int joy_deadzone = 0; + struct joybutton { int state; fix time_went_down; - fix time_held_down; int num_downs; int num_ups; }; +struct joyaxis { + int value; + int min_val; + int center_val; + int max_val; +}; + static struct joyinfo { int n_axes; int n_buttons; - int axes[MAX_AXES]; + struct joyaxis axes[MAX_AXES]; struct joybutton buttons[MAX_BUTTONS]; } Joystick; @@ -74,9 +88,6 @@ void joy_button_handler(SDL_JoyButtonEvent *jbe) Joystick.buttons[button].num_downs++; break; case SDL_JOYBUTTONUP: - Joystick.buttons[button].time_held_down - += timer_get_fixed_seconds() - - Joystick.buttons[button].time_went_down; Joystick.buttons[button].num_ups++; break; } @@ -88,7 +99,7 @@ void joy_axis_handler(SDL_JoyAxisEvent *jae) axis = SDL_Joysticks[jae->which].axis_map[jae->axis]; - Joystick.axes[axis] = jae->value; + Joystick.axes[axis].value = jae->value; } @@ -102,9 +113,9 @@ int joy_init() n = SDL_NumJoysticks(); - con_printf(CON_VERBOSE, "Joystick: found %d joysticks\n", n); + con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n); for (i = 0; i < n; i++) { - con_printf(CON_VERBOSE, "Joystick %d: %s\n", i, SDL_JoystickName(i)); + con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i)); SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i); if (SDL_Joysticks[num_joysticks].handle) { joy_present = 1; @@ -112,15 +123,17 @@ int joy_init() = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle); SDL_Joysticks[num_joysticks].n_buttons = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle); - con_printf(CON_VERBOSE, "Joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes); - con_printf(CON_VERBOSE, "Joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons); + con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes); + con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons); for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++) SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++; for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++) SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++; num_joysticks++; } else - con_printf(CON_VERBOSE, "Joystick: initialization failed!\n"); + con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n"); + con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes); + con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons); } return joy_present; @@ -134,55 +147,175 @@ void joy_close() void joy_get_pos(int *x, int *y) { - *x = Joystick.axes[0] << 8; - *y = Joystick.axes[1] << 8; + int axis[MAX_AXES]; + + if (!num_joysticks) { + *x=*y=0; + return; + } + + joystick_read_raw_axis (JOY_ALL_AXIS, axis); + + *x = joy_get_scaled_reading( axis[0], 0 ); + *y = joy_get_scaled_reading( axis[1], 1 ); } int joy_get_btns() { +#if 0 // This is never used? + int i, buttons = 0; + for (i=0; i++; i= Joystick.n_buttons) + return 0; + + event_poll(); + + return Joystick.buttons[btn].state; } void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max) { + int i; + + for (i = 0; i < JOY_NUM_AXES; i++) { + axis_center[i] = Joystick.axes[i].center_val; + axis_min[i] = Joystick.axes[i].min_val; + axis_max[i] = Joystick.axes[i].max_val; + } } void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max) { + int i; + + for (i = 0; i < JOY_NUM_AXES; i++) { + Joystick.axes[i].center_val = axis_center[i]; + Joystick.axes[i].min_val = axis_min[i]; + Joystick.axes[i].max_val = axis_max[i]; + } } -int joy_get_scaled_reading( int raw, int axn ) +int joy_get_scaled_reading( int raw, int axis_num ) { - return 0; + int d, x; + + raw -= Joystick.axes[axis_num].center_val; + + if (raw < 0) + d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val; + else if (raw > 0) + d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val; + else + d = 0; + + if (d) + x = ((raw << 7) / d); + else + x = 0; + + if ( x < -128 ) + x = -128; + if ( x > 127 ) + x = 127; + + d = (joy_deadzone) * 6; + if ((x > (-1*d)) && (x < d)) + x = 0; + +#ifdef JOYSTICK_DEBUG + printf("scaled: axis %d: %d\n", axis_num, x); +#endif + + return x; } void joy_set_slow_reading( int flag ) -- 2.39.2