00001
00009 #include "SDL.h"
00010 #include "joystick_SDL.h"
00011
00012
00013 int numJoy;
00014 int numAxes;
00015 int thereIsJoy=0;
00016 SDL_Joystick* joystick;
00017
00018
00020
00024 int joystick_init()
00025 {
00026 int dummy;
00027
00028 if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0 )
00029 {
00030 fprintf(stderr, "Couldn't initialize joystick: %s\n",SDL_GetError());
00031 }
00032
00033 numJoy = SDL_NumJoysticks();
00034
00035 printf("Number of joysticks: %d \n", numJoy);
00036
00037 if (numJoy>0)
00038 {
00039 joystick = SDL_JoystickOpen(0);
00040 numAxes = SDL_JoystickNumAxes (joystick);
00041 printf( "Joystick found, Number of axes: %d, name: %s \n", numAxes, SDL_JoystickName(0));
00042 dummy = SDL_JoystickEventState(SDL_QUERY);
00043 thereIsJoy=1;
00044 }
00045 else
00046 {
00047 fprintf( stderr, "No Joystick could be opened!! \n");
00048 thereIsJoy=0;
00049 }
00050
00051 return 1;
00052 }
00053
00054
00055
00057
00068 void joystick_getXYZR(float *X, float *Y, float *Z, float *R, bool doX, bool doY, bool doZ, bool doR)
00069 {
00070 if (SDL_JoystickOpened(0))
00071 {
00072 if (numAxes>0 && doX) *X = (float(SDL_JoystickGetAxis(joystick, 0))/32768.0f);
00073 if (numAxes>1 && doY) *Y = (float(SDL_JoystickGetAxis(joystick, 1))/32768.0f);
00074 if (numAxes>2 && doZ) *Z = (float(SDL_JoystickGetAxis(joystick, 2))/32768.0f);
00075 if (numAxes>3 && doR) *R = (float(SDL_JoystickGetAxis(joystick, 3))/32768.0f);
00076 }
00077 }
00078
00079
00080
00082
00085 void joystick_close()
00086 {
00087 if (numJoy>0)
00088 {
00089 SDL_JoystickClose(joystick);
00090 printf("Joystick closed.");
00091 }
00092 }
00093
00094
00096
00099 int is_there_a_joystick()
00100 {
00101 return thereIsJoy;
00102 }