1 | #include "../../common/Compiler.h"
|
---|
2 |
|
---|
3 | #if defined WINDOWS
|
---|
4 | #include <winsock2.h>
|
---|
5 | #include <WS2tcpip.h>
|
---|
6 | #elif defined LINUX
|
---|
7 | #include <sys/types.h>
|
---|
8 | #include <unistd.h>
|
---|
9 | #include <sys/socket.h>
|
---|
10 | #include <netinet/in.h>
|
---|
11 | #include <netdb.h>
|
---|
12 | #include <cstring>
|
---|
13 | #endif
|
---|
14 |
|
---|
15 | #include <cstdio>
|
---|
16 | #include <cstdlib>
|
---|
17 | #include <cmath>
|
---|
18 | #include <sys/types.h>
|
---|
19 | #include <string>
|
---|
20 | #include <iostream>
|
---|
21 | #include <sstream>
|
---|
22 | #include <fstream>
|
---|
23 | #include <map>
|
---|
24 |
|
---|
25 | #include <allegro5/allegro.h>
|
---|
26 | #include <allegro5/allegro_font.h>
|
---|
27 | #include <allegro5/allegro_ttf.h>
|
---|
28 | #include <allegro5/allegro_primitives.h>
|
---|
29 |
|
---|
30 | #include "../../common/Common.h"
|
---|
31 | #include "../../common/MessageContainer.h"
|
---|
32 | #include "../../common/MessageProcessor.h"
|
---|
33 | #include "../../common/WorldMap.h"
|
---|
34 | #include "../../common/Player.h"
|
---|
35 | #include "../../common/Projectile.h"
|
---|
36 |
|
---|
37 | #include "Window.h"
|
---|
38 | #include "Textbox.h"
|
---|
39 | #include "Button.h"
|
---|
40 | #include "RadioButtonList.h"
|
---|
41 | #include "TextLabel.h"
|
---|
42 | #include "chat.h"
|
---|
43 |
|
---|
44 | #ifdef WINDOWS
|
---|
45 | #pragma comment(lib, "ws2_32.lib")
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | using namespace std;
|
---|
49 |
|
---|
50 | void initWinSock();
|
---|
51 | void shutdownWinSock();
|
---|
52 | void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed);
|
---|
53 | void drawMap(WorldMap* gameMap);
|
---|
54 | void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);
|
---|
55 | POSITION screenToMap(POSITION pos);
|
---|
56 | POSITION mapToScreen(POSITION pos);
|
---|
57 | int getRefreshRate(int width, int height);
|
---|
58 |
|
---|
59 | // callbacks
|
---|
60 | void goToLoginScreen();
|
---|
61 | void goToRegisterScreen();
|
---|
62 | void registerAccount();
|
---|
63 | void login();
|
---|
64 | void logout();
|
---|
65 | void quit();
|
---|
66 | void sendChatMessage();
|
---|
67 | void toggleDebugging();
|
---|
68 | void drawMessageStatus(ALLEGRO_FONT* font);
|
---|
69 |
|
---|
70 | void error(const char *);
|
---|
71 |
|
---|
72 | const float FPS = 60;
|
---|
73 | const int SCREEN_W = 1024;
|
---|
74 | const int SCREEN_H = 768;
|
---|
75 |
|
---|
76 | enum STATE {
|
---|
77 | STATE_START,
|
---|
78 | STATE_LOGIN // this means you're already logged in
|
---|
79 | };
|
---|
80 |
|
---|
81 | int state;
|
---|
82 |
|
---|
83 | bool doexit;
|
---|
84 |
|
---|
85 | Window* wndLogin;
|
---|
86 | Window* wndRegister;
|
---|
87 | Window* wndMain;
|
---|
88 | Window* wndMainDebug;
|
---|
89 | Window* wndCurrent;
|
---|
90 |
|
---|
91 | // wndLogin
|
---|
92 | Textbox* txtUsername;
|
---|
93 | Textbox* txtPassword;
|
---|
94 | TextLabel* lblLoginStatus;
|
---|
95 |
|
---|
96 | // wndRegister
|
---|
97 | Textbox* txtUsernameRegister;
|
---|
98 | Textbox* txtPasswordRegister;
|
---|
99 | RadioButtonList* rblClasses;
|
---|
100 | TextLabel* lblRegisterStatus;
|
---|
101 |
|
---|
102 | // wndMain
|
---|
103 | Textbox* txtChat;
|
---|
104 |
|
---|
105 | int sock;
|
---|
106 | struct sockaddr_in server, from;
|
---|
107 | struct hostent *hp;
|
---|
108 | NETWORK_MSG msgTo, msgFrom;
|
---|
109 | string username;
|
---|
110 | chat chatConsole, debugConsole;
|
---|
111 | bool debugging;
|
---|
112 |
|
---|
113 | MessageProcessor msgProcessor;
|
---|
114 |
|
---|
115 | int main(int argc, char **argv)
|
---|
116 | {
|
---|
117 | ALLEGRO_DISPLAY *display = NULL;
|
---|
118 | ALLEGRO_EVENT_QUEUE *event_queue = NULL;
|
---|
119 | ALLEGRO_TIMER *timer = NULL;
|
---|
120 | bool key[4] = { false, false, false, false };
|
---|
121 | bool redraw = true;
|
---|
122 | doexit = false;
|
---|
123 | map<unsigned int, Player> mapPlayers;
|
---|
124 | map<unsigned int, Projectile> mapProjectiles;
|
---|
125 | unsigned int curPlayerId = -1;
|
---|
126 | int scoreBlue, scoreRed;
|
---|
127 | bool fullscreen = false;
|
---|
128 | debugging = false;
|
---|
129 | ofstream outputLog;
|
---|
130 |
|
---|
131 | scoreBlue = 0;
|
---|
132 | scoreRed = 0;
|
---|
133 |
|
---|
134 | state = STATE_START;
|
---|
135 |
|
---|
136 | if(!al_init()) {
|
---|
137 | fprintf(stderr, "failed to initialize allegro!\n");
|
---|
138 | return -1;
|
---|
139 | }
|
---|
140 |
|
---|
141 | outputLog.open("client.log", ios::app);
|
---|
142 | outputLog << "Started client on " << getCurrentDateTimeString() << endl;
|
---|
143 |
|
---|
144 | if (al_init_primitives_addon())
|
---|
145 | cout << "Primitives initialized" << endl;
|
---|
146 | else
|
---|
147 | cout << "Primitives not initialized" << endl;
|
---|
148 |
|
---|
149 | al_init_font_addon();
|
---|
150 | al_init_ttf_addon();
|
---|
151 |
|
---|
152 | #if defined WINDOWS
|
---|
153 | ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
|
---|
154 | #elif defined LINUX
|
---|
155 | ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
|
---|
156 | #endif
|
---|
157 |
|
---|
158 | if (!font) {
|
---|
159 | fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
|
---|
160 | getchar();
|
---|
161 | return -1;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if(!al_install_keyboard()) {
|
---|
165 | fprintf(stderr, "failed to initialize the keyboard!\n");
|
---|
166 | return -1;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if(!al_install_mouse()) {
|
---|
170 | fprintf(stderr, "failed to initialize the mouse!\n");
|
---|
171 | return -1;
|
---|
172 | }
|
---|
173 |
|
---|
174 | timer = al_create_timer(1.0 / FPS);
|
---|
175 | if(!timer) {
|
---|
176 | fprintf(stderr, "failed to create timer!\n");
|
---|
177 | return -1;
|
---|
178 | }
|
---|
179 |
|
---|
180 | int refreshRate = getRefreshRate(SCREEN_W, SCREEN_H);
|
---|
181 | // if the computer doesn't support this resolution, just use windowed mode
|
---|
182 | if (refreshRate > 0 && fullscreen) {
|
---|
183 | al_set_new_display_flags(ALLEGRO_FULLSCREEN);
|
---|
184 | al_set_new_display_refresh_rate(refreshRate);
|
---|
185 | }
|
---|
186 | display = al_create_display(SCREEN_W, SCREEN_H);
|
---|
187 | if(!display) {
|
---|
188 | fprintf(stderr, "failed to create display!\n");
|
---|
189 | al_destroy_timer(timer);
|
---|
190 | return -1;
|
---|
191 | }
|
---|
192 |
|
---|
193 | WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
|
---|
194 |
|
---|
195 | cout << "Loaded map" << endl;
|
---|
196 |
|
---|
197 | debugConsole.addLine("Debug console:");
|
---|
198 | debugConsole.addLine("");
|
---|
199 |
|
---|
200 | wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
201 | wndLogin->addComponent(new Textbox(516, 40, 100, 20, font));
|
---|
202 | wndLogin->addComponent(new Textbox(516, 70, 100, 20, font));
|
---|
203 | wndLogin->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT));
|
---|
204 | wndLogin->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT));
|
---|
205 | wndLogin->addComponent(new TextLabel((SCREEN_W-600)/2, 100, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE));
|
---|
206 | wndLogin->addComponent(new Button(SCREEN_W/2-100, 130, 90, 20, font, "Register", goToRegisterScreen));
|
---|
207 | wndLogin->addComponent(new Button(SCREEN_W/2+10, 130, 90, 20, font, "Login", login));
|
---|
208 | wndLogin->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
|
---|
209 | wndLogin->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
|
---|
210 |
|
---|
211 | txtUsername = (Textbox*)wndLogin->getComponent(0);
|
---|
212 | txtPassword = (Textbox*)wndLogin->getComponent(1);
|
---|
213 | lblLoginStatus = (TextLabel*)wndLogin->getComponent(4);
|
---|
214 |
|
---|
215 | cout << "Created login screen" << endl;
|
---|
216 |
|
---|
217 | wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
218 | wndRegister->addComponent(new Textbox(516, 40, 100, 20, font));
|
---|
219 | wndRegister->addComponent(new Textbox(516, 70, 100, 20, font));
|
---|
220 | wndRegister->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT));
|
---|
221 | wndRegister->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT));
|
---|
222 | wndRegister->addComponent(new RadioButtonList(432, 100, "Pick a class", font));
|
---|
223 | wndRegister->addComponent(new TextLabel((SCREEN_W-600)/2, 190, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE));
|
---|
224 | wndRegister->addComponent(new Button(SCREEN_W/2-100, 220, 90, 20, font, "Back", goToLoginScreen));
|
---|
225 | wndRegister->addComponent(new Button(SCREEN_W/2+10, 220, 90, 20, font, "Submit", registerAccount));
|
---|
226 | wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
|
---|
227 | wndRegister->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
|
---|
228 |
|
---|
229 | txtUsernameRegister = (Textbox*)wndRegister->getComponent(0);
|
---|
230 | txtPasswordRegister = (Textbox*)wndRegister->getComponent(1);
|
---|
231 |
|
---|
232 | rblClasses = (RadioButtonList*)wndRegister->getComponent(4);
|
---|
233 | rblClasses->addRadioButton("Warrior");
|
---|
234 | rblClasses->addRadioButton("Ranger");
|
---|
235 |
|
---|
236 | lblRegisterStatus = (TextLabel*)wndRegister->getComponent(5);
|
---|
237 |
|
---|
238 | cout << "Created register screen" << endl;
|
---|
239 |
|
---|
240 | wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
241 | wndMain->addComponent(new Textbox(95, 40, 300, 20, font));
|
---|
242 | wndMain->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage));
|
---|
243 | wndMain->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
|
---|
244 | wndMain->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
|
---|
245 |
|
---|
246 | txtChat = (Textbox*)wndMain->getComponent(0);
|
---|
247 |
|
---|
248 | wndMainDebug = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
249 | wndMainDebug->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
|
---|
250 | wndMainDebug->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
|
---|
251 |
|
---|
252 | cout << "Created main screen" << endl;
|
---|
253 |
|
---|
254 | goToLoginScreen();
|
---|
255 |
|
---|
256 | event_queue = al_create_event_queue();
|
---|
257 | if(!event_queue) {
|
---|
258 | fprintf(stderr, "failed to create event_queue!\n");
|
---|
259 | al_destroy_display(display);
|
---|
260 | al_destroy_timer(timer);
|
---|
261 | return -1;
|
---|
262 | }
|
---|
263 |
|
---|
264 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
265 |
|
---|
266 | al_register_event_source(event_queue, al_get_display_event_source(display));
|
---|
267 | al_register_event_source(event_queue, al_get_timer_event_source(timer));
|
---|
268 | al_register_event_source(event_queue, al_get_keyboard_event_source());
|
---|
269 | al_register_event_source(event_queue, al_get_mouse_event_source());
|
---|
270 |
|
---|
271 | al_clear_to_color(al_map_rgb(0,0,0));
|
---|
272 |
|
---|
273 | al_flip_display();
|
---|
274 |
|
---|
275 | if (argc != 3) {
|
---|
276 | cout << "Usage: server port" << endl;
|
---|
277 | exit(1);
|
---|
278 | }
|
---|
279 |
|
---|
280 | initWinSock();
|
---|
281 |
|
---|
282 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
283 | if (sock < 0)
|
---|
284 | error("socket");
|
---|
285 |
|
---|
286 | set_nonblock(sock);
|
---|
287 |
|
---|
288 | server.sin_family = AF_INET;
|
---|
289 | hp = gethostbyname(argv[1]);
|
---|
290 | if (hp==0)
|
---|
291 | error("Unknown host");
|
---|
292 |
|
---|
293 | memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
|
---|
294 | server.sin_port = htons(atoi(argv[2]));
|
---|
295 |
|
---|
296 | al_start_timer(timer);
|
---|
297 |
|
---|
298 | while(!doexit)
|
---|
299 | {
|
---|
300 | ALLEGRO_EVENT ev;
|
---|
301 |
|
---|
302 | al_wait_for_event(event_queue, &ev);
|
---|
303 |
|
---|
304 | if(wndCurrent->handleEvent(ev)) {
|
---|
305 | // do nothing
|
---|
306 | }
|
---|
307 | else if(ev.type == ALLEGRO_EVENT_TIMER) {
|
---|
308 | redraw = true; // seems like we should just call a draw function here instead
|
---|
309 | }
|
---|
310 | else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
|
---|
311 | doexit = true;
|
---|
312 | }
|
---|
313 | else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
|
---|
314 | }
|
---|
315 | else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
|
---|
316 | switch(ev.keyboard.keycode) {
|
---|
317 | case ALLEGRO_KEY_ESCAPE:
|
---|
318 | doexit = true;
|
---|
319 | break;
|
---|
320 | case ALLEGRO_KEY_S: // pickup an item next to you
|
---|
321 | if (state == STATE_LOGIN) {
|
---|
322 | msgTo.type = MSG_TYPE_PICKUP_FLAG;
|
---|
323 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
324 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
325 | }
|
---|
326 | break;
|
---|
327 | case ALLEGRO_KEY_D: // drop the current item
|
---|
328 | if (state == STATE_LOGIN) {
|
---|
329 | // find the current player in the player list
|
---|
330 | map<unsigned int, Player>::iterator it;
|
---|
331 | Player* p = NULL;
|
---|
332 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
333 | {
|
---|
334 | if (it->second.id == curPlayerId)
|
---|
335 | p = &it->second;
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (p != NULL) {
|
---|
339 | int flagType = WorldMap::OBJECT_NONE;
|
---|
340 |
|
---|
341 | if (p->hasBlueFlag)
|
---|
342 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
343 | else if (p->hasRedFlag)
|
---|
344 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
345 |
|
---|
346 | if (flagType != WorldMap::OBJECT_NONE) {
|
---|
347 | msgTo.type = MSG_TYPE_DROP_FLAG;
|
---|
348 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
349 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
350 | }
|
---|
351 | }
|
---|
352 | }
|
---|
353 | break;
|
---|
354 | }
|
---|
355 | }
|
---|
356 | else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
357 | if(wndCurrent == wndMain) {
|
---|
358 | if (ev.mouse.button == 1) { // left click
|
---|
359 | msgTo.type = MSG_TYPE_PLAYER_MOVE;
|
---|
360 |
|
---|
361 | POSITION pos;
|
---|
362 | pos.x = ev.mouse.x;
|
---|
363 | pos.y = ev.mouse.y;
|
---|
364 | pos = screenToMap(pos);
|
---|
365 |
|
---|
366 | if (pos.x != -1)
|
---|
367 | {
|
---|
368 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
369 | memcpy(msgTo.buffer+4, &pos.x, 4);
|
---|
370 | memcpy(msgTo.buffer+8, &pos.y, 4);
|
---|
371 |
|
---|
372 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
373 | }
|
---|
374 | else
|
---|
375 | cout << "Invalid point: User did not click on the map" << endl;
|
---|
376 | }else if (ev.mouse.button == 2) { // right click
|
---|
377 | map<unsigned int, Player>::iterator it;
|
---|
378 |
|
---|
379 | cout << "Detected a right-click" << endl;
|
---|
380 |
|
---|
381 | Player* curPlayer;
|
---|
382 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
383 | {
|
---|
384 | if (it->second.id == curPlayerId)
|
---|
385 | curPlayer = &it->second;
|
---|
386 | }
|
---|
387 |
|
---|
388 | Player* target;
|
---|
389 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
390 | {
|
---|
391 | // need to check if the right-click was actually on this player
|
---|
392 | // right now, this code will target all players other than the current one
|
---|
393 | target = &it->second;
|
---|
394 | if (target->id != curPlayerId && target->team != curPlayer->team) {
|
---|
395 | msgTo.type = MSG_TYPE_START_ATTACK;
|
---|
396 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
397 | memcpy(msgTo.buffer+4, &target->id, 4);
|
---|
398 |
|
---|
399 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
400 | }
|
---|
401 | }
|
---|
402 | }
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | if (msgProcessor.receiveMessage(&msgFrom, sock, &from) >= 0)
|
---|
407 | processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
|
---|
408 |
|
---|
409 | if (redraw)
|
---|
410 | {
|
---|
411 | redraw = false;
|
---|
412 |
|
---|
413 | msgProcessor.resendUnackedMessages(sock);
|
---|
414 | //msgProcessor.cleanAckedMessages();
|
---|
415 |
|
---|
416 | if (debugging && wndCurrent == wndMain)
|
---|
417 | wndMainDebug->draw(display);
|
---|
418 | else
|
---|
419 | wndCurrent->draw(display);
|
---|
420 |
|
---|
421 | if(wndCurrent == wndMain) {
|
---|
422 | if (!debugging)
|
---|
423 | chatConsole.draw(font, al_map_rgb(255,255,255));
|
---|
424 |
|
---|
425 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
|
---|
426 |
|
---|
427 | ostringstream ossScoreBlue, ossScoreRed;
|
---|
428 |
|
---|
429 | ossScoreBlue << "Blue: " << scoreBlue << endl;
|
---|
430 | ossScoreRed << "Red: " << scoreRed << endl;
|
---|
431 |
|
---|
432 | al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
|
---|
433 | al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
|
---|
434 |
|
---|
435 | // update players
|
---|
436 | map<unsigned int, Player>::iterator it;
|
---|
437 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
438 | {
|
---|
439 | it->second.updateTarget(mapPlayers);
|
---|
440 | }
|
---|
441 |
|
---|
442 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
443 | {
|
---|
444 | it->second.move(gameMap); // ignore return value
|
---|
445 | }
|
---|
446 |
|
---|
447 | // update projectile positions
|
---|
448 | map<unsigned int, Projectile>::iterator it2;
|
---|
449 | for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
|
---|
450 | {
|
---|
451 | it2->second.move(mapPlayers);
|
---|
452 | }
|
---|
453 |
|
---|
454 | drawMap(gameMap);
|
---|
455 | drawPlayers(mapPlayers, font, curPlayerId);
|
---|
456 |
|
---|
457 | // draw projectiles
|
---|
458 | for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
|
---|
459 | {
|
---|
460 | Projectile proj = it2->second;
|
---|
461 |
|
---|
462 | FLOAT_POSITION target = mapPlayers[proj.target].pos;
|
---|
463 | float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
|
---|
464 |
|
---|
465 | POSITION start, end;
|
---|
466 | start.x = cos(angle)*15+proj.pos.x;
|
---|
467 | start.y = sin(angle)*15+proj.pos.y;
|
---|
468 | end.x = proj.pos.x;
|
---|
469 | end.y = proj.pos.y;
|
---|
470 |
|
---|
471 | start = mapToScreen(start);
|
---|
472 | end = mapToScreen(end);
|
---|
473 |
|
---|
474 | al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | if (debugging) {
|
---|
479 | //debugConsole.draw(font, al_map_rgb(255,255,255));
|
---|
480 | drawMessageStatus(font);
|
---|
481 | }
|
---|
482 |
|
---|
483 | al_flip_display();
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 | #if defined WINDOWS
|
---|
488 | closesocket(sock);
|
---|
489 | #elif defined LINUX
|
---|
490 | close(sock);
|
---|
491 | #endif
|
---|
492 |
|
---|
493 | shutdownWinSock();
|
---|
494 |
|
---|
495 | delete wndLogin;
|
---|
496 | delete wndMain;
|
---|
497 |
|
---|
498 | delete gameMap;
|
---|
499 |
|
---|
500 | al_destroy_event_queue(event_queue);
|
---|
501 | al_destroy_display(display);
|
---|
502 | al_destroy_timer(timer);
|
---|
503 |
|
---|
504 | outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
|
---|
505 | outputLog.close();
|
---|
506 |
|
---|
507 | return 0;
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 |
|
---|
512 | // need to make a function like this that works on windows
|
---|
513 | void error(const char *msg)
|
---|
514 | {
|
---|
515 | perror(msg);
|
---|
516 | shutdownWinSock();
|
---|
517 | exit(1);
|
---|
518 | }
|
---|
519 |
|
---|
520 | void initWinSock()
|
---|
521 | {
|
---|
522 | #if defined WINDOWS
|
---|
523 | WORD wVersionRequested;
|
---|
524 | WSADATA wsaData;
|
---|
525 | int wsaerr;
|
---|
526 |
|
---|
527 | wVersionRequested = MAKEWORD(2, 2);
|
---|
528 | wsaerr = WSAStartup(wVersionRequested, &wsaData);
|
---|
529 |
|
---|
530 | if (wsaerr != 0) {
|
---|
531 | cout << "The Winsock dll not found." << endl;
|
---|
532 | exit(1);
|
---|
533 | }else
|
---|
534 | cout << "The Winsock dll was found." << endl;
|
---|
535 | #endif
|
---|
536 | }
|
---|
537 |
|
---|
538 | void shutdownWinSock()
|
---|
539 | {
|
---|
540 | #if defined WINDOWS
|
---|
541 | WSACleanup();
|
---|
542 | #endif
|
---|
543 | }
|
---|
544 |
|
---|
545 | POSITION screenToMap(POSITION pos)
|
---|
546 | {
|
---|
547 | pos.x = pos.x-300;
|
---|
548 | pos.y = pos.y-100;
|
---|
549 |
|
---|
550 | if (pos.x < 0 || pos.y < 0)
|
---|
551 | {
|
---|
552 | pos.x = -1;
|
---|
553 | pos.y = -1;
|
---|
554 | }
|
---|
555 |
|
---|
556 | return pos;
|
---|
557 | }
|
---|
558 |
|
---|
559 | POSITION mapToScreen(POSITION pos)
|
---|
560 | {
|
---|
561 | pos.x = pos.x+300;
|
---|
562 | pos.y = pos.y+100;
|
---|
563 |
|
---|
564 | return pos;
|
---|
565 | }
|
---|
566 |
|
---|
567 | POSITION mapToScreen(FLOAT_POSITION pos)
|
---|
568 | {
|
---|
569 | POSITION p;
|
---|
570 | p.x = pos.x+300;
|
---|
571 | p.y = pos.y+100;
|
---|
572 |
|
---|
573 | return p;
|
---|
574 | }
|
---|
575 |
|
---|
576 | void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed)
|
---|
577 | {
|
---|
578 | string response = string(msg.buffer);
|
---|
579 |
|
---|
580 | switch(state)
|
---|
581 | {
|
---|
582 | case STATE_START:
|
---|
583 | {
|
---|
584 | cout << "In STATE_START" << endl;
|
---|
585 |
|
---|
586 | switch(msg.type)
|
---|
587 | {
|
---|
588 | case MSG_TYPE_REGISTER:
|
---|
589 | {
|
---|
590 | lblRegisterStatus->setText(response);
|
---|
591 | break;
|
---|
592 | }
|
---|
593 | default:
|
---|
594 | {
|
---|
595 | cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
|
---|
596 | break;
|
---|
597 | }
|
---|
598 | }
|
---|
599 |
|
---|
600 | break;
|
---|
601 | }
|
---|
602 | case STATE_LOGIN:
|
---|
603 | {
|
---|
604 | switch(msg.type)
|
---|
605 | {
|
---|
606 | case MSG_TYPE_LOGIN:
|
---|
607 | {
|
---|
608 | if (response.compare("Player has already logged in.") == 0)
|
---|
609 | {
|
---|
610 | goToLoginScreen();
|
---|
611 | state = STATE_START;
|
---|
612 |
|
---|
613 | lblLoginStatus->setText(response);
|
---|
614 | }
|
---|
615 | else if (response.compare("Incorrect username or password") == 0)
|
---|
616 | {
|
---|
617 | goToLoginScreen();
|
---|
618 | state = STATE_START;
|
---|
619 |
|
---|
620 | lblLoginStatus->setText(response);
|
---|
621 | }
|
---|
622 | else
|
---|
623 | {
|
---|
624 | wndCurrent = wndMain;
|
---|
625 |
|
---|
626 | Player p("", "");
|
---|
627 | p.deserialize(msg.buffer);
|
---|
628 | mapPlayers[p.id] = p;
|
---|
629 | curPlayerId = p.id;
|
---|
630 |
|
---|
631 | cout << "Got a valid login response with the player" << endl;
|
---|
632 | cout << "Player id: " << curPlayerId << endl;
|
---|
633 | cout << "Player health: " << p.health << endl;
|
---|
634 | cout << "player map size: " << mapPlayers.size() << endl;
|
---|
635 | }
|
---|
636 |
|
---|
637 | break;
|
---|
638 | }
|
---|
639 | case MSG_TYPE_LOGOUT:
|
---|
640 | {
|
---|
641 | cout << "Got a logout message" << endl;
|
---|
642 |
|
---|
643 | if (response.compare("You have successfully logged out.") == 0)
|
---|
644 | {
|
---|
645 | cout << "Logged out" << endl;
|
---|
646 | state = STATE_START;
|
---|
647 | goToLoginScreen();
|
---|
648 | }
|
---|
649 |
|
---|
650 | break;
|
---|
651 | }
|
---|
652 | case MSG_TYPE_PLAYER:
|
---|
653 | {
|
---|
654 | cout << "Received MSG_TYPE_PLAYER" << endl;
|
---|
655 |
|
---|
656 | Player p("", "");
|
---|
657 | p.deserialize(msg.buffer);
|
---|
658 | p.timeLastUpdated = getCurrentMillis();
|
---|
659 | p.isChasing = false;
|
---|
660 | if (p.health <= 0)
|
---|
661 | p.isDead = true;
|
---|
662 | else
|
---|
663 | p.isDead = false;
|
---|
664 |
|
---|
665 | mapPlayers[p.id] = p;
|
---|
666 |
|
---|
667 | break;
|
---|
668 | }
|
---|
669 | case MSG_TYPE_PLAYER_MOVE:
|
---|
670 | {
|
---|
671 | unsigned int id;
|
---|
672 | int x, y;
|
---|
673 |
|
---|
674 | memcpy(&id, msg.buffer, 4);
|
---|
675 | memcpy(&x, msg.buffer+4, 4);
|
---|
676 | memcpy(&y, msg.buffer+8, 4);
|
---|
677 |
|
---|
678 | mapPlayers[id].target.x = x;
|
---|
679 | mapPlayers[id].target.y = y;
|
---|
680 |
|
---|
681 | break;
|
---|
682 | }
|
---|
683 | case MSG_TYPE_CHAT:
|
---|
684 | {
|
---|
685 | chatConsole.addLine(response);
|
---|
686 |
|
---|
687 | break;
|
---|
688 | }
|
---|
689 | case MSG_TYPE_OBJECT:
|
---|
690 | {
|
---|
691 | cout << "Received object message in STATE_LOGIN." << endl;
|
---|
692 |
|
---|
693 | WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
|
---|
694 | o.deserialize(msg.buffer);
|
---|
695 | cout << "object id: " << o.id << endl;
|
---|
696 | gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
|
---|
697 |
|
---|
698 | break;
|
---|
699 | }
|
---|
700 | case MSG_TYPE_REMOVE_OBJECT:
|
---|
701 | {
|
---|
702 | cout << "Received REMOVE_OBJECT message!" << endl;
|
---|
703 |
|
---|
704 | int id;
|
---|
705 | memcpy(&id, msg.buffer, 4);
|
---|
706 |
|
---|
707 | cout << "Removing object with id " << id << endl;
|
---|
708 |
|
---|
709 | if (!gameMap->removeObject(id))
|
---|
710 | cout << "Did not remove the object" << endl;
|
---|
711 |
|
---|
712 | break;
|
---|
713 | }
|
---|
714 | case MSG_TYPE_SCORE:
|
---|
715 | {
|
---|
716 | memcpy(&scoreBlue, msg.buffer, 4);
|
---|
717 | memcpy(&scoreRed, msg.buffer+4, 4);
|
---|
718 |
|
---|
719 | break;
|
---|
720 | }
|
---|
721 | case MSG_TYPE_ATTACK:
|
---|
722 | {
|
---|
723 | cout << "Received ATTACK message" << endl;
|
---|
724 |
|
---|
725 | break;
|
---|
726 | }
|
---|
727 | case MSG_TYPE_START_ATTACK:
|
---|
728 | {
|
---|
729 | cout << "Received START_ATTACK message" << endl;
|
---|
730 |
|
---|
731 | unsigned int id, targetID;
|
---|
732 | memcpy(&id, msg.buffer, 4);
|
---|
733 | memcpy(&targetID, msg.buffer+4, 4);
|
---|
734 |
|
---|
735 | cout << "source id: " << id << endl;
|
---|
736 | cout << "target id: " << targetID << endl;
|
---|
737 |
|
---|
738 | Player* source = &mapPlayers[id];
|
---|
739 | source->targetPlayer = targetID;
|
---|
740 | source->isChasing = true;
|
---|
741 |
|
---|
742 | break;
|
---|
743 | }
|
---|
744 | case MSG_TYPE_PROJECTILE:
|
---|
745 | {
|
---|
746 | cout << "Received a PROJECTILE message" << endl;
|
---|
747 |
|
---|
748 | int id, x, y, targetId;
|
---|
749 |
|
---|
750 | memcpy(&id, msg.buffer, 4);
|
---|
751 | memcpy(&x, msg.buffer+4, 4);
|
---|
752 | memcpy(&y, msg.buffer+8, 4);
|
---|
753 | memcpy(&targetId, msg.buffer+12, 4);
|
---|
754 |
|
---|
755 | cout << "id: " << id << endl;
|
---|
756 | cout << "x: " << x << endl;
|
---|
757 | cout << "y: " << y << endl;
|
---|
758 | cout << "Target: " << targetId << endl;
|
---|
759 |
|
---|
760 | Projectile proj(x, y, targetId, 0);
|
---|
761 | proj.setId(id);
|
---|
762 |
|
---|
763 | mapProjectiles[id] = proj;
|
---|
764 |
|
---|
765 | break;
|
---|
766 | }
|
---|
767 | case MSG_TYPE_REMOVE_PROJECTILE:
|
---|
768 | {
|
---|
769 | cout << "Received a REMOVE_PROJECTILE message" << endl;
|
---|
770 |
|
---|
771 | int id;
|
---|
772 |
|
---|
773 | memcpy(&id, msg.buffer, 4);
|
---|
774 |
|
---|
775 | mapProjectiles.erase(id);
|
---|
776 |
|
---|
777 | break;
|
---|
778 | }
|
---|
779 | default:
|
---|
780 | {
|
---|
781 | cout << "(STATE_LOGIN) Received invlaid message of type " << msg.type << endl;
|
---|
782 | break;
|
---|
783 | }
|
---|
784 | }
|
---|
785 |
|
---|
786 | break;
|
---|
787 | }
|
---|
788 | default:
|
---|
789 | {
|
---|
790 | cout << "The state has an invalid value: " << state << endl;
|
---|
791 |
|
---|
792 | break;
|
---|
793 | }
|
---|
794 | }
|
---|
795 | }
|
---|
796 |
|
---|
797 | // this should probably be in the WorldMap class
|
---|
798 | void drawMap(WorldMap* gameMap)
|
---|
799 | {
|
---|
800 | POSITION mapPos;
|
---|
801 | mapPos.x = 0;
|
---|
802 | mapPos.y = 0;
|
---|
803 | mapPos = mapToScreen(mapPos);
|
---|
804 |
|
---|
805 | for (int x=0; x<gameMap->width; x++)
|
---|
806 | {
|
---|
807 | for (int y=0; y<gameMap->height; y++)
|
---|
808 | {
|
---|
809 | WorldMap::TerrainType el = gameMap->getElement(x, y);
|
---|
810 | WorldMap::StructureType structure = gameMap->getStructure(x, y);
|
---|
811 |
|
---|
812 | if (el == WorldMap::TERRAIN_GRASS)
|
---|
813 | al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 255, 0));
|
---|
814 | else if (el == WorldMap::TERRAIN_OCEAN)
|
---|
815 | al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 0, 255));
|
---|
816 | else if (el == WorldMap::TERRAIN_ROCK)
|
---|
817 | al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(100, 100, 0));
|
---|
818 |
|
---|
819 | if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
|
---|
820 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
821 | //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(0, 0, 255));
|
---|
822 | }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
|
---|
823 | al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
|
---|
824 | //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(255, 0, 0));
|
---|
825 | }
|
---|
826 | }
|
---|
827 | }
|
---|
828 |
|
---|
829 | for (int x=0; x<gameMap->width; x++)
|
---|
830 | {
|
---|
831 | for (int y=0; y<gameMap->height; y++)
|
---|
832 | {
|
---|
833 | vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
|
---|
834 |
|
---|
835 | vector<WorldMap::Object>::iterator it;
|
---|
836 | for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
|
---|
837 | switch(it->type) {
|
---|
838 | case WorldMap::OBJECT_BLUE_FLAG:
|
---|
839 | al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(0, 0, 255));
|
---|
840 | break;
|
---|
841 | case WorldMap::OBJECT_RED_FLAG:
|
---|
842 | al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(255, 0, 0));
|
---|
843 | break;
|
---|
844 | }
|
---|
845 | }
|
---|
846 | }
|
---|
847 | }
|
---|
848 | }
|
---|
849 |
|
---|
850 | void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
|
---|
851 | {
|
---|
852 | map<unsigned int, Player>::iterator it;
|
---|
853 |
|
---|
854 | Player* p;
|
---|
855 | POSITION pos;
|
---|
856 | ALLEGRO_COLOR color;
|
---|
857 |
|
---|
858 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
859 | {
|
---|
860 | p = &it->second;
|
---|
861 |
|
---|
862 | if (p->isDead)
|
---|
863 | continue;
|
---|
864 |
|
---|
865 | pos = mapToScreen(p->pos);
|
---|
866 |
|
---|
867 | if (p->id == curPlayerId)
|
---|
868 | al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
|
---|
869 |
|
---|
870 | if (p->team == 0)
|
---|
871 | color = al_map_rgb(0, 0, 255);
|
---|
872 | else if (p->team == 1)
|
---|
873 | color = al_map_rgb(255, 0, 0);
|
---|
874 |
|
---|
875 | al_draw_filled_circle(pos.x, pos.y, 12, color);
|
---|
876 |
|
---|
877 | // draw player class
|
---|
878 | int fontHeight = al_get_font_line_height(font);
|
---|
879 |
|
---|
880 | string strClass;
|
---|
881 | switch (p->playerClass) {
|
---|
882 | case Player::CLASS_WARRIOR:
|
---|
883 | strClass = "W";
|
---|
884 | break;
|
---|
885 | case Player::CLASS_RANGER:
|
---|
886 | strClass = "R";
|
---|
887 | break;
|
---|
888 | case Player::CLASS_NONE:
|
---|
889 | strClass = "";
|
---|
890 | break;
|
---|
891 | default:
|
---|
892 | strClass = "";
|
---|
893 | break;
|
---|
894 | }
|
---|
895 | al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
|
---|
896 |
|
---|
897 | // draw player health
|
---|
898 | al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
|
---|
899 | if (it->second.maxHealth != 0)
|
---|
900 | al_draw_filled_rectangle(pos.x-11, pos.y-23, pos.x-11+(22*it->second.health)/it->second.maxHealth, pos.y-17, al_map_rgb(255, 0, 0));
|
---|
901 |
|
---|
902 | if (p->hasBlueFlag)
|
---|
903 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
|
---|
904 | else if (p->hasRedFlag)
|
---|
905 | al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
|
---|
906 | }
|
---|
907 | }
|
---|
908 |
|
---|
909 | void goToRegisterScreen()
|
---|
910 | {
|
---|
911 | txtUsernameRegister->clear();
|
---|
912 | txtPasswordRegister->clear();
|
---|
913 | lblRegisterStatus->setText("");
|
---|
914 | rblClasses->setSelectedButton(-1);
|
---|
915 |
|
---|
916 | wndCurrent = wndRegister;
|
---|
917 | }
|
---|
918 |
|
---|
919 | void goToLoginScreen()
|
---|
920 | {
|
---|
921 | txtUsername->clear();
|
---|
922 | txtPassword->clear();
|
---|
923 | lblLoginStatus->setText("");
|
---|
924 |
|
---|
925 | wndCurrent = wndLogin;
|
---|
926 | }
|
---|
927 |
|
---|
928 | // maybe need a goToGameScreen function as well and add state changes to these functions as well
|
---|
929 |
|
---|
930 | void registerAccount()
|
---|
931 | {
|
---|
932 | string username = txtUsernameRegister->getStr();
|
---|
933 | string password = txtPasswordRegister->getStr();
|
---|
934 |
|
---|
935 | txtUsernameRegister->clear();
|
---|
936 | txtPasswordRegister->clear();
|
---|
937 | // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
|
---|
938 |
|
---|
939 | Player::PlayerClass playerClass;
|
---|
940 |
|
---|
941 | switch (rblClasses->getSelectedButton()) {
|
---|
942 | case 0:
|
---|
943 | playerClass = Player::CLASS_WARRIOR;
|
---|
944 | break;
|
---|
945 | case 1:
|
---|
946 | playerClass = Player::CLASS_RANGER;
|
---|
947 | break;
|
---|
948 | default:
|
---|
949 | cout << "Invalid class selection" << endl;
|
---|
950 | playerClass = Player::CLASS_NONE;
|
---|
951 | break;
|
---|
952 | }
|
---|
953 |
|
---|
954 | msgTo.type = MSG_TYPE_REGISTER;
|
---|
955 |
|
---|
956 | strcpy(msgTo.buffer, username.c_str());
|
---|
957 | strcpy(msgTo.buffer+username.size()+1, password.c_str());
|
---|
958 | memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
|
---|
959 |
|
---|
960 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
961 | }
|
---|
962 |
|
---|
963 | void login()
|
---|
964 | {
|
---|
965 | string strUsername = txtUsername->getStr();
|
---|
966 | string strPassword = txtPassword->getStr();
|
---|
967 | username = strUsername;
|
---|
968 |
|
---|
969 | txtUsername->clear();
|
---|
970 | txtPassword->clear();
|
---|
971 |
|
---|
972 | msgTo.type = MSG_TYPE_LOGIN;
|
---|
973 |
|
---|
974 | strcpy(msgTo.buffer, strUsername.c_str());
|
---|
975 | strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
|
---|
976 |
|
---|
977 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
978 |
|
---|
979 | state = STATE_LOGIN;
|
---|
980 | }
|
---|
981 |
|
---|
982 | void logout()
|
---|
983 | {
|
---|
984 | txtChat->clear();
|
---|
985 | chatConsole.clear();
|
---|
986 |
|
---|
987 | msgTo.type = MSG_TYPE_LOGOUT;
|
---|
988 |
|
---|
989 | strcpy(msgTo.buffer, username.c_str());
|
---|
990 |
|
---|
991 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
992 | }
|
---|
993 |
|
---|
994 | void quit()
|
---|
995 | {
|
---|
996 | doexit = true;
|
---|
997 | }
|
---|
998 |
|
---|
999 | void sendChatMessage()
|
---|
1000 | {
|
---|
1001 | string msg = txtChat->getStr();
|
---|
1002 |
|
---|
1003 | txtChat->clear();
|
---|
1004 |
|
---|
1005 | msgTo.type = MSG_TYPE_CHAT;
|
---|
1006 |
|
---|
1007 | strcpy(msgTo.buffer, msg.c_str());
|
---|
1008 |
|
---|
1009 | msgProcessor.sendMessage(&msgTo, sock, &server);
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | void toggleDebugging()
|
---|
1013 | {
|
---|
1014 | debugging = !debugging;
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | int getRefreshRate(int width, int height)
|
---|
1018 | {
|
---|
1019 | int numRefreshRates = al_get_num_display_modes();
|
---|
1020 | ALLEGRO_DISPLAY_MODE displayMode;
|
---|
1021 |
|
---|
1022 | for(int i=0; i<numRefreshRates; i++) {
|
---|
1023 | al_get_display_mode(i, &displayMode);
|
---|
1024 |
|
---|
1025 | if (displayMode.width == width && displayMode.height == height)
|
---|
1026 | return displayMode.refresh_rate;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | return 0;
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | void drawMessageStatus(ALLEGRO_FONT* font)
|
---|
1033 | {
|
---|
1034 | int clientMsgOffset = 0;
|
---|
1035 | int serverMsgOffset = 650;
|
---|
1036 |
|
---|
1037 | al_draw_text(font, al_map_rgb(0, 255, 255), 5, 43, ALLEGRO_ALIGN_LEFT, "ID");
|
---|
1038 | al_draw_text(font, al_map_rgb(0, 255, 255), 25, 43, ALLEGRO_ALIGN_LEFT, "Type");
|
---|
1039 | al_draw_text(font, al_map_rgb(0, 255, 255), 245, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
|
---|
1040 |
|
---|
1041 | al_draw_text(font, al_map_rgb(0, 255, 255), 5+serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
|
---|
1042 | al_draw_text(font, al_map_rgb(0, 255, 255), 25+serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
|
---|
1043 |
|
---|
1044 | map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
|
---|
1045 | int id, type;
|
---|
1046 | bool acked;
|
---|
1047 | ostringstream ossId, ossAcked;
|
---|
1048 |
|
---|
1049 | map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
|
---|
1050 |
|
---|
1051 | int msgCount = 0;
|
---|
1052 | for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
|
---|
1053 | map<unsigned long, MessageContainer> playerMessage = it->second;
|
---|
1054 | map<unsigned long, MessageContainer>::iterator it2;
|
---|
1055 | for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
|
---|
1056 |
|
---|
1057 | id = it->first;
|
---|
1058 | ossId.str("");;
|
---|
1059 | ossId << id;
|
---|
1060 |
|
---|
1061 | type = it2->second.getMessage()->type;
|
---|
1062 | string typeStr = MessageContainer::getMsgTypeString(type);
|
---|
1063 |
|
---|
1064 | acked = it2->second.getAcked();
|
---|
1065 | ossAcked.str("");;
|
---|
1066 | ossAcked << boolalpha << acked;
|
---|
1067 |
|
---|
1068 | al_draw_text(font, al_map_rgb(0, 255, 0), 5, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
|
---|
1069 | al_draw_text(font, al_map_rgb(0, 255, 0), 25, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
|
---|
1070 | al_draw_text(font, al_map_rgb(0, 255, 0), 245, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
|
---|
1071 |
|
---|
1072 | msgCount++;
|
---|
1073 | }
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | map<unsigned int, MessageContainer>& ackedMessages = msgProcessor.getAckedMessages();
|
---|
1077 | map<unsigned int, MessageContainer>::iterator it3;
|
---|
1078 |
|
---|
1079 | msgCount = 0;
|
---|
1080 | for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
|
---|
1081 | ossId.str("");;
|
---|
1082 | ossId << it3->first;
|
---|
1083 |
|
---|
1084 | string typeStr = MessageContainer::getMsgTypeString(it3->second.getMessage()->type);
|
---|
1085 |
|
---|
1086 | al_draw_text(font, al_map_rgb(255, 0, 0), 5+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
|
---|
1087 | al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
|
---|
1088 |
|
---|
1089 | msgCount++;
|
---|
1090 | }
|
---|
1091 | }
|
---|