Blender V2.61 - r43446

SCA_JoystickEvents.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): snailrose.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00032 #ifdef WITH_SDL
00033 #  include <SDL.h>
00034 #endif
00035 
00036 #include "SCA_Joystick.h"
00037 #include "SCA_JoystickPrivate.h"
00038 
00039 
00040 #ifdef WITH_SDL
00041 void SCA_Joystick::OnAxisMotion(SDL_Event* sdl_event)
00042 {
00043     if(sdl_event->jaxis.axis >= JOYAXIS_MAX)
00044         return;
00045     
00046     m_axis_array[sdl_event->jaxis.axis]= sdl_event->jaxis.value;
00047     m_istrig_axis = 1;
00048 }
00049 
00050 /* See notes below in the event loop */
00051 void SCA_Joystick::OnHatMotion(SDL_Event* sdl_event)
00052 {
00053     if(sdl_event->jhat.hat >= JOYHAT_MAX)
00054         return;
00055 
00056     m_hat_array[sdl_event->jhat.hat]= sdl_event->jhat.value;
00057     m_istrig_hat = 1;
00058 }
00059 
00060 /* See notes below in the event loop */
00061 void SCA_Joystick::OnButtonUp(SDL_Event* sdl_event)
00062 {
00063     m_istrig_button = 1;
00064 }
00065 
00066 
00067 void SCA_Joystick::OnButtonDown(SDL_Event* sdl_event)
00068 {
00069     //if(sdl_event->jbutton.button > m_buttonmax) /* unsigned int so always above 0 */
00070     //  return;
00071     // sdl_event->jbutton.button;
00072     
00073     m_istrig_button = 1;
00074 }
00075 
00076 
00077 void SCA_Joystick::OnNothing(SDL_Event* sdl_event)
00078 {
00079     m_istrig_axis = m_istrig_button = m_istrig_hat = 0;
00080 }
00081 
00082 void SCA_Joystick::HandleEvents(void)
00083 {
00084     SDL_Event       sdl_event;
00085     
00086     int i;
00087     for (i=0; i<m_joynum; i++) { /* could use JOYINDEX_MAX but no reason to */
00088         if(SCA_Joystick::m_instance[i])
00089             SCA_Joystick::m_instance[i]->OnNothing(&sdl_event);
00090     }
00091     
00092     while(SDL_PollEvent(&sdl_event))
00093     {
00094         /* Note! m_instance[sdl_event.jaxis.which]
00095          * will segfault if over JOYINDEX_MAX, not too nice but what are the chances? */
00096         
00097         /* Note!, with buttons, this wont care which button is pressed,
00098          * only to set 'm_istrig_button', actual pressed buttons are detected by SDL_JoystickGetButton */
00099         
00100         /* Note!, if you manage to press and release a button within 1 logic tick
00101          * it wont work as it should */
00102         
00103         switch(sdl_event.type)
00104         {
00105         case SDL_JOYAXISMOTION:
00106             SCA_Joystick::m_instance[sdl_event.jaxis.which]->OnAxisMotion(&sdl_event);
00107             break;
00108         case SDL_JOYHATMOTION:
00109             SCA_Joystick::m_instance[sdl_event.jhat.which]->OnHatMotion(&sdl_event);
00110             break;
00111         case SDL_JOYBUTTONUP:
00112             SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonUp(&sdl_event);
00113             break;
00114         case SDL_JOYBUTTONDOWN:
00115             SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonDown(&sdl_event);
00116             break;
00117 #if 0   /* Not used yet */
00118         case SDL_JOYBALLMOTION:
00119             SCA_Joystick::m_instance[sdl_event.jball.which]->OnBallMotion(&sdl_event);
00120             break;
00121 #endif
00122         default:
00123             printf("SCA_Joystick::HandleEvents, Unknown SDL event, this should not happen\n");
00124             break;
00125         }
00126     }
00127 }
00128 #endif /* WITH_SDL */