~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp

Tags: upstream-2.40
ImportĀ upstreamĀ versionĀ 2.40

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * ***** BEGIN GPL/BL DUAL 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. The Blender
 
8
 * Foundation also sells licenses for use in proprietary software under
 
9
 * the Blender License.  See http://www.blender.org/BL/ for information
 
10
 * about this.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software Foundation,
 
19
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
20
 *
 
21
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 
22
 * All rights reserved.
 
23
 *
 
24
 * The Original Code is: all of this file.
 
25
 *
 
26
 * Contributor(s): snailrose.
 
27
 *
 
28
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 
29
 */
 
30
#include <SDL.h>
 
31
 
 
32
#include "SCA_Joystick.h"
 
33
#include "SCA_JoystickPrivate.h"
 
34
 
 
35
 
 
36
SCA_Joystick::SCA_Joystick()
 
37
        :
 
38
        m_axis10(0),
 
39
        m_axis11(0),
 
40
        m_axis20(0),
 
41
        m_axis21(0),
 
42
        m_prec(3200),
 
43
        m_buttonnum(-2),
 
44
        m_hatdir(-2),
 
45
        m_isinit(0),
 
46
        m_istrig(0)
 
47
{
 
48
        m_private = new PrivateData();
 
49
}
 
50
 
 
51
 
 
52
SCA_Joystick::~SCA_Joystick()
 
53
 
 
54
{
 
55
        delete m_private;
 
56
}
 
57
 
 
58
 
 
59
bool SCA_Joystick::CreateJoystickDevice()
 
60
{
 
61
        bool init = false;
 
62
        init = pCreateJoystickDevice();
 
63
        return init;
 
64
}
 
65
 
 
66
 
 
67
void SCA_Joystick::DestroyJoystickDevice()
 
68
{
 
69
        if(m_isinit)
 
70
                pDestroyJoystickDevice();
 
71
}
 
72
 
 
73
 
 
74
void SCA_Joystick::HandleEvents()
 
75
{
 
76
        if(m_isinit)
 
77
        {
 
78
                if(SDL_PollEvent(&m_private->m_event))
 
79
                {
 
80
                        switch(m_private->m_event.type)
 
81
                        {
 
82
                        case SDL_JOYAXISMOTION: {HANDLE_AXISMOTION(OnAxisMotion);break;}
 
83
                        case SDL_JOYHATMOTION:  {HANDLE_HATMOTION(OnHatMotion);  break;}
 
84
                        case SDL_JOYBUTTONUP:   {HANDLE_BUTTONUP(OnButtonUp);    break;}
 
85
                        case SDL_JOYBUTTONDOWN: {HANDLE_BUTTONDOWN(OnButtonDown);break;}
 
86
                        case SDL_JOYBALLMOTION: {HANDLE_BALLMOTION(OnBallMotion);break;}
 
87
                        default:                                {HANDLE_NOEVENT(OnNothing);      break;}
 
88
                        }
 
89
                }
 
90
        }
 
91
}
 
92
 
 
93
 
 
94
void SCA_Joystick::cSetPrecision(int val)
 
95
{
 
96
        m_prec = val;
 
97
}
 
98
 
 
99
 
 
100
bool SCA_Joystick::aRightAxisIsPositive(int axis)
 
101
{
 
102
        bool result;
 
103
        int res = pGetAxis(axis,1);
 
104
        res > m_prec? result = true: result = false;
 
105
        m_istrig = result;
 
106
        return result;
 
107
}
 
108
 
 
109
 
 
110
bool SCA_Joystick::aUpAxisIsPositive(int axis)
 
111
{
 
112
        bool result;
 
113
        int res = pGetAxis(axis,0);
 
114
        res < -m_prec? result = true : result = false;
 
115
        m_istrig = result;
 
116
        return result;
 
117
}
 
118
 
 
119
 
 
120
bool SCA_Joystick::aLeftAxisIsPositive(int axis)
 
121
{
 
122
        bool result;
 
123
        int res = pGetAxis(axis,1);
 
124
        res < -m_prec ? result = true : result = false;
 
125
        m_istrig = result;
 
126
        return result;
 
127
}
 
128
 
 
129
 
 
130
bool SCA_Joystick::aDownAxisIsPositive(int axis)
 
131
{
 
132
        bool result;
 
133
        int res = pGetAxis(axis,0);
 
134
        res > m_prec ? result = true:result = false;
 
135
        m_istrig = result;
 
136
        return result;
 
137
}
 
138
 
 
139
 
 
140
bool SCA_Joystick::aButtonPressIsPositive(int button)
 
141
{
 
142
        bool result;
 
143
        SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
 
144
        m_istrig = result;
 
145
        return result;
 
146
}
 
147
 
 
148
 
 
149
bool SCA_Joystick::aButtonReleaseIsPositive(int button)
 
150
{
 
151
        bool result;
 
152
        SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
 
153
        m_istrig = result;
 
154
        return result;
 
155
}
 
156
 
 
157
 
 
158
bool SCA_Joystick::aHatIsPositive(int dir)
 
159
{
 
160
        bool result;
 
161
        int res = pGetHat(dir);
 
162
        res == dir? result = true : result = false;
 
163
        m_istrig = result;
 
164
        return result;
 
165
}
 
166
 
 
167
 
 
168
int SCA_Joystick::pGetButtonPress(int button)
 
169
{
 
170
        if(button == m_buttonnum)
 
171
                return m_buttonnum;
 
172
        return -2;
 
173
}
 
174
 
 
175
 
 
176
int SCA_Joystick::pGetButtonRelease(int button)
 
177
{
 
178
        if(button == m_buttonnum)
 
179
                return m_buttonnum;
 
180
        return -2;
 
181
}
 
182
 
 
183
 
 
184
int SCA_Joystick::pGetHat(int direction)
 
185
{
 
186
        if(direction == m_hatdir){
 
187
                return m_hatdir;
 
188
        }
 
189
        return 0;
 
190
}
 
191
 
 
192
 
 
193
bool SCA_Joystick::GetJoyAxisMotion()
 
194
{
 
195
        bool result = false;
 
196
        if(m_isinit){
 
197
                if(SDL_PollEvent(&m_private->m_event)){
 
198
                        switch(m_private->m_event.type)
 
199
                        {
 
200
                        case SDL_JOYAXISMOTION:
 
201
                                result = true;
 
202
                                break;
 
203
                        }
 
204
                }
 
205
        }
 
206
        return result;
 
207
}
 
208
 
 
209
 
 
210
bool SCA_Joystick::GetJoyButtonPress()
 
211
{
 
212
        bool result = false;
 
213
        if(m_isinit){
 
214
                if(SDL_PollEvent(&m_private->m_event)){
 
215
                        switch(m_private->m_event.type)
 
216
                        {
 
217
                        case SDL_JOYBUTTONDOWN:
 
218
                                result = true;
 
219
                                break;
 
220
                        }
 
221
                }
 
222
        }
 
223
        return result;
 
224
}
 
225
 
 
226
 
 
227
bool SCA_Joystick::GetJoyButtonRelease()
 
228
{
 
229
        bool result = false;
 
230
        if(m_isinit)
 
231
        {
 
232
                if(SDL_PollEvent(&m_private->m_event)){
 
233
                        switch(m_private->m_event.type)
 
234
                        {
 
235
                        case SDL_JOYBUTTONUP:
 
236
                                result = true;
 
237
                                break;
 
238
                        }
 
239
                }
 
240
        }
 
241
        return result;
 
242
}
 
243
 
 
244
 
 
245
bool SCA_Joystick::GetJoyHatMotion()
 
246
{
 
247
        bool result = false;
 
248
        if(m_isinit){
 
249
                if(SDL_PollEvent(&m_private->m_event)){
 
250
                        switch(m_private->m_event.type)
 
251
                        {
 
252
                        case SDL_JOYHATMOTION:
 
253
                                result = true;
 
254
                                break;
 
255
                        }
 
256
                }
 
257
        }
 
258
        return 0;
 
259
}
 
260
 
 
261
 
 
262
int SCA_Joystick::GetNumberOfAxes()
 
263
{
 
264
        int number;
 
265
        if(m_isinit){
 
266
                if(m_private->m_joystick){
 
267
                        number = SDL_JoystickNumAxes(m_private->m_joystick);
 
268
                        return number;
 
269
                }
 
270
        }
 
271
        return -1;
 
272
}
 
273
 
 
274
 
 
275
int SCA_Joystick::GetNumberOfButtons()
 
276
{
 
277
        int number;
 
278
        if(m_isinit){
 
279
                if(m_private->m_joystick){
 
280
                        number = SDL_JoystickNumButtons(m_private->m_joystick);
 
281
                        return number;
 
282
                }
 
283
        }
 
284
        return -1;
 
285
}
 
286
 
 
287
 
 
288
int SCA_Joystick::GetNumberOfHats()
 
289
{
 
290
        int number;
 
291
        if(m_isinit){
 
292
                if(m_private->m_joystick){
 
293
                        number = SDL_JoystickNumHats(m_private->m_joystick);
 
294
                        return number;
 
295
                }
 
296
        }
 
297
        return -1;
 
298
}
 
299
 
 
300
bool SCA_Joystick::pCreateJoystickDevice()
 
301
{
 
302
        if(m_isinit == false){
 
303
                if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) == -1 ){
 
304
                        echo("Error-Initializing-SDL: " << SDL_GetError());
 
305
                        return false;
 
306
                }
 
307
                if(SDL_NumJoysticks() > 0){
 
308
                        for(int i=0; i<SDL_NumJoysticks();i++){
 
309
                                m_private->m_joystick = SDL_JoystickOpen(i);
 
310
                                SDL_JoystickEventState(SDL_ENABLE);
 
311
                                m_numjoys = i;
 
312
                        }
 
313
                        echo("Joystick-initialized");
 
314
                        m_isinit = true;
 
315
                        return true;
 
316
                }else{
 
317
                        echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
 
318
                        return false;
 
319
                }
 
320
        }
 
321
        return false;
 
322
}
 
323
 
 
324
 
 
325
void SCA_Joystick::pDestroyJoystickDevice()
 
326
{
 
327
        echo("Closing-");
 
328
        for(int i=0; i<SDL_NumJoysticks(); i++){
 
329
                if(SDL_JoystickOpened(i)){
 
330
                        SDL_JoystickClose(m_private->m_joystick);
 
331
                }
 
332
        }
 
333
        SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
 
334
}
 
335
 
 
336
 
 
337
void SCA_Joystick::pFillAxes()
 
338
{
 
339
        if(GetNumberOfAxes() == 1){
 
340
                m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
 
341
                m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
 
342
        }else if(GetNumberOfAxes() > 1){
 
343
                m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
 
344
                m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
 
345
                m_axis20 = SDL_JoystickGetAxis(m_private->m_joystick, 2);
 
346
                m_axis21 = SDL_JoystickGetAxis(m_private->m_joystick, 3);
 
347
        }else{
 
348
                m_axis10 = 0;m_axis11 = 0;
 
349
                m_axis20 = 0;m_axis21 = 0;
 
350
        }
 
351
}
 
352
 
 
353
 
 
354
int SCA_Joystick::pGetAxis(int axisnum, int udlr)
 
355
{
 
356
        if(axisnum == 1 && udlr == 1)return m_axis10; //u/d
 
357
        if(axisnum == 1 && udlr == 0)return m_axis11; //l/r
 
358
        if(axisnum == 2 && udlr == 0)return m_axis20; //...
 
359
        if(axisnum == 2 && udlr == 1)return m_axis21;
 
360
        return 0;
 
361
}
 
362