PEBL
2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
sdl/PlatformJoystick.cpp
Go to the documentation of this file.
1
//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3
// Name: src/platforms/sdl/PlatformJoystick.h
4
// Purpose: Class for SDL Joysticks
5
// Author: Shane T. Mueller, Ph.D.
6
// Copyright: (c) 2011-2026 Shane T. Mueller <smueller@obereed.net>
7
// License: GPL 2
8
//
9
//
10
//
11
// This file is part of the PEBL project.
12
//
13
// PEBL is free software; you can redistribute it and/or modify
14
// it under the terms of the GNU General Public License as published by
15
// the Free Software Foundation; either version 2 of the License, or
16
// (at your option) any later version.
17
//
18
// PEBL is distributed in the hope that it will be useful,
19
// but WITHOUT ANY WARRANTY; without even the implied warranty of
20
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
// GNU General Public License for more details.
22
//
23
// You should have received a copy of the GNU General Public License
24
// along with PEBL; if not, write to the Free Software
25
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
28
#include "
PlatformJoystick.h
"
29
#include "SDL.h"
30
31
#include <list>
32
#include <stdio.h>
33
#include "../../utility/PError.h"
34
#include "../../base/PList.h"
35
#include "../../base/PComplexData.h"
36
37
using
std::ostream;
38
using
std::flush;
39
41
PlatformJoystick::PlatformJoystick
(
int
index)
42
{
43
if
((SDL_NumJoysticks()>= (index+1)) &
44
(index>=0))
45
{
46
mJoystick = SDL_JoystickOpen(index);
47
mIndex = index;
48
mIsOpen
=
true
;
49
50
}
else
51
{
52
PError::SignalFatalError
(
Variant
(
"Joystick unavailable: "
) +
Variant
(index));
53
}
54
55
//The joystick API has changed a bit:
56
#ifdef SDL2_DELETE
57
mName
= SDL_JoystickName(mIndex);
58
#endif
59
60
mNumHats
= SDL_JoystickNumHats(mJoystick);
61
mNumBalls
= SDL_JoystickNumBalls(mJoystick);
62
mNumAxes
= SDL_JoystickNumAxes(mJoystick);
63
mNumButtons
= SDL_JoystickNumButtons(mJoystick);
64
65
}
66
67
PlatformJoystick::PlatformJoystick
(
const
PlatformJoystick
& pjs)
68
{
69
mJoystick =
NULL
;
70
}
71
73
PlatformJoystick::~PlatformJoystick
()
74
{
75
76
SDL_JoystickClose(mJoystick);
77
78
}
79
80
81
signed
int
PlatformJoystick::GetHatState
(
unsigned
int
hat)
82
{
83
84
//check to be sure hat exists.
85
if
(
GetNumHats
()<hat)
86
{
87
PError::SignalWarning
(
Variant
(
"Unavailable joystick hat "
)+
Variant
((
int
)hat)+
Variant
(
" requested"
));
88
return
0;
89
}
90
91
92
SDL_JoystickUpdate();
93
return
SDL_JoystickGetHat(mJoystick,hat-1);
94
}
95
96
97
Variant
PlatformJoystick::GetBallState
(
unsigned
int
ball)
98
{
99
if
(
GetNumBalls
()<ball)
100
{
101
PError::SignalWarning
(
Variant
(
"Unavailable joystick ball "
)+
Variant
((
int
)ball)+
Variant
(
" requested"
));
102
return
0;
103
}
104
105
106
//check to be sure ball exists.
107
SDL_JoystickUpdate();
108
int
dx;
109
int
dy;
110
SDL_JoystickGetBall(mJoystick,ball-1,&dx,&dy);
111
112
PList
* newlist =
new
PList
();
113
newlist->
PushBack
(
Variant
(dx));
114
newlist->
PushBack
(
Variant
(dy));
115
counted_ptr<PEBLObjectBase>
newlist2 =
counted_ptr<PEBLObjectBase>
(newlist);
116
PComplexData
* pcd =
new
PComplexData
(newlist2);
117
return
Variant
(pcd);
118
119
120
}
121
122
signed
int
PlatformJoystick::GetAxisState
(
unsigned
int
axis)
123
{
124
125
// if(GetNumAxes()<axis)
126
// {
127
// PError::SignalWarning(Variant("Unavailable joystick axis ")+Variant((int)axis)+Variant(" requested"));
128
// return 0;
129
// }
130
131
SDL_JoystickUpdate();
132
//check to be sure axis exists.
133
return
SDL_JoystickGetAxis(mJoystick,axis-1);
134
}
135
136
signed
int
PlatformJoystick::GetButtonState
(
unsigned
int
button)
137
{
138
139
if
(
GetNumButtons
()<button)
140
{
141
PError::SignalWarning
(
Variant
(
"Unavailable joystick button "
)+
Variant
((
int
)button)+
Variant
(
" requested"
));
142
return
0;
143
}
144
145
SDL_JoystickUpdate();
146
//check to be sure button exists.
147
return
SDL_JoystickGetButton(mJoystick,button-1);
148
149
}
150
151
152
// Inheritable function that is called by friend method << operator of PComplexData
153
ostream &
PlatformJoystick::SendToStream
(ostream& out)
const
154
{
155
156
out <<
"<PEBL Joystick: "
<<
mName
<<
">"
<< flush;
157
158
return
out;
159
}
160
NULL
#define NULL
Definition
BinReloc.cpp:317
PComplexData
Definition
PComplexData.h:88
PJoystick::mName
Variant mName
Definition
PJoystick.h:68
PJoystick::mNumButtons
int mNumButtons
Definition
PJoystick.h:72
PJoystick::mNumHats
int mNumHats
Definition
PJoystick.h:69
PJoystick::mIsOpen
bool mIsOpen
Definition
PJoystick.h:74
PJoystick::mNumAxes
int mNumAxes
Definition
PJoystick.h:71
PJoystick::GetNumButtons
int GetNumButtons()
Definition
PJoystick.h:55
PJoystick::GetNumHats
int GetNumHats()
Definition
PJoystick.h:52
PJoystick::GetNumBalls
int GetNumBalls()
Definition
PJoystick.h:53
PJoystick::mNumBalls
int mNumBalls
Definition
PJoystick.h:70
PList
Definition
PList.h:45
PList::PushBack
void PushBack(const Variant &v)
Definition
PList.cpp:149
PlatformJoystick
Definition
sdl/PlatformJoystick.h:38
PlatformJoystick::~PlatformJoystick
virtual ~PlatformJoystick()
The Standard destructor.
Definition
sdl/PlatformJoystick.cpp:73
PlatformJoystick::GetButtonState
virtual signed int GetButtonState(unsigned int button)
Definition
sdl/PlatformJoystick.cpp:136
PlatformJoystick::GetBallState
virtual Variant GetBallState(unsigned int ball)
Definition
sdl/PlatformJoystick.cpp:97
PlatformJoystick::PlatformJoystick
PlatformJoystick()
Definition
validator/PlatformJoystick.cpp:4
PlatformJoystick::SendToStream
virtual std::ostream & SendToStream(std::ostream &out) const
Definition
validator/PlatformJoystick.cpp:13
PlatformJoystick::GetHatState
virtual signed int GetHatState(unsigned int hat)
Definition
sdl/PlatformJoystick.cpp:81
PlatformJoystick::GetAxisState
virtual signed int GetAxisState(unsigned int axis)
Definition
sdl/PlatformJoystick.cpp:122
Variant
Definition
Variant.h:67
counted_ptr
Definition
rc_ptrs.h:66
PError::SignalWarning
void SignalWarning(const std::string &message)
Definition
PError.cpp:119
PError::SignalFatalError
void SignalFatalError(const std::string &message)
PlatformJoystick.h
src
platforms
sdl
PlatformJoystick.cpp
Generated by
1.9.8