Mana
Loading...
Searching...
No Matches
joystick.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "joystick.h"
23
24#include "configuration.h"
25#include "log.h"
26
27#include <cassert>
28
30
32{
33 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
34
35 // Have SDL call SDL_JoystickUpdate() automatically
36 SDL_JoystickEventState(SDL_ENABLE);
37
38 joystickCount = SDL_NumJoysticks();
39 Log::info("%i joysticks/gamepads found", joystickCount);
40 for (int i = 0; i < joystickCount; i++)
41 Log::info("- %s", SDL_JoystickNameForIndex(i));
42}
43
45 : mDirection(0)
46 , mUpTolerance(config.upTolerance)
47 , mDownTolerance(config.downTolerance)
48 , mLeftTolerance(config.leftTolerance)
49 , mRightTolerance(config.rightTolerance)
50 , mCalibrating(false)
51 , mEnabled(config.joystickEnabled)
52{
53 assert(no < joystickCount);
54
55 mJoystick = SDL_JoystickOpen(no);
56
57 // TODO Bail out!
58 if (!mJoystick)
59 {
60 Log::info("Couldn't open joystick: %s", SDL_GetError());
61 return;
62 }
63
64 Log::info("Axes: %i ", SDL_JoystickNumAxes(mJoystick));
65 Log::info("Balls: %i", SDL_JoystickNumBalls(mJoystick));
66 Log::info("Hats: %i", SDL_JoystickNumHats(mJoystick));
67 Log::info("Buttons: %i", SDL_JoystickNumButtons(mJoystick));
68}
69
71{
72 SDL_JoystickClose(mJoystick);
73}
74
76{
77 mDirection = 0;
78
79 // When calibrating, don't bother the outside with our state
80 if (mCalibrating)
81 {
83 return;
84 };
85
86 if (!mEnabled)
87 return;
88
89 // X-Axis
90 int position = SDL_JoystickGetAxis(mJoystick, 0);
91 if (position >= mRightTolerance)
93 else if (position <= mLeftTolerance)
95
96 // Y-Axis
97 position = SDL_JoystickGetAxis(mJoystick, 1);
98 if (position <= mUpTolerance)
99 mDirection |= UP;
100 else if (position >= mDownTolerance)
101 mDirection |= DOWN;
102
103 // Buttons
104 for (int i = 0; i < MAX_BUTTONS; i++)
105 mButtons[i] = (SDL_JoystickGetButton(mJoystick, i) == 1);
106}
107
109{
110 mUpTolerance = 0;
111 mDownTolerance = 0;
112 mLeftTolerance = 0;
113 mRightTolerance = 0;
114 mCalibrating = true;
115}
116
118{
119 // X-Axis
120 int position = SDL_JoystickGetAxis(mJoystick, 0);
121 if (position > mRightTolerance)
122 mRightTolerance = position;
123 else if (position < mLeftTolerance)
124 mLeftTolerance = position;
125
126 // Y-Axis
127 position = SDL_JoystickGetAxis(mJoystick, 1);
128 if (position > mDownTolerance)
129 mDownTolerance = position;
130 else if (position < mUpTolerance)
131 mUpTolerance = position;
132}
133
142
143bool Joystick::buttonPressed(unsigned char no) const
144{
145 return (mEnabled && no < MAX_BUTTONS) ? mButtons[no] : false;
146}
SDL_Joystick * mJoystick
Definition joystick.h:91
bool mEnabled
Definition joystick.h:95
void finishCalibration()
Definition joystick.cpp:134
static void init()
Initializes the joystick subsystem.
Definition joystick.cpp:31
int mUpTolerance
Definition joystick.h:93
int mLeftTolerance
Definition joystick.h:93
static int joystickCount
Definition joystick.h:97
void startCalibration()
Definition joystick.cpp:108
void update()
Updates the direction and button information.
Definition joystick.cpp:75
bool mButtons[MAX_BUTTONS]
Definition joystick.h:90
Joystick(int no)
Constructor, pass the number of the joystick the new object should access.
Definition joystick.cpp:44
void doCalibration()
Definition joystick.cpp:117
@ MAX_BUTTONS
Definition joystick.h:34
unsigned char mDirection
Definition joystick.h:89
int mRightTolerance
Definition joystick.h:93
bool mCalibrating
Definition joystick.h:94
bool buttonPressed(unsigned char no) const
Definition joystick.cpp:143
int mDownTolerance
Definition joystick.h:93
Config config
Global settings (config.xml)
Definition client.cpp:97
void info(const char *log_text,...) LOG_PRINTF_ATTR
int downTolerance
int leftTolerance
int rightTolerance
int upTolerance