Windows (MSVC2013) move to SDL2
[blender.git] / source / gameengine / GameLogic / Joystick / SCA_JoystickEvents.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): snailrose.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
29  *  \ingroup gamelogic
30  */
31
32 #ifdef WITH_SDL
33 #  include <SDL.h>
34 #endif
35
36 #include "SCA_Joystick.h"
37 #include "SCA_JoystickPrivate.h"
38
39 #ifdef _MSC_VER
40 #  include <cstdio> /* printf */
41 #endif
42
43 #ifdef WITH_SDL
44 void SCA_Joystick::OnAxisMotion(SDL_Event* sdl_event)
45 {
46         if (sdl_event->jaxis.axis >= JOYAXIS_MAX)
47                 return;
48         
49         m_axis_array[sdl_event->jaxis.axis] = sdl_event->jaxis.value;
50         m_istrig_axis = 1;
51 }
52
53 /* See notes below in the event loop */
54 void SCA_Joystick::OnHatMotion(SDL_Event* sdl_event)
55 {
56         if (sdl_event->jhat.hat >= JOYHAT_MAX)
57                 return;
58
59         m_hat_array[sdl_event->jhat.hat] = sdl_event->jhat.value;
60         m_istrig_hat = 1;
61 }
62
63 /* See notes below in the event loop */
64 void SCA_Joystick::OnButtonUp(SDL_Event* sdl_event)
65 {
66         m_istrig_button = 1;
67 }
68
69
70 void SCA_Joystick::OnButtonDown(SDL_Event* sdl_event)
71 {
72         //if (sdl_event->jbutton.button > m_buttonmax) /* unsigned int so always above 0 */
73         //      return;
74         // sdl_event->jbutton.button;
75         
76         m_istrig_button = 1;
77 }
78
79
80 void SCA_Joystick::OnNothing(SDL_Event* sdl_event)
81 {
82         m_istrig_axis = m_istrig_button = m_istrig_hat = 0;
83 }
84
85 void SCA_Joystick::HandleEvents(void)
86 {
87         SDL_Event               sdl_event;
88         
89         int i;
90         for (i=0; i<m_joynum; i++) { /* could use JOYINDEX_MAX but no reason to */
91                 if (SCA_Joystick::m_instance[i])
92                         SCA_Joystick::m_instance[i]->OnNothing(&sdl_event);
93         }
94         
95         while (SDL_PollEvent(&sdl_event)) {
96                 /* Note! m_instance[sdl_event.jaxis.which]
97                  * will segfault if over JOYINDEX_MAX, not too nice but what are the chances? */
98                 
99                 /* Note!, with buttons, this wont care which button is pressed,
100                  * only to set 'm_istrig_button', actual pressed buttons are detected by SDL_JoystickGetButton */
101                 
102                 /* Note!, if you manage to press and release a button within 1 logic tick
103                  * it wont work as it should */
104                 
105                 switch (sdl_event.type) {
106                         case SDL_JOYAXISMOTION:
107                                 SCA_Joystick::m_instance[sdl_event.jaxis.which]->OnAxisMotion(&sdl_event);
108                                 break;
109                         case SDL_JOYHATMOTION:
110                                 SCA_Joystick::m_instance[sdl_event.jhat.which]->OnHatMotion(&sdl_event);
111                                 break;
112                         case SDL_JOYBUTTONUP:
113                                 SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonUp(&sdl_event);
114                                 break;
115                         case SDL_JOYBUTTONDOWN:
116                                 SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonDown(&sdl_event);
117                                 break;
118 #if 0   /* Not used yet */
119                         case SDL_JOYBALLMOTION:
120                                 SCA_Joystick::m_instance[sdl_event.jball.which]->OnBallMotion(&sdl_event);
121                                 break;
122 #endif
123                         default:
124                                 printf("SCA_Joystick::HandleEvents, Unknown SDL event (%d), this should not happen\n", sdl_event.type);
125                                 break;
126                 }
127         }
128 }
129 #endif /* WITH_SDL */