-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventHandler.cpp
338 lines (308 loc) · 10.2 KB
/
EventHandler.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <typeinfo>
#include "GLHeader.h"
#include <iostream>
#include <Box2D/Box2D.h>
#include "EventHandler.h"
#include "Walls.h"
#include "DestroyableIntf.h"
#include "TraceLogger.h"
#include "Ships.h"
#include "Star.h"
#include <sys/time.h>
EventHandler::EventHandler(SDL_Window *pWindow, b2World& world, Ships& ships, Walls& walls):
m_bQuit(false),
m_pWindow(pWindow),
m_ships(ships),
m_timeStep(1.0f / 60.0f),
m_velocityIterations(6),
m_positionIterations(2),
m_world(world),
m_walls(walls)
{
//Setup a new SDL event to handle controls from whatever source
//currently, WiiMote
m_controlEvent = SDL_RegisterEvents(1);
SDL_zero(m_templateEvent);
m_templateEvent.type = SDL_USEREVENT;
m_templateEvent.user.type = m_controlEvent;
}
//Main Event Loop
void EventHandler::EventLoop()
{
struct timeval myTimeval;
struct timezone myTimezone;
gettimeofday(&myTimeval, &myTimezone);
srand(myTimeval.tv_usec);
Star star(-10.0f,0.0f,0.0f,10.0f); //top left
Star star2(0.0f, 10.0f, -10.0f,0.0f); //bot right
Star star3(-10.0f, 0.0f, -10.0f,0.0f); //bot left
Star star4(0.0f, 10.0f, 0.0f,10.0f); //top right
SDL_Event event;
while(!m_bQuit)
{
if(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
m_bQuit=true;
break;
case SDL_KEYDOWN:
{
if(event.key.repeat)
{
break;
}
ShipIntf *pShip = 0;
uint32 command = 0;
switch(event.key.keysym.scancode)
{
case SDL_SCANCODE_B:
//TBD do test things
break;
case SDL_SCANCODE_X:
m_bQuit=true;
break;
case SDL_SCANCODE_Q: //Stop the ship exactly where it is,
case SDL_SCANCODE_R: //Reset to 0 position
case SDL_SCANCODE_A: //left
case SDL_SCANCODE_D: //right
case SDL_SCANCODE_W: //up
case SDL_SCANCODE_S: //shoot
case SDL_SCANCODE_E: //bomb
pShip=m_ships[0];
break;
case SDL_SCANCODE_U: //Stop the ship exactly where it is,
case SDL_SCANCODE_P: //Reset to 0 position
case SDL_SCANCODE_J: //left
case SDL_SCANCODE_L: //right
case SDL_SCANCODE_I: //up
case SDL_SCANCODE_K: //shoot
case SDL_SCANCODE_O: //bomb
if(m_ships.Length() > 1)
{
pShip = m_ships[1];
}
break;
default:
break;
};
if(pShip)
{
command=KeyDown(event.key.keysym.scancode);
pShip->ProcessInput(command);
}
}
break;
case SDL_KEYUP:
{
ShipIntf *pShip = 0;
uint32 command = 0;
switch(event.key.keysym.scancode)
{
case SDL_SCANCODE_Q: //Stop the ship exactly where it is,
case SDL_SCANCODE_R: //Reset to 0 position
case SDL_SCANCODE_A: //left
case SDL_SCANCODE_D: //right
case SDL_SCANCODE_W: //up
case SDL_SCANCODE_S: //shoot
case SDL_SCANCODE_E: //bomb
pShip=m_ships[0];
break;
case SDL_SCANCODE_U: //Stop the ship exactly where it is,
case SDL_SCANCODE_P: //Reset to 0 position
case SDL_SCANCODE_J: //left
case SDL_SCANCODE_L: //right
case SDL_SCANCODE_I: //up
case SDL_SCANCODE_K: //shoot
case SDL_SCANCODE_O: //bomb
if(m_ships.Length() > 1)
{
pShip = m_ships[1];
}
break;
default:
break;
};
if(pShip)
{
command=KeyUp(event.key.keysym.scancode);
pShip->ProcessInput(command);
}
}
break;
case SDL_USEREVENT:
{
//All of the user event types should have a ship pointer
ShipIntf *pShip = reinterpret_cast<ShipIntf*>(event.user.data1);
pShip->ProcessInput(event.user.code);
}
break;
default:
break;
}
}
//Do the physics stepping
m_world.Step(m_timeStep, m_velocityIterations, m_positionIterations);
m_world.ClearForces();
ProcessEffects();
//clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
star.Draw();
star2.Draw();
star3.Draw();
star4.Draw();
//Draw objects that need to be drawn
m_walls.Draw();
//Ships store/draw their own bullets
ProcessShips();
//Display the back buffer
SDL_GL_SwapWindow(m_pWindow);
//Sleep to not eat the machine
SDL_Delay(60);
}
}
//Destructor
EventHandler::~EventHandler()
{
};
/*******************************************/
void EventHandler::ProcessShips() const
{
ShipIntf *pShip = 0;
while ((pShip = m_ships.Next()))
{
pShip->DoCommands();
pShip->Draw();
}
}
/*******************************************/
void EventHandler::ProcessEffects()
{
//Handle all the collisions that occurred
if(!m_collisionSet.empty())
{
CollisionSet::iterator iter = m_collisionSet.begin();
for(;iter != m_collisionSet.end(); ++iter)
{
std::pair<b2Body*, b2Body*> collisionPair =
(*iter);
void *pUserData = collisionPair.first->GetUserData();
if(pUserData)
{
static_cast<DestroyableIntfContainer*>(pUserData)->pDestroyable->DestroyObject();
}
pUserData = collisionPair.second->GetUserData();
if(pUserData)
{
static_cast<DestroyableIntfContainer*>(pUserData)->pDestroyable->DestroyObject();
}
}
m_collisionSet.clear();
}
}
/*******************************************/
//Controls
void EventHandler::ButtonHome(int /* controllerIndex */)
{
m_templateEvent.type = SDL_QUIT;
SDL_PushEvent(&m_templateEvent);
}
void EventHandler::ButtonPushed(int controller, int buttons)
{
m_templateEvent.user.code = buttons;
m_templateEvent.user.data1 = m_ships[controller];
SDL_PushEvent(&m_templateEvent);
}
uint32 EventHandler::KeyDown(SDL_Scancode scankeycode) const
{
uint32 command = 0;
switch(scankeycode)
{
case SDL_SCANCODE_Q: //Stop the ship exactly where it is,
case SDL_SCANCODE_U: //Stop the ship exactly where it is,
command |= SHIP_STOP;
break;
case SDL_SCANCODE_R: //Reset to 0 position
case SDL_SCANCODE_P: //Reset to 0 position
command |= SHIP_RESET;
break;
case SDL_SCANCODE_A: //left
case SDL_SCANCODE_J: //left
command |= SHIP_CCW;
break;
case SDL_SCANCODE_D: //right
case SDL_SCANCODE_L: //right
command |= SHIP_CW;
break;
case SDL_SCANCODE_W: //up
case SDL_SCANCODE_I: //up
command |= SHIP_FORWARD;
break;
case SDL_SCANCODE_S: //shoot
case SDL_SCANCODE_K: //shoot
command |= SHIP_SHOOT;
break;
case SDL_SCANCODE_E: //bomb
case SDL_SCANCODE_O: //bomb
command |= SHIP_BOMB;
break;
default:
//do nothing
break;
}
return command;
}
uint32 EventHandler::KeyUp(SDL_Scancode scankeycode) const
{
uint32 command = 0;
switch(scankeycode)
{
case SDL_SCANCODE_Q: //Stop the ship exactly where it is,
case SDL_SCANCODE_U: //Stop the ship exactly where it is,
command &= ~SHIP_STOP;
break;
case SDL_SCANCODE_R: //Reset to 0 position
case SDL_SCANCODE_P: //Reset to 0 position
command &= ~SHIP_RESET;
break;
case SDL_SCANCODE_A: //left
case SDL_SCANCODE_J: //left
command &= ~SHIP_CCW;
break;
case SDL_SCANCODE_D: //right
case SDL_SCANCODE_L: //right
command &= ~SHIP_CW;
break;
case SDL_SCANCODE_W: //up
case SDL_SCANCODE_I: //up
command &= ~SHIP_FORWARD;
break;
case SDL_SCANCODE_S: //shoot
case SDL_SCANCODE_K: //shoot
command &= ~SHIP_SHOOT;
break;
case SDL_SCANCODE_E: //bomb
case SDL_SCANCODE_O: //bomb
command &= ~SHIP_BOMB;
break;
default:
//do nothing
break;
};
return command;
}
//Put anything that collided into a set
void EventHandler::CollisionHappened(b2Body* bodyA, b2Body* bodyB)
{
//order the pairs that go in so that there can be no duplicates
//with just the order changed.
if(bodyA < bodyB)
{
m_collisionSet.insert(std::pair<b2Body*,b2Body*>(bodyA,bodyB));
}
else
{
m_collisionSet.insert(std::pair<b2Body*,b2Body*>(bodyB,bodyA));
}
}