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
37using std::ostream;
38using std::flush;
39
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
68{
69 mJoystick = NULL;
70}
71
74{
75
76 SDL_JoystickClose(mJoystick);
77
78}
79
80
81signed 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
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));
116 PComplexData * pcd = new PComplexData(newlist2);
117 return Variant(pcd);
118
119
120 }
121
122signed 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
136signed 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
153ostream & PlatformJoystick::SendToStream(ostream& out) const
154{
155
156 out << "<PEBL Joystick: " << mName << ">" << flush;
157
158 return out;
159}
160
#define NULL
Definition BinReloc.cpp:317
Variant mName
Definition PJoystick.h:68
int mNumButtons
Definition PJoystick.h:72
int mNumHats
Definition PJoystick.h:69
bool mIsOpen
Definition PJoystick.h:74
int mNumAxes
Definition PJoystick.h:71
int GetNumButtons()
Definition PJoystick.h:55
int GetNumHats()
Definition PJoystick.h:52
int GetNumBalls()
Definition PJoystick.h:53
int mNumBalls
Definition PJoystick.h:70
Definition PList.h:45
void PushBack(const Variant &v)
Definition PList.cpp:149
virtual ~PlatformJoystick()
The Standard destructor.
virtual signed int GetButtonState(unsigned int button)
virtual Variant GetBallState(unsigned int ball)
virtual std::ostream & SendToStream(std::ostream &out) const
virtual signed int GetHatState(unsigned int hat)
virtual signed int GetAxisState(unsigned int axis)
void SignalWarning(const std::string &message)
Definition PError.cpp:119
void SignalFatalError(const std::string &message)