-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputHandler.h
108 lines (81 loc) · 2.48 KB
/
InputHandler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// InputHandler.h
// SDL Game Programming Book
//
// Created by shaun mitchell on 24/01/2013.
// Copyright (c) 2013 shaun mitchell. All rights reserved.
//
#ifndef __SDL_Game_Programming_Book__InputHandler__
#define __SDL_Game_Programming_Book__InputHandler__
#include <iostream>
#include <vector>
#include "SDL.h"
#include <utils/Vector2D.h>
enum mouse_buttons
{
LEFT = 0,
MIDDLE = 1,
RIGHT = 2
};
class InputHandler
{
public:
static InputHandler* Instance()
{
if(s_pInstance == 0)
{
s_pInstance = new InputHandler();
}
return s_pInstance;
}
// init joysticks
void initialiseJoysticks();
bool joysticksInitialised() const { return m_bJoysticksInitialised; }
void reset();
// update and clean the input handler
void update();
void clean();
// keyboard events
bool isKeyDown(SDL_Scancode key) const;
// joystick events
int getAxisX(int joy, int stick) const;
int getAxisY(int joy, int stick) const;
bool getButtonState(int joy, int buttonNumber) const;
// mouse events
bool getMouseButtonState(int buttonNumber) const;
Vector2D const& getMousePosition() const;
private:
InputHandler();
~InputHandler();
InputHandler(const InputHandler&);
InputHandler& operator=(const InputHandler&);
// private functions to handle different event types
// handle keyboard events
void onKeyDown();
void onKeyUp();
// handle mouse events
void onMouseMove(SDL_Event& event);
void onMouseButtonDown(SDL_Event& event);
void onMouseButtonUp(SDL_Event& event);
// handle joysticks events
void onJoystickAxisMove(SDL_Event& event);
void onJoystickButtonDown(SDL_Event& event);
void onJoystickButtonUp(SDL_Event& event);
// member variables
// keyboard specific
const Uint8* m_keystates;
// joystick specific
std::vector<std::pair<Vector2D*, Vector2D*>> m_joystickValues;
std::vector<SDL_Joystick*> m_joysticks;
std::vector<std::vector<bool>> m_buttonStates;
bool m_bJoysticksInitialised;
static const int m_joystickDeadZone = 10000;
// mouse specific
std::vector<bool> m_mouseButtonStates;
Vector2D m_mousePosition;
bool m_logMouseClicks;
// singleton
static InputHandler* s_pInstance;
};
typedef InputHandler TheInputHandler;
#endif /* defined(__SDL_Game_Programming_Book__InputHandler__) */