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 "export.hpp"
33#include "nomlib/config.hpp"
34#include "nomlib/system/Joystick.hpp"
35
36// Forward declarations (third-party)
37typedef struct _SDL_Joystick SDL_Joystick;
38typedef struct _SDL_GameController SDL_GameController;
39
40namespace nom {
41
47NOM_EXPORT bool init_game_controller_subsystem();
48
50NOM_EXPORT void shutdown_game_controller_subsystem();
51
53NOM_EXPORT void GameControllerDeleter(SDL_GameController* dev);
54
56class NOM_EXPORT GameController
57{
58 public:
60 enum Axis: int8
61 {
62 AXIS_INVALID = -1,
63 AXIS_LEFT_X,
64 AXIS_LEFT_Y,
65 AXIS_RIGHT_X,
66 AXIS_RIGHT_Y,
67 AXIS_TRIGGER_LEFT,
68 AXIS_TRIGGER_RIGHT,
69 AXIS_MAX,
70 };
71
73 enum Button: int8
74 {
75 BUTTON_INVALID = -1,
76 BUTTON_A,
77 BUTTON_B,
78 BUTTON_X,
79 BUTTON_Y,
80 BUTTON_BACK,
81 BUTTON_GUIDE,
82 BUTTON_START,
83 BUTTON_LEFT_STICK,
84 BUTTON_RIGHT_STICK,
85 BUTTON_LEFT_SHOULDER,
86 BUTTON_RIGHT_SHOULDER,
87 BUTTON_DPAD_UP,
88 BUTTON_DPAD_DOWN,
89 BUTTON_DPAD_LEFT,
90 BUTTON_DPAD_RIGHT,
91 BUTTON_MAX = SDL_CONTROLLER_BUTTON_MAX,
92 };
93
94 // NOTE: This is a workaround to prevent a double-free bug that
95 // occasionally happens when the joystick device is closed more than once.
96 // This issue can be reliably reproduced by adding a second
97 // SDL_GameControllerClose call to test/testgamecontroller.c from the SDL
98 // source repository.
99 NOM_EXPORT static bool device_closed_;
100
101 typedef GameController self_type;
102
104
106
110 SDL_Joystick* device() const;
111
116 bool attached() const;
117
122 JoystickID device_id() const;
123
128 std::string name() const;
129
139 bool open(JoystickIndex device_index);
140
144 void close();
145
157 NOM_EXPORT static std::string name(JoystickIndex device_index);
158
174 NOM_EXPORT static int set_event_state(int state);
175
180 NOM_EXPORT static void update();
181
182 std::string mapping_string();
183
185 NOM_EXPORT static std::string mapping_string(JoystickGUID guid);
186
188 NOM_EXPORT
189 static int load_mapping_memory(const char* buffer, int buffer_size);
190
192 NOM_EXPORT static int load_mapping_file(const std::string& filename);
193
194 NOM_EXPORT static int load_mapping_string(const std::string& mapping);
195
207 NOM_EXPORT static JoystickID device_id(JoystickIndex device_index);
208
211 NOM_EXPORT static bool compatible_joystick(JoystickIndex device_index);
212
213 private:
214 typedef std::unique_ptr<SDL_GameController, void (*)(SDL_GameController*)>
215 joystick_dev;
216
217 joystick_dev device_;
218};
219
220NOM_EXPORT std::unique_ptr<GameController> make_unique_game_controller();
221NOM_EXPORT std::shared_ptr<GameController> make_shared_game_controller();
222
223} // namespace nom
224
225#endif // include guard defined
226
SDL2 game controller interface.
static NOM_EXPORT std::string name(JoystickIndex device_index)
Get the implementation-dependent name of the joystick.
static NOM_EXPORT JoystickID device_id(JoystickIndex device_index)
Get the instance ID of a joystick device.
JoystickID device_id() const
Get the instance ID of an opened game controller.
bool open(JoystickIndex device_index)
Open a joystick for use.
static NOM_EXPORT std::string mapping_string(JoystickGUID guid)
SDL_Joystick * device() const
Get the underlying implementation pointer.
Button
Game controller button definitions.
std::string name() const
Get the implementation-dependent name of the joystick.
static NOM_EXPORT int load_mapping_memory(const char *buffer, int buffer_size)
static NOM_EXPORT void update()
Update the current state of the opened game controllers.
static NOM_EXPORT int load_mapping_file(const std::string &filename)
Axis
Game controller axis definitions.
bool attached() const
Get the status of the joystick.
void close()
Close an opened joystick.
static NOM_EXPORT bool compatible_joystick(JoystickIndex device_index)
Check to see if the joystick is supported by SDL's game controller interface.
static NOM_EXPORT int set_event_state(int state)
Toggle the game controller's event polling state.
Internal representation of a stable, fixed identifier – 128 bits wide – for a joystick device....
Definition Joystick.hpp:59