/* * sturmbahnfahrer main program. * (c) 2006 by Bram Stolk * bram at gmail.com * LICENSED ACCORDING TO THE GPL */ #include #include #include #include #ifdef WIN32 #include #include #else #include // for ioctl() #include // for close() #include // for perror() #endif #include "controller.h" #include "joydb.h" #ifdef WIN32 /* implementation functions */ void SetMMerror(char *function, int code) { static char *error; static char errbuf[BUFSIZ]; errbuf[0] = 0; switch (code) { case MMSYSERR_NODRIVER: error = "Joystick driver not present"; break; case MMSYSERR_INVALPARAM: case JOYERR_PARMS: error = "Invalid parameter(s)"; break; case MMSYSERR_BADDEVICEID: error = "Bad device ID"; break; case JOYERR_UNPLUGGED: error = "Joystick not attached"; break; case JOYERR_NOCANDO: error = "Can't capture joystick input"; break; default: sprintf(errbuf, "%s: Unknown Multimedia system error: 0x%x", function, code); break; } if(!errbuf[0]) { sprintf(errbuf, "%s: %s", function, error); } fprintf(stderr, "%s\n", errbuf); } #endif ControllerPad::ControllerPad(const std::string &devfile) : opened(false), fd(0), DeadMargin(0.05), joydb_entry(0), steer_axis(-1), brake_axis(-1), brake_button(-1), ebrake_button(-1), action_button(-1), accel_axis(-1), accel_button(-1) { return; } ControllerPad::~ControllerPad() { if (opened) { #ifdef WIN32 #else int retval = close(fd); if (retval==-1) perror("close() failed"); #endif } } void ControllerPad::Sustain(float dt) { } float ControllerPad::GetAxisValue(int nr) const { assert(nr < (int) AxisValues.size()); assert(nr >= 0); float x=AxisValues[nr]/32768.0; if (fabsf(x)0)?DeadMargin:-DeadMargin; return x / (1.0 - DeadMargin); } bool ControllerPad::GetButtonValue(int nr) const { assert(nr < (int) ButtonValues.size()); assert(nr >= 0); return ButtonValues[nr]; } bool ControllerPad::GetButtonChanged(int nr) { bool retval = ButtonChanged[nr]; ButtonChanged[nr]=false; return retval; } bool ControllerPad::HasAnyButtonChanged(bool down) { int num_generic_buttons; const int *gbuttons = joydb_get(JOYDB_GENERIC_BUTTONS, joydb_entry, &num_generic_buttons); for (int i=0; i= 2) { accel_axis = axes[1]; brake_axis = axes[0]; } const int *sbuttons = joydb_get(JOYDB_SHOULDER_BUTTONS, joydb_entry, &num_shoulder_buttons); if (num_shoulder_buttons >= 2) { brake_button = sbuttons[0]; accel_button = sbuttons[num_shoulder_buttons/2]; } const int *gbuttons = joydb_get(JOYDB_GENERIC_BUTTONS, joydb_entry, &num_generic_buttons); assert(num_generic_buttons >= 2); action_button = gbuttons[0]; ebrake_button = gbuttons[1]; axes = joydb_get(JOYDB_SHOULDER_AXES, joydb_entry, &num_shoulder_axes); if (num_shoulder_axes >= 2) { brake_axis = axes[0]; accel_axis = axes[1]; } } else { // We know nothing about this gamepad/joystick/wheel fprintf ( stderr, "Your joystick device called '%s' is not in this game's joystickdatabase.\n" "Please submit information on your gamepad (see JOYSTICKS file).\n" "In the mean while, you'll have to play via keyboard (a,z,<,>,SPACE,b)\n", name.c_str() ); opened = false; return; } fprintf(stderr,"Nr of shoulder buttons = %d\n", num_shoulder_buttons); fprintf(stderr,"Nr of shoulder axes = %d\n", num_shoulder_axes); fprintf(stderr,"Nr of foot paddles = %d\n", num_foot_paddles); fprintf(stderr,"Nr of generic buttons = %d\n", num_generic_buttons); }