00001 /* 00002 MX library. 00003 00004 http://lostsidedead.com/svn 00005 00006 00007 libmx written by jared bruni, it is wrappers around SDL 00008 and other api's to provide a set of classes and functions 00009 to produce multi media applications, using object oriented 00010 techniques. 00011 00012 (C) 2008 LostSideDead 00013 */ 00014 00015 #include "mxjoystick.h" 00016 00017 #include<iostream> 00018 00019 00020 namespace mx 00021 { 00022 00023 mxJoystick::mxJoystick(int id) 00024 { 00025 stick = 0; 00026 00027 initJoystick(id); 00028 00029 } 00030 00031 mxJoystick::mxJoystick(SDL_Joystick *stick) 00032 { 00033 00034 if(this->stick != 0) 00035 { 00036 freeJoystick(); 00037 } 00038 00039 this->stick = stick; 00040 index = SDL_JoystickIndex(this->stick); 00041 } 00042 00043 00044 mxJoystick::~mxJoystick() // no-throw guarantee 00045 { 00046 freeJoystick(); 00047 } 00048 00049 00050 void mxJoystick::initJoystick(int id) 00051 { 00052 00053 stick = SDL_JoystickOpen(id); 00054 SDL_JoystickEventState(SDL_ENABLE); 00055 00056 if(stick == 0) 00057 { 00058 std::cout << "could not open joystick id: " << id << "\n"; 00059 } 00060 00061 index = id; 00062 00063 } 00064 00065 00066 void mxJoystick::freeJoystick() 00067 { 00068 SDL_JoystickClose(stick); 00069 stick = 0; 00070 } 00071 00072 const bool mxJoystick::joystickGetButton(int button) const 00073 { 00074 return SDL_JoystickGetButton(stick, button) ? true : false; 00075 } 00076 00077 const int mxJoystick::joystickGetAxis(int axis) const 00078 { 00079 return static_cast<int>(SDL_JoystickGetAxis(stick, axis)); 00080 } 00081 00082 const int mxJoystick::joystickGetHat(int hat) const 00083 { 00084 return static_cast<int>(SDL_JoystickGetHat(stick, hat)); 00085 } 00086 00087 const int mxJoystick::joystickGetBall(int ball, int &x, int &y) 00088 { 00089 return static_cast<int>( SDL_JoystickGetBall(stick, ball, &x, &y) ); 00090 } 00091 00092 } 00093 00094 00095
1.5.8