-
Notifications
You must be signed in to change notification settings - Fork 0
/
AEEvent.cpp
232 lines (186 loc) · 5.24 KB
/
AEEvent.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "AEEvent.h"
using namespace std;
using namespace boost;
namespace AE
{
MouseEvents::MouseEvents() : right(false), left(false), middle(false), wheel_down(false), wheel_up(false)
{
}
Event::Event() : m_keys_pressed( new bool[SDLK_LAST] ), m_there_is_event(false), m_type_event(-1)
{
memset(m_keys_pressed, false, SDLK_LAST); // On remplit m_keys_pressed de falses (bien sûr, puisque aucune touche n'a été pressée, non :D ? )
}
Event::Event(const Event &a_copier) : m_type_event(-1)
{
m_event = a_copier.m_event;
m_there_is_event = a_copier.m_there_is_event;
m_keys_pressed = new bool[SDLK_LAST];
memcpy(m_keys_pressed, a_copier.m_keys_pressed, SDLK_LAST);
m_mouse_events = a_copier.m_mouse_events;
}
Event::~Event()
{
}
bool Event::poll()
{
m_there_is_event = SDL_PollEvent( &m_event );
/*if( mouseButtonDown(SDL_BUTTON_RIGHT) )
m_mouse_button_right_pressed = true;
else if( mouseButtonDown(SDL_BUTTON_LEFT) )
m_mouse_button_left_pressed = true;
if( mouseButtonUp(SDL_BUTTON_RIGHT) )
m_mouse_button_right_pressed = false;
if( mouseButtonDown(SDL_BUTTON_LEFT) )
m_mouse_button_left_pressed = false;*/
majVars();
return m_there_is_event;
}
void Event::majVars()
{
if(m_there_is_event) // S'il y a eu un event
{
m_type_event = m_event.type;
if(m_type_event == SDL_KEYDOWN)
m_keys_pressed[m_event.key.keysym.sym] = true; // On rend la touche event.key.keysym.sym enfoncée
else if(m_type_event == SDL_KEYUP)
m_keys_pressed[m_event.key.keysym.sym] = false; // L'utilisateur a lâché la touche, donc elle n'est plus pressée
if(m_type_event == SDL_MOUSEBUTTONDOWN || m_type_event == SDL_MOUSEBUTTONUP)
{
bool affect;
if(m_type_event == SDL_MOUSEBUTTONDOWN)
{
affect = true;
}
if(m_type_event == SDL_MOUSEBUTTONUP)
{
affect = false;
}
switch(m_event.button.button)
{
case SDL_BUTTON_RIGHT:
m_mouse_events.right = affect;
break;
case SDL_BUTTON_LEFT:
m_mouse_events.left = affect;
break;
case SDL_BUTTON_MIDDLE:
m_mouse_events.middle = affect;
break;
case SDL_BUTTON_WHEELUP:
m_mouse_events.wheel_up = affect;
break;
case SDL_BUTTON_WHEELDOWN:
m_mouse_events.wheel_down = affect;
break;
};
m_mouse_events.button_position.x = m_event.button.x;
m_mouse_events.button_position.y = m_event.button.y;
}
if(m_type_event == SDL_MOUSEMOTION)
{
m_mouse_events.motion_position.x = m_event.motion.x;
m_mouse_events.motion_position.y = m_event.motion.y;
SDL_GetRelativeMouseState( &m_mouse_events.relative_position.x, &m_mouse_events.relative_position.y);
}
}
}
bool Event::wait()
{
if( SDL_WaitEvent( &m_event ) == 0) // Aucune erreur
{
m_there_is_event = true; // Il y a eu un évènement car SDL_WaitEvent attend jusqu'à ce qu'un event se passe
return true;
}
else
return false;
}
int Event::type() // Retourne le type d'évènement
{
return m_type_event;
}
bool Event::keyPressed(int keysym)
{
if(keysym >= 0 && keysym <= SDLK_LAST)
return m_keys_pressed[keysym];
else
return false;
}
bool Event::keyUp(int keysym)
{
if(!m_there_is_event)
return false;
if(m_type_event == SDL_KEYUP)
return (m_event.key.keysym.sym == keysym);
else
return false;
}
bool Event::keyDown(int keysym)
{
if(!m_there_is_event)
return false;
if(m_type_event == SDL_KEYDOWN)
return (m_event.key.keysym.sym == keysym);
else
return false;
}
bool Event::mouseButtonDown(int button)
{
if(!m_there_is_event)
return false;
if(m_type_event == SDL_MOUSEBUTTONDOWN)
return m_event.button.button == button;
else
return false;
}
bool Event::mouseButtonUp(int button)
{
if(!m_there_is_event)
return false;
if(m_type_event == SDL_MOUSEBUTTONUP)
return m_event.button.button == button;
else
return false;
}
bool Event::mouseButtonPressed(int button)
{
if(button == SDL_BUTTON_RIGHT)
return m_mouse_events.right;
else if(button == SDL_BUTTON_LEFT)
return m_mouse_events.left;
else if(button == SDL_BUTTON_MIDDLE)
return m_mouse_events.middle;
else if(button == SDL_BUTTON_WHEELDOWN)
return m_mouse_events.wheel_down;
else if(button == SDL_BUTTON_WHEELUP)
return m_mouse_events.wheel_up;
else
return false;
}
IVector2D Event::mouseButtonPosition()
{
return m_mouse_events.button_position;
}
bool Event::mouseMotion()
{
if(m_there_is_event)
return (m_type_event == SDL_MOUSEMOTION);
else
return false;
}
IVector2D Event::mouseMotionPosition()
{
return m_mouse_events.motion_position;
}
IVector2D Event::mouseRelativePosition()
{
return m_mouse_events.relative_position;
}
void Event::operator=(Event &a_copier)
{
m_event = a_copier.m_event;
m_there_is_event = a_copier.m_there_is_event;
m_keys_pressed = new bool[SDLK_LAST];
memcpy(m_keys_pressed, a_copier.m_keys_pressed, SDLK_LAST);
m_mouse_events = a_copier.m_mouse_events;
}
}