nomlib
Loading...
Searching...
No Matches
GameController.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_SYSTEM_GAME_CONTROLLER_HPP
30#define NOMLIB_SYSTEM_GAME_CONTROLLER_HPP
31
32#include "nomlib/config.hpp"
33#include "nomlib/system/Joystick.hpp"
34
35// Forward declarations (third-party)
36typedef struct _SDL_Joystick SDL_Joystick;
37typedef struct _SDL_GameController SDL_GameController;
38
39namespace nom {
40
46bool init_game_controller_subsystem();
47
49void shutdown_game_controller_subsystem();
50
52void GameControllerDeleter(SDL_GameController* dev);
53
55class GameController
56{
57 public:
59 enum Axis: int8
60 {
61 AXIS_INVALID = -1,
62 AXIS_LEFT_X,
63 AXIS_LEFT_Y,
64 AXIS_RIGHT_X,
65 AXIS_RIGHT_Y,
66 AXIS_TRIGGER_LEFT,
67 AXIS_TRIGGER_RIGHT,
68 AXIS_MAX,
69 };
70
72 enum Button: int8
73 {
74 BUTTON_INVALID = -1,
75 BUTTON_A,
76 BUTTON_B,
77 BUTTON_X,
78 BUTTON_Y,
79 BUTTON_BACK,
80 BUTTON_GUIDE,
81 BUTTON_START,
82 BUTTON_LEFT_STICK,
83 BUTTON_RIGHT_STICK,
84 BUTTON_LEFT_SHOULDER,
85 BUTTON_RIGHT_SHOULDER,
86 BUTTON_DPAD_UP,
87 BUTTON_DPAD_DOWN,
88 BUTTON_DPAD_LEFT,
89 BUTTON_DPAD_RIGHT,
90 BUTTON_MAX = SDL_CONTROLLER_BUTTON_MAX,
91 };
92
93 // NOTE: This is a workaround to prevent a double-free bug that
94 // occasionally happens when the joystick device is closed more than once.
95 // This issue can be reliably reproduced by adding a second
96 // SDL_GameControllerClose call to test/testgamecontroller.c from the SDL
97 // source repository.
98 static bool device_closed_;
99
100 typedef GameController self_type;
101
103
105
109 SDL_Joystick* device() const;
110
115 bool attached() const;
116
121 JoystickID device_id() const;
122
127 std::string name() const;
128
138 bool open(JoystickIndex device_index);
139
143 void close();
144
156 static std::string name(JoystickIndex device_index);
157
173 static int set_event_state(int state);
174
179 static void update();
180
181 std::string mapping_string();
182
184 static std::string mapping_string(JoystickGUID guid);
185
187 static int load_mapping_memory(const char* buffer, int buffer_size);
188
190 static int load_mapping_file(const std::string& filename);
191
192 static int load_mapping_string(const std::string& mapping);
193
205 static JoystickID device_id(JoystickIndex device_index);
206
209 static bool compatible_joystick(JoystickIndex device_index);
210
211 private:
212 typedef std::unique_ptr<SDL_GameController, void (*)(SDL_GameController*)>
213 joystick_dev;
214
215 joystick_dev device_;
216};
217
218std::unique_ptr<GameController> make_unique_game_controller();
219std::shared_ptr<GameController> make_shared_game_controller();
220
221} // namespace nom
222
223#endif // include guard defined
224
SDL2 game controller interface.
static void update()
Update the current state of the opened game controllers.
JoystickID device_id() const
Get the instance ID of an opened game controller.
static std::string name(JoystickIndex device_index)
Get the implementation-dependent name of the joystick.
static int set_event_state(int state)
Toggle the game controller's event polling state.
bool open(JoystickIndex device_index)
Open a joystick for use.
static bool compatible_joystick(JoystickIndex device_index)
Check to see if the joystick is supported by SDL's game controller interface.
static std::string mapping_string(JoystickGUID guid)
SDL_Joystick * device() const
Get the underlying implementation pointer.
static int load_mapping_memory(const char *buffer, int buffer_size)
Button
Game controller button definitions.
std::string name() const
Get the implementation-dependent name of the joystick.
static JoystickID device_id(JoystickIndex device_index)
Get the instance ID of a joystick device.
Axis
Game controller axis definitions.
static int load_mapping_file(const std::string &filename)
bool attached() const
Get the status of the joystick.
void close()
Close an opened joystick.
Internal representation of a stable, fixed identifier – 128 bits wide – for a joystick device....
Definition Joystick.hpp:58