Back to index

extremetuxracer  0.5beta
joystick.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  *
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "joystick.h"
00023 #include "game_config.h"
00024 
00025 #if defined( HAVE_SDL_JOYSTICKOPEN )
00026 
00027 #include "SDL.h"
00028 #include "SDL_joystick.h"
00029 
00030 static SDL_Joystick *joystick = NULL;
00031 static int num_buttons = 0;
00032 static int num_axes = 0;
00033 
00034 void init_joystick()
00035 {
00036     if(getparam_disable_joystick()) return;
00037        
00038        int num_joysticks = 0;
00039     char *js_name;
00040 
00041     /* Initialize SDL SDL joystick module */
00042     if ( SDL_Init( SDL_INIT_JOYSTICK ) < 0 ) {
00043        handle_error( 1, "Couldn't initialize SDL: %s", SDL_GetError() );
00044     }
00045 
00046     num_joysticks = SDL_NumJoysticks();
00047 
00048     print_debug( DEBUG_JOYSTICK, "Found %d joysticks", num_joysticks );
00049 
00050     if ( num_joysticks == 0 ) {
00051        joystick = NULL;
00052        return;
00053     }
00054 
00055     js_name = (char*) SDL_JoystickName( 0 );
00056 
00057     print_debug( DEBUG_JOYSTICK, "Using joystick `%s'", js_name );
00058 
00059     joystick = SDL_JoystickOpen( 0 );
00060 
00061     if ( joystick == NULL ) {
00062        print_debug( DEBUG_JOYSTICK, "Cannot open joystick" );
00063        return;
00064     }
00065 
00066     /* Get number of buttons */
00067     num_buttons = SDL_JoystickNumButtons( joystick );
00068     print_debug( DEBUG_JOYSTICK, "Joystick has %d button%s", 
00069                num_buttons, num_buttons == 1 ? "" : "s" );
00070 
00071     /* Get number of axes */
00072     num_axes = SDL_JoystickNumAxes( joystick );
00073     print_debug( DEBUG_JOYSTICK, "Joystick has %d ax%ss", 
00074                num_axes, num_axes == 1 ? "i" : "e" );
00075 
00076 }
00077 
00078 bool is_joystick_active()
00079 {
00080        if(getparam_disable_joystick()) return false;
00081 
00082        return (bool) ( joystick != NULL );
00083        
00084 }
00085 
00086 void update_joystick()
00087 {
00088        if(getparam_disable_joystick()) return;
00089     SDL_JoystickUpdate();
00090 }
00091 
00092 double get_joystick_x_axis()
00093 {
00094     if(getparam_disable_joystick()) return 0.0;
00095 
00096        
00097        static bool warning_given = false;
00098     int axis;
00099 
00100     check_assertion( joystick != NULL,
00101                    "joystick is null" );
00102 
00103     axis = getparam_joystick_x_axis();
00104 
00105     /* Make sure axis is valid */
00106     if ( axis >= num_axes || axis < 0 ) {
00107 
00108        if ( !warning_given ) {
00109            print_warning( IMPORTANT_WARNING, 
00110                         "joystick x axis mapped to axis %d "
00111                         "but joystick only has %d axes", axis, num_axes );
00112            warning_given = true;
00113        }
00114 
00115        return 0.0;
00116     }
00117 
00118     return SDL_JoystickGetAxis( joystick, axis )/32768.0;
00119 }
00120 
00121 double get_joystick_y_axis()
00122 {
00123        if(getparam_disable_joystick()) return 0.0;
00124        
00125     static bool warning_given = false;
00126     int axis;
00127 
00128     check_assertion( joystick != NULL,
00129                    "joystick is null" );
00130 
00131     axis = getparam_joystick_y_axis();
00132 
00133     /* Make sure axis is valid */
00134     if ( axis >= num_axes || axis < 0 ) {
00135 
00136        if ( !warning_given ) {
00137            print_warning( IMPORTANT_WARNING, 
00138                         "joystick y axis mapped to axis %d "
00139                         "but joystick only has %d axes", axis, num_axes );
00140            warning_given = true;
00141        }
00142 
00143        return 0.0;
00144     }
00145 
00146     return SDL_JoystickGetAxis( joystick, getparam_joystick_y_axis() )/32768.0;
00147 }
00148 
00149 bool is_joystick_button_down( int button ) 
00150 {
00151     if(getparam_disable_joystick()) return false;
00152        
00153        static bool warning_given = false;
00154 
00155     check_assertion( joystick != NULL,
00156                    "joystick is null" );
00157 
00158     check_assertion( button >= 0, "button is negative" );
00159 
00160     if ( button >= num_buttons ) {
00161        if ( !warning_given ) {
00162            print_warning( IMPORTANT_WARNING,
00163                         "state of button %d requested, but "
00164                         "joystick only has %d buttons.  Further warnings "
00165                         "of this type will be suppressed", 
00166                         button, num_buttons );
00167            warning_given = true;
00168        }
00169        return false;
00170     }
00171 
00172     return (bool) SDL_JoystickGetButton( joystick, button );
00173 }
00174 
00175 bool is_joystick_continue_button_down()
00176 {
00177        if(getparam_disable_joystick()) return false;
00178        
00179     if ( joystick == NULL ) {
00180        return false;
00181     }
00182 
00183     if ( getparam_joystick_continue_button() < 0 ) {
00184        return false;
00185     }
00186 
00187     return is_joystick_button_down( getparam_joystick_continue_button() );
00188 }
00189 
00190 int get_joystick_down_button()
00191 {
00192     for(int i=0; i<num_buttons; i++){
00193               if(is_joystick_button_down(i)){
00194                      return i;                   
00195               }      
00196        }
00197        return -1;
00198 }
00199 
00200 
00201 
00202 
00203 
00204 
00205 #else
00206 
00207 /* Stub functions */
00208 
00209 void init_joystick()
00210 {
00211 }
00212 
00213 bool is_joystick_active()
00214 {
00215     return false;
00216 }
00217 
00218 void update_joystick()
00219 {
00220 }
00221 
00222 double get_joystick_x_axis()
00223 {
00224     return 0.0;
00225 }
00226 
00227 double get_joystick_y_axis()
00228 {
00229     return 0.0;
00230 }
00231 
00232 bool is_joystick_button_down( int button ) 
00233 {
00234     return false;
00235 }
00236 
00237 bool is_joystick_continue_button_down()
00238 {
00239     return false;
00240 }
00241 
00242 int get_joystick_down_button()
00243 {
00244     return -1;
00245 }
00246 
00247 
00248 #endif
00249 
00250 /* EOF */