Change Details
src/inputmanager.cpp |
30 | 30 | |
31 | 31 | using namespace std; |
32 | 32 | |
33 | | static SDL_Joystick *joystick; |
34 | | |
35 | 33 | void InputManager::init(const string &conffile) { |
36 | 34 | if (!readConfFile(conffile)) { |
37 | 35 | ERROR("InputManager initialization from config file failed.\n"); |
38 | 36 | } |
39 | 37 | } |
40 | 38 | |
41 | | InputManager::InputManager() { |
42 | | initJoystick(); |
43 | | } |
44 | | |
45 | | InputManager::~InputManager() { |
| 39 | InputManager::InputManager() |
| 40 | : joystick(NULL) |
| 41 | { |
46 | 42 | if (SDL_NumJoysticks() > 0) { |
47 | | SDL_JoystickClose(joystick); |
| 43 | joystick = SDL_JoystickOpen(0); |
48 | 44 | } |
49 | 45 | } |
50 | 46 | |
51 | | void InputManager::initJoystick() { |
52 | | if (SDL_NumJoysticks() > 0) { |
53 | | joystick = SDL_JoystickOpen(0); |
| 47 | InputManager::~InputManager() { |
| 48 | if (joystick) { |
| 49 | SDL_JoystickClose(joystick); |
54 | 50 | } |
55 | 51 | } |
56 | 52 | |
... | ... | |
134 | 130 | //TODO: when an event is processed, program a new event |
135 | 131 | //in some time, and when it occurs, do a key repeat |
136 | 132 | |
137 | | SDL_JoystickUpdate(); |
| 133 | if (joystick) { |
| 134 | SDL_JoystickUpdate(); |
| 135 | } |
138 | 136 | SDL_Event event; |
139 | 137 | if (wait) { |
140 | 138 | SDL_WaitEvent(&event); |
src/inputmanager.h |
23 | 23 | |
24 | 24 | #include <string> |
25 | 25 | |
| 26 | typedef struct _SDL_Joystick SDL_Joystick; |
| 27 | |
26 | 28 | class InputManager { |
27 | 29 | public: |
28 | 30 | enum Button { |
... | ... | |
58 | 60 | unsigned int code; |
59 | 61 | }; |
60 | 62 | |
61 | | ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE]; |
62 | | |
63 | 63 | bool readConfFile(const std::string &conffile); |
64 | | void initJoystick(); |
65 | 64 | bool getEvent(ButtonEvent *bevent, bool wait); |
66 | 65 | Button waitForButton(ButtonState state); |
| 66 | |
| 67 | ButtonMapEntry buttonMap[BUTTON_TYPE_SIZE]; |
| 68 | SDL_Joystick *joystick; |
67 | 69 | }; |
68 | 70 | |
69 | 71 | #endif |
Download the corresponding diff file