Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Joystick.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #include "Joystick.h"
21 
23 #include "ps/CLogger.h"
24 #include "ps/ConfigDB.h"
25 
27 
29  m_Joystick(NULL), m_Deadzone(0)
30 {
31 }
32 
34 {
35  bool joystickEnable = false;
36  CFG_GET_VAL("joystick.enable", Bool, joystickEnable);
37  if (!joystickEnable)
38  return;
39 
40  CFG_GET_VAL("joystick.deadzone", Int, m_Deadzone);
41 
43  {
44  LOGERROR(L"CJoystick::Initialise failed to initialise joysticks (\"%hs\")", SDL_GetError());
45  return;
46  }
47 
48  int numJoysticks = SDL_NumJoysticks();
49 
50  LOGMESSAGE(L"Found %d joystick(s)", numJoysticks);
51 
52  for (int i = 0; i < numJoysticks; ++i)
53  {
54 #if SDL_VERSION_ATLEAST(2, 0, 0)
55  SDL_Joystick* stick = SDL_JoystickOpen(i);
56  if (!stick)
57  {
58  LOGERROR(L"CJoystick::Initialise failed to open joystick %d (\"%hs\")", i, SDL_GetError());
59  continue;
60  }
61  const char* name = SDL_JoystickName(stick);
62 #else // SDL 1.2
63  const char* name = SDL_JoystickName(i);
64 #endif
65  LOGMESSAGE(L"Joystick %d: %hs", i, name);
66 #if SDL_VERSION_ATLEAST(2, 0, 0)
67  SDL_JoystickClose(stick);
68 #endif
69  }
70 
71  if (numJoysticks)
72  {
74 
75  // Always pick the first joystick, and assume that's the right one
77  if (!m_Joystick)
78  LOGERROR(L"CJoystick::Initialise failed to open joystick (\"%hs\")", SDL_GetError());
79  }
80 }
81 
83 {
84  return (m_Joystick != NULL);
85 }
86 
87 float CJoystick::GetAxisValue(int axis)
88 {
89  if (!m_Joystick || axis < 0 || axis >= SDL_JoystickNumAxes(m_Joystick))
90  return 0.f;
91 
93 
94  // Normalize values into the range [-1, +1] excluding the deadzone around 0
95  float norm;
96  if (val > m_Deadzone)
97  norm = (val - m_Deadzone) / (float)(32767 - m_Deadzone);
98  else if (val < -m_Deadzone)
99  norm = (val + m_Deadzone) / (float)(32767 - m_Deadzone);
100  else
101  norm = 0.f;
102 
103  return norm;
104 }
int SDL_JoystickNumAxes(SDL_Joystick *joystick)
Definition: wsdl.cpp:746
int SDL_JoystickEventState(int state)
Definition: wsdl.cpp:731
#define LOGERROR
Definition: CLogger.h:35
void Initialise()
Initialises joystick support.
Definition: Joystick.cpp:33
float GetAxisValue(int axis)
Returns current value of the given joystick axis, in the range [-1, +1].
Definition: Joystick.cpp:87
#define CFG_GET_VAL(name, type, destination)
Definition: ConfigDB.h:147
#define LOGMESSAGE
Definition: CLogger.h:32
short int16_t
Definition: wposix_types.h:38
void * SDL_Joystick
Definition: wsdl.h:157
CJoystick g_Joystick
Definition: Joystick.cpp:26
SDL_Joystick * m_Joystick
Definition: Joystick.h:44
Sint16 SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis)
Definition: wsdl.cpp:751
bool IsEnabled()
Returns true if initialised and the joystick is present and enabled by configuration.
Definition: Joystick.cpp:82
SDL_Joystick * SDL_JoystickOpen(int device_index)
Definition: wsdl.cpp:741
const char * SDL_JoystickName(int device_index)
Definition: wsdl.cpp:736
int SDL_InitSubSystem(Uint32 flags)
Definition: wsdl.cpp:1500
#define SDL_GetError()
Definition: wsdl.h:334
#define SDL_INIT_JOYSTICK
Definition: wsdl.h:46
int SDL_NumJoysticks()
Definition: wsdl.cpp:726
int m_Deadzone
Definition: Joystick.h:45