-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEventHandler.cpp
More file actions
129 lines (112 loc) · 2.51 KB
/
Copy pathEventHandler.cpp
File metadata and controls
129 lines (112 loc) · 2.51 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
#include "GLHeader.h"
#include "EventHandler.h"
#include <SDL2/SDL.h>
#include <iostream>
#include <box2d/box2d.h>
#include "Room.h"
#include "Ship.h"
#include "Shaders.h"
#include "NodeData.h"
EventHandler::EventHandler(SDL_Window *pWindow, b2World& physicsWorld):
m_bQuit(false),
m_pWindow(pWindow),
m_physicsWorld(physicsWorld),
m_timeStep(1.0f / 60.0f),
m_velocityIterations(6),
m_positionIterations(2)
{
}
//Main Event Loop
void EventHandler::EventLoop()
{
NodeData nodeData;
Shaders shaders;
//TBD - This should probably be either passed in or created dynamically
//Rooms are 192x108 grid
Room room(m_physicsWorld,
glm::ortho(-96.0,95.0,-54.0,53.0),
shaders,
nodeData,
"grid.bmp");
Ship myShip(m_physicsWorld,
glm::ortho(-96.0,95.0,-54.0,53.0));
unsigned int command = 0;
bool bShipCommand = false;
SDL_Event event;
while(!m_bQuit)
{
bShipCommand = false;
if(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
m_bQuit=true;
break;
case SDL_KEYDOWN:
{
bShipCommand = true;
switch(event.key.keysym.scancode)
{
case SDL_SCANCODE_X:
m_bQuit=true;
break;
case SDL_SCANCODE_A: //left
command |= SHIP_CCW;
break;
case SDL_SCANCODE_D: //right
command |= SHIP_CW;
break;
case SDL_SCANCODE_W: //up
command |= SHIP_FORWARD;
break;
case SDL_SCANCODE_S: //shoot
// command |= SHIP_SHOOT;
break;
}
break;
}
case SDL_KEYUP:
{
bShipCommand = true;
switch(event.key.keysym.scancode)
{
case SDL_SCANCODE_A: //left
command &= ~SHIP_CCW;
break;
case SDL_SCANCODE_D: //right
command &= ~SHIP_CW;
break;
case SDL_SCANCODE_W: //up
command &= ~SHIP_FORWARD;
break;
case SDL_SCANCODE_S: //shoot
// command &= ~SHIP_SHOOT;
break;
}
break;
}
default:
break;
}
}
if(bShipCommand)
{
myShip.ProcessInput(command);
}
m_physicsWorld.Step(m_timeStep, m_velocityIterations, m_positionIterations);
//clear the screen
glClear( GL_COLOR_BUFFER_BIT );
room.Draw();
myShip.DoCommands();
myShip.Draw();
//Display the back buffer
SDL_GL_SwapWindow(m_pWindow);
//Sleep to not eat the machine
SDL_Delay(60);
}
}
//Destructor
EventHandler::~EventHandler()
{
};