Repository navigation
Expand file tree
/
Copy pathInput.cpp
More file actions
132 lines (109 loc) · 2.1 KB
/
Copy pathInput.cpp
File metadata and controls
132 lines (109 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <vector>
#include "Input.h"
#include "Game.h"
InputHandler* InputHandler::s_pInstance = 0;
InputHandler::InputHandler() : m_keystates(0), m_mousePosition(new Vector(0, 0))
{
for (int i = 0; i < 3; i++)
{
m_mouseButtonStates.push_back(false);
}
}
void InputHandler::clean()
{ }
void InputHandler::update()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
Simon::Instance()->quit();
break;
case SDL_MOUSEMOTION:
onMouseMove(event);
break;
case SDL_MOUSEBUTTONDOWN:
onMouseButtonDown(event);
break;
case SDL_MOUSEBUTTONUP:
onMouseButtonUp(event);
break;
case SDL_KEYDOWN:
onKeyDown();
break;
case SDL_KEYUP:
onKeyUp();
break;
default:
break;
}
}
}
void InputHandler::onMouseButtonDown(SDL_Event& event)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
m_mouseButtonStates[LEFT] = true;
}
if (event.button.button == SDL_BUTTON_MIDDLE)
{
m_mouseButtonStates[MIDDLE] = true;
}
if (event.button.button == SDL_BUTTON_RIGHT)
{
m_mouseButtonStates[RIGHT] = true;
}
}
void InputHandler::onMouseButtonUp(SDL_Event& event)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
m_mouseButtonStates[LEFT] = false;
}
if (event.button.button == SDL_BUTTON_MIDDLE)
{
m_mouseButtonStates[MIDDLE] = false;
}
if (event.button.button == SDL_BUTTON_RIGHT)
{
m_mouseButtonStates[RIGHT] = false;
}
}
void InputHandler::onMouseMove(SDL_Event& event)
{
m_mousePosition->setX(event.motion.x);
m_mousePosition->setY(event.motion.y);
}
void InputHandler::onKeyDown()
{
m_keystates = SDL_GetKeyboardState(0);
}
void InputHandler::onKeyUp()
{
m_keystates = SDL_GetKeyboardState(0);
}
bool InputHandler::isKeyDown(SDL_Scancode key)
{
if (m_keystates != 0)
{
if (m_keystates[key] == 1)
{
return true;
}
else
{
return false;
}
}
return false;
}
void InputHandler::reset()
{
for (int i = 0; i < 3; i++)
{
m_mouseButtonStates.push_back(false);
}
}