forked from lokesh-sharma/GameEngine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputHandler.h
More file actions
90 lines (84 loc) · 2.1 KB
/
InputHandler.h
File metadata and controls
90 lines (84 loc) · 2.1 KB
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
#ifndef INPUTHANDLER_H
#define INPUTHANDLER_H
#include<SDL2/SDL.h>
#include<vector>
#include<glm/glm.hpp>
class Display;
enum mouse_buttons
{
LEFT=0,
MIDDLE=1,
RIGHT=2
};
enum mouse_scroll
{
UP=0,
DOWN=1
};
enum move_keys
{
W_KEY=0,
A_KEY=0,
S_KEY=0,
D_KEY=0
};
class InputHandler
{
private:
static InputHandler* pInstance;
const Uint8* m_keystate;
std::vector<bool>m_mouseButtonStates;
std::vector<bool>m_mouseWheelStates;
std::vector<bool>m_movementKeysStates;
glm::vec2 m_mousePos;
Display* display;
InputHandler() : m_keystate(NULL) ,m_mousePos( glm::vec2(0,0))
{
for(int i = 0 ; i<3 ; i++)
m_mouseButtonStates.push_back(false);
for(int i = 0 ; i<2 ; i++)
m_mouseWheelStates.push_back(false);
for(int i = 0 ; i<4 ; i++)
m_movementKeysStates.push_back(false);
}
void onKeyDown();
void onKeyUp();
void onMouseMove(SDL_Event& event);
void onMouseButtonDown(SDL_Event& event);
void onMouseButtonUp(SDL_Event& event);
void onMouseWheel(SDL_Event& event);
void onDrag(SDL_Event& event);
public:
static InputHandler* getInstance()
{
if(pInstance==0)
{
pInstance = new InputHandler();
return pInstance;
}
else return pInstance;
}
void update(Display& d);
bool isKeyDown(SDL_Scancode key);
void clean() {};
bool getMouseButtonState(int button)
{ return m_mouseButtonStates[button];}
void resetStates()
{
m_mouseWheelStates[UP]=m_mouseWheelStates[DOWN]=0;
m_mouseButtonStates[LEFT] = 0;
m_mouseButtonStates[MIDDLE] = 0;
m_mouseButtonStates[RIGHT] = 0;
}
void setDisplay(Display*d) { display = d;}
void setMousePos(int x , int y);
void disableCursor() {SDL_ShowCursor(SDL_DISABLE);}
bool getMouseWheelState(int motion)
{
return m_mouseWheelStates[motion];
}
glm::vec2 getMousePos() { return m_mousePos;}
Display*getDisplay() { return display ;}
};
typedef InputHandler TheInputHandler;
#endif // INPUTHANDLER_