-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.cpp
executable file
·221 lines (194 loc) · 5.68 KB
/
event.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
#include "main.hpp"
#include <algorithm>
#include "tracker.hpp"
typedef std::vector<EventListener*> EventListenerPool;
static EventListenerPool eventListeners, eventListenersCopy;
static bool eventListenersChanged = false;
bool EventListener::keyDown(SDL_keysym sym) { return false; }
bool EventListener::keyUp(SDL_keysym sym) { return false; }
bool EventListener::mouseDown(int x, int y, int button, int mod) { return false; }
bool EventListener::mouseUp(int x, int y, int button) { return false; }
bool EventListener::mouseMotion(int x, int y, int xrel, int yrel) { return false; }
void EventListener::redraw() { }
bool EventListener::isOpaque() { return true; }
bool shouldDestroyEngine = false;
static bool comparePriorities(const EventListener *a, const EventListener *b) {
return a->getEventPriority() > b->getEventPriority();
}
EventListener::EventListener(int priority)
{
areEventsEnabled = false;
eventPriority = priority;
enableEvents();
}
EventListener::~EventListener()
{
disableEvents();
}
void EventListener::enableEvents()
{
if(areEventsEnabled) return;
areEventsEnabled = true;
eventListeners.push_back(this);
eventListenersChanged = true;
}
void EventListener::disableEvents()
{
if(!areEventsEnabled) return;
areEventsEnabled = false;
for(EventListenerPool::iterator ii=eventListeners.begin(); ii!=eventListeners.end(); ii++) {
if(*ii == this) {
eventListeners.erase(ii);
break;
}
}
for(EventListenerPool::iterator ii=eventListenersCopy.begin(); ii!=eventListenersCopy.end(); ii++) {
if(*ii == this) {
eventListenersCopy.erase(ii);
break;
}
}
}
bool EventListener::eventsEnabled() const
{ return areEventsEnabled; }
int EventListener::getEventPriority() const
{ return eventPriority; }
void EventListener::setEventPriority(int priority) {
disableEvents();
eventPriority = priority;
enableEvents();
}
void handleEvent(SDL_Event &event);
void handleEvents();
void drawScreen();
void mainLoop()
{
while(1)
{
updateTime();
handleEvents();
if(network != NULL)
network->timepass();
tracker.timepass();
if (activeFrame)
activeFrame->timepass();
if(engine != NULL)
{
if(engine->controller)
engine->controller->timepass(getDt());
if(engine->view)
engine->view->timepass(getDt());
for(unsigned ii=0; ii < engine->aiPlayers.size(); ii++)
engine->aiPlayers[ii]->timepass(getDt());
}
drawScreen();
if(shouldDestroyEngine) {
if(engine) {
delete engine;
engine = NULL;
}
if(network) {
delete network;
network = NULL;
}
shouldDestroyEngine = false;
}
}
}
void handleEvents()
{
SDL_Event event;
while(SDL_PollEvent(&event))
handleEvent(event);
}
void updateEventListeners()
{
if(eventListenersChanged) {
sort(eventListeners.begin(), eventListeners.end(), comparePriorities);
eventListenersCopy = eventListeners;
eventListenersChanged = false;
}
}
static int kmod = 0;
void handleEvent(SDL_Event &event)
{
updateEventListeners();
EventListenerPool::iterator ii;
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_LSHIFT: kmod |= KMOD_LSHIFT; break;
case SDLK_RSHIFT: kmod |= KMOD_RSHIFT; break;
case SDLK_LCTRL: kmod |= KMOD_LCTRL; break;
case SDLK_RCTRL: kmod |= KMOD_RCTRL; break;
case SDLK_LALT: kmod |= KMOD_LALT; break;
case SDLK_RALT: kmod |= KMOD_RALT; break;
}
for(ii=eventListenersCopy.begin(); ii!=eventListenersCopy.end(); ii++)
if((*ii)->keyDown(event.key.keysym)) break;
break;
case SDL_KEYUP:
switch(event.key.keysym.sym) {
case SDLK_LSHIFT: kmod &= ~KMOD_LSHIFT; break;
case SDLK_RSHIFT: kmod &= ~KMOD_RSHIFT; break;
case SDLK_LCTRL: kmod &= ~KMOD_LCTRL; break;
case SDLK_RCTRL: kmod &= ~KMOD_RCTRL; break;
case SDLK_LALT: kmod &= ~KMOD_LALT; break;
case SDLK_RALT: kmod &= ~KMOD_RALT; break;
}
for(ii=eventListenersCopy.begin(); ii!=eventListenersCopy.end(); ii++)
if((*ii)->keyUp(event.key.keysym)) break;
break;
case SDL_MOUSEMOTION:
cursor->moveCursor(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel);
for(ii=eventListenersCopy.begin(); ii!=eventListenersCopy.end(); ii++) {
if((*ii)->mouseMotion(cursor->getLocation().x, cursor->getLocation().y,
event.motion.xrel, event.motion.yrel))
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
for(ii=eventListenersCopy.begin(); ii!=eventListenersCopy.end(); ii++) {
if((*ii)->mouseDown(cursor->getLocation().x, cursor->getLocation().y,
event.button.button, kmod))
break;
}
break;
case SDL_MOUSEBUTTONUP:
for(ii=eventListenersCopy.begin(); ii!=eventListenersCopy.end(); ii++) {
if((*ii)->mouseUp(cursor->getLocation().x, cursor->getLocation().y,
event.button.button))
break;
}
break;
case SDL_QUIT:
dumpAllocs();
exit(0);
break;
}
}
void drawScreen()
{
EventListenerPool::iterator ii, start, end;
updateEventListeners();
start = eventListenersCopy.begin();
end = eventListenersCopy.end();
glClear(GL_COLOR_BUFFER_BIT);
if(eventListenersCopy.size()>0)
{
for(ii=eventListenersCopy.begin(); ii!=eventListenersCopy.end(); ii++) {
if((*ii)->isOpaque()) {
end = ii;
end++;
break;
}
}
for(ii=end; ii!=start; ) {
ii--;
(*ii)->redraw();
}
}
updateTextCache();
SDL_GL_SwapBuffers();
}