source: network-game/client/Client/main.cpp@ f3fb980

Last change on this file since f3fb980 was 9c18cb7, checked in by dportnoy <dmp1488@…>, 11 years ago

Removed error function definition from main.cpp

  • Property mode set to 100644
File size: 44.2 KB
Line 
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#include "../../common/Game.h"
37#include "../../common/GameSummary.h"
38
39#include "Window.h"
40#include "TextLabel.h"
41#include "Button.h"
42#include "Textbox.h"
43#include "RadioButtonList.h"
44
45#include "GameRender.h"
46
47#include "chat.h"
48
49#ifdef WINDOWS
50 #pragma comment(lib, "ws2_32.lib")
51#endif
52
53using namespace std;
54
55void initWinSock();
56void shutdownWinSock();
57void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player*>& mapPlayers,
58 map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed);
59int getRefreshRate(int width, int height);
60void drawMessageStatus(ALLEGRO_FONT* font);
61
62// Callback declarations
63void goToLoginScreen();
64void goToRegisterScreen();
65void registerAccount();
66void login();
67void logout();
68void quit();
69void sendChatMessage();
70void toggleDebugging();
71void joinGame();
72void createGame();
73void leaveGame();
74void closeGameSummary();
75
76const float FPS = 60;
77const int SCREEN_W = 1024;
78const int SCREEN_H = 768;
79
80enum STATE {
81 STATE_START,
82 STATE_LOBBY,
83 STATE_GAME,
84 STATE_NEW_GAME
85};
86
87int state;
88
89bool doexit;
90
91Window* wndLogin;
92Window* wndRegister;
93Window* wndLobby;
94Window* wndGame;
95Window* wndNewGame;
96Window* wndGameDebug;
97Window* wndGameSummary;
98Window* wndCurrent;
99
100// wndLogin
101Textbox* txtUsername;
102Textbox* txtPassword;
103TextLabel* lblLoginStatus;
104
105// wndRegister
106Textbox* txtUsernameRegister;
107Textbox* txtPasswordRegister;
108RadioButtonList* rblClasses;
109TextLabel* lblRegisterStatus;
110
111// wndLobby
112Textbox* txtJoinGame;
113Textbox* txtCreateGame;
114
115// wndGame
116Textbox* txtChat;
117
118int sock;
119struct sockaddr_in server, from;
120struct hostent *hp;
121NETWORK_MSG msgTo, msgFrom;
122string username;
123chat chatConsole, debugConsole;
124bool debugging;
125map<string, int> mapGames;
126Game* game;
127GameSummary* gameSummary;
128
129MessageProcessor msgProcessor;
130
131int main(int argc, char **argv)
132{
133 ALLEGRO_DISPLAY *display = NULL;
134 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
135 ALLEGRO_TIMER *timer = NULL;
136 bool key[4] = { false, false, false, false };
137 map<unsigned int, Player*> mapPlayers;
138 map<unsigned int, Projectile> mapProjectiles;
139 unsigned int curPlayerId = -1;
140 ofstream outputLog;
141
142 int scoreBlue, scoreRed;
143
144 doexit = false;
145 debugging = false;
146 bool redraw = true;
147 bool fullscreen = false;
148 game = NULL;
149 gameSummary = NULL;
150
151 scoreBlue = 0;
152 scoreRed = 0;
153
154 state = STATE_START;
155
156 if(!al_init()) {
157 fprintf(stderr, "failed to initialize allegro!\n");
158 return -1;
159 }
160
161 outputLog.open("client.log", ios::app);
162 outputLog << "Started client on " << getCurrentDateTimeString() << endl;
163
164 if (al_init_primitives_addon())
165 cout << "Primitives initialized" << endl;
166 else
167 cout << "Primitives not initialized" << endl;
168
169 al_init_font_addon();
170 al_init_ttf_addon();
171
172 #if defined WINDOWS
173 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
174 #elif defined LINUX
175 ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
176 #endif
177
178 if (!font) {
179 fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
180 getchar();
181 return -1;
182 }
183
184 if(!al_install_keyboard()) {
185 fprintf(stderr, "failed to initialize the keyboard!\n");
186 return -1;
187 }
188
189 if(!al_install_mouse()) {
190 fprintf(stderr, "failed to initialize the mouse!\n");
191 return -1;
192 }
193
194 timer = al_create_timer(1.0 / FPS);
195 if(!timer) {
196 fprintf(stderr, "failed to create timer!\n");
197 return -1;
198 }
199
200 int refreshRate = getRefreshRate(SCREEN_W, SCREEN_H);
201 // if the computer doesn't support this resolution, just use windowed mode
202 if (refreshRate > 0 && fullscreen) {
203 al_set_new_display_flags(ALLEGRO_FULLSCREEN);
204 al_set_new_display_refresh_rate(refreshRate);
205 }
206 display = al_create_display(SCREEN_W, SCREEN_H);
207 if(!display) {
208 fprintf(stderr, "failed to create display!\n");
209 al_destroy_timer(timer);
210 return -1;
211 }
212
213 WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
214
215 cout << "Loaded map" << endl;
216
217 debugConsole.addLine("Debug console:");
218 debugConsole.addLine("");
219
220 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
221 wndLogin->addComponent(new Textbox(516, 40, 100, 20, font));
222 wndLogin->addComponent(new Textbox(516, 70, 100, 20, font));
223 wndLogin->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT));
224 wndLogin->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT));
225 wndLogin->addComponent(new TextLabel((SCREEN_W-600)/2, 100, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE));
226 wndLogin->addComponent(new Button(SCREEN_W/2-100, 130, 90, 20, font, "Register", goToRegisterScreen));
227 wndLogin->addComponent(new Button(SCREEN_W/2+10, 130, 90, 20, font, "Login", login));
228 wndLogin->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
229 wndLogin->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
230
231 txtUsername = (Textbox*)wndLogin->getComponent(0);
232 txtPassword = (Textbox*)wndLogin->getComponent(1);
233 lblLoginStatus = (TextLabel*)wndLogin->getComponent(4);
234
235 cout << "Created login screen" << endl;
236
237 wndRegister = new Window(0, 0, SCREEN_W, SCREEN_H);
238 wndRegister->addComponent(new Textbox(516, 40, 100, 20, font));
239 wndRegister->addComponent(new Textbox(516, 70, 100, 20, font));
240 wndRegister->addComponent(new TextLabel(410, 40, 100, 20, font, "Username:", ALLEGRO_ALIGN_RIGHT));
241 wndRegister->addComponent(new TextLabel(410, 70, 100, 20, font, "Password:", ALLEGRO_ALIGN_RIGHT));
242 wndRegister->addComponent(new RadioButtonList(432, 100, "Pick a class", font));
243 wndRegister->addComponent(new TextLabel((SCREEN_W-600)/2, 190, 600, 20, font, "", ALLEGRO_ALIGN_CENTRE));
244 wndRegister->addComponent(new Button(SCREEN_W/2-100, 220, 90, 20, font, "Back", goToLoginScreen));
245 wndRegister->addComponent(new Button(SCREEN_W/2+10, 220, 90, 20, font, "Submit", registerAccount));
246 wndRegister->addComponent(new Button(920, 10, 80, 20, font, "Quit", quit));
247 wndRegister->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
248
249 txtUsernameRegister = (Textbox*)wndRegister->getComponent(0);
250 txtPasswordRegister = (Textbox*)wndRegister->getComponent(1);
251
252 rblClasses = (RadioButtonList*)wndRegister->getComponent(4);
253 rblClasses->addRadioButton("Warrior");
254 rblClasses->addRadioButton("Ranger");
255
256 lblRegisterStatus = (TextLabel*)wndRegister->getComponent(5);
257
258 cout << "Created register screen" << endl;
259
260 wndLobby = new Window(0, 0, SCREEN_W, SCREEN_H);
261 wndLobby->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
262 wndLobby->addComponent(new TextLabel(SCREEN_W*1/2+15-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT));
263 wndLobby->addComponent(new Textbox(SCREEN_W*1/2+15+4, 40, 100, 20, font));
264 wndLobby->addComponent(new Button(SCREEN_W*1/2+15-100, 80, 200, 20, font, "Join Existing Game", joinGame));
265 wndLobby->addComponent(new TextLabel(SCREEN_W*3/4-112, 40, 110, 20, font, "Game Name:", ALLEGRO_ALIGN_RIGHT));
266 wndLobby->addComponent(new Textbox(SCREEN_W*3/4+4, 40, 100, 20, font));
267 wndLobby->addComponent(new Button(SCREEN_W*3/4-100, 80, 200, 20, font, "Create New Game", createGame));
268 wndLobby->addComponent(new Textbox(95, 40, 300, 20, font));
269 wndLobby->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage));
270
271 txtJoinGame = (Textbox*)wndLobby->getComponent(2);
272 txtCreateGame = (Textbox*)wndLobby->getComponent(5);
273 txtChat = (Textbox*)wndLobby->getComponent(7);
274
275 cout << "Created lobby screen" << endl;
276
277 // this is the old game screen
278 wndGame = new Window(0, 0, SCREEN_W, SCREEN_H);
279 wndGame->addComponent(new Textbox(95, 40, 300, 20, font));
280 wndGame->addComponent(new Button(95, 70, 60, 20, font, "Send", sendChatMessage));
281 wndGame->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
282 wndGame->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
283
284 wndGameDebug = new Window(0, 0, SCREEN_W, SCREEN_H);
285 wndGameDebug->addComponent(new Button(20, 10, 160, 20, font, "Toggle Debugging", toggleDebugging));
286 wndGameDebug->addComponent(new Button(920, 10, 80, 20, font, "Logout", logout));
287
288 cout << "Created game screen" << endl;
289
290 // this is the new game screen, without a debug console
291 wndNewGame = new Window(0, 0, SCREEN_W, SCREEN_H);
292 wndNewGame->addComponent(new Button(880, 10, 120, 20, font, "Leave Game", leaveGame));
293
294 cout << "Created new game screen" << endl;
295
296 wndGameSummary = new Window(0, 0, SCREEN_W, SCREEN_H);
297 wndGameSummary->addComponent(new Button(840, 730, 160, 20, font, "Back to Lobby", closeGameSummary));
298
299 cout << "Created game summary screen" << endl;
300
301 goToLoginScreen();
302
303 event_queue = al_create_event_queue();
304 if(!event_queue) {
305 fprintf(stderr, "failed to create event_queue!\n");
306 al_destroy_display(display);
307 al_destroy_timer(timer);
308 return -1;
309 }
310
311 al_set_target_bitmap(al_get_backbuffer(display));
312
313 al_register_event_source(event_queue, al_get_display_event_source(display));
314 al_register_event_source(event_queue, al_get_timer_event_source(timer));
315 al_register_event_source(event_queue, al_get_keyboard_event_source());
316 al_register_event_source(event_queue, al_get_mouse_event_source());
317
318 al_clear_to_color(al_map_rgb(0,0,0));
319
320 al_flip_display();
321
322 if (argc != 3) {
323 cout << "Usage: server port" << endl;
324 exit(1);
325 }
326
327 initWinSock();
328
329 sock = socket(AF_INET, SOCK_DGRAM, 0);
330 if (sock < 0)
331 error("socket");
332
333 set_nonblock(sock);
334
335 server.sin_family = AF_INET;
336 hp = gethostbyname(argv[1]);
337 if (hp == 0)
338 error("Unknown host");
339
340 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
341 server.sin_port = htons(atoi(argv[2]));
342
343 msgProcessor = MessageProcessor(sock, &outputLog);
344
345 al_start_timer(timer);
346
347 while(!doexit)
348 {
349 ALLEGRO_EVENT ev;
350
351 al_wait_for_event(event_queue, &ev);
352
353 if(wndCurrent->handleEvent(ev)) {
354 // do nothing
355 }
356 else if(ev.type == ALLEGRO_EVENT_TIMER) {
357 redraw = true; // seems like we should just call a draw function here instead
358 }
359 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
360 doexit = true;
361 }
362 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
363 }
364 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
365 switch(ev.keyboard.keycode) {
366 case ALLEGRO_KEY_ESCAPE:
367 doexit = true;
368 break;
369 case ALLEGRO_KEY_S: // pickup an item next to you
370 if (state == STATE_GAME || state == STATE_NEW_GAME) {
371 msgTo.type = MSG_TYPE_PICKUP_FLAG;
372 memcpy(msgTo.buffer, &curPlayerId, 4);
373 msgProcessor.sendMessage(&msgTo, &server);
374 }
375 break;
376 case ALLEGRO_KEY_D: // drop the current item
377 if (state == STATE_GAME || state == STATE_NEW_GAME) {
378 Player* p = NULL;
379 try {
380 p = mapPlayers.at(curPlayerId);
381 } catch (const out_of_range& ex) {}
382
383 if (p != NULL) {
384 int flagType = WorldMap::OBJECT_NONE;
385
386 if (p->hasBlueFlag)
387 flagType = WorldMap::OBJECT_BLUE_FLAG;
388 else if (p->hasRedFlag)
389 flagType = WorldMap::OBJECT_RED_FLAG;
390
391 if (flagType != WorldMap::OBJECT_NONE) {
392 msgTo.type = MSG_TYPE_DROP_FLAG;
393 memcpy(msgTo.buffer, &curPlayerId, 4);
394 msgProcessor.sendMessage(&msgTo, &server);
395 }
396 }
397 }
398 break;
399 }
400 }
401 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
402 if(wndCurrent == wndGame || wndCurrent == wndNewGame) {
403 if (ev.mouse.button == 1) { // left click
404 msgTo.type = MSG_TYPE_PLAYER_MOVE;
405
406 POSITION pos;
407 pos.x = ev.mouse.x;
408 pos.y = ev.mouse.y;
409 pos = screenToMap(pos);
410
411 if (pos.x != -1)
412 {
413 memcpy(msgTo.buffer, &curPlayerId, 4);
414 memcpy(msgTo.buffer+4, &pos.x, 4);
415 memcpy(msgTo.buffer+8, &pos.y, 4);
416
417 msgProcessor.sendMessage(&msgTo, &server);
418 }
419 else
420 cout << "Invalid point: User did not click on the map" << endl;
421 }else if (ev.mouse.button == 2) { // right click
422 cout << "Detected a right-click" << endl;
423 map<unsigned int, Player*>::iterator it;
424
425 Player* curPlayer;
426 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
427 {
428 if (it->second->id == curPlayerId)
429 curPlayer = it->second;
430 }
431
432 cout << "Got current player" << endl;
433 cout << "current game: " << game << endl;
434
435 map<unsigned int, Player*> playersInGame = game->getPlayers();
436 Player* target;
437
438 for(it = playersInGame.begin(); it != playersInGame.end(); it++)
439 {
440 // need to check if the right-click was actually on this player
441 // right now, this code will target all players other than the current one
442 target = it->second;
443 cout << "set target" << endl;
444 if (target->id != curPlayerId && target->team != curPlayer->team)
445 {
446 cout << "Found valid target" << endl;
447
448 msgTo.type = MSG_TYPE_START_ATTACK;
449 memcpy(msgTo.buffer, &curPlayerId, 4);
450 memcpy(msgTo.buffer+4, &target->id, 4);
451
452 msgProcessor.sendMessage(&msgTo, &server);
453 }
454 }
455 }
456 }
457 }
458
459 if (msgProcessor.receiveMessage(&msgFrom, &from) >= 0)
460 processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, mapProjectiles, curPlayerId, scoreBlue, scoreRed);
461
462 if (redraw)
463 {
464 redraw = false;
465
466 msgProcessor.resendUnackedMessages();
467
468 if (debugging && wndCurrent == wndGame)
469 wndGameDebug->draw(display);
470 else
471 wndCurrent->draw(display);
472
473 if (wndCurrent == wndLobby) {
474 chatConsole.draw(font, al_map_rgb(255,255,255));
475
476 map<string, int>::iterator it;
477 int i=0;
478 ostringstream ossGame;
479 for (it = mapGames.begin(); it != mapGames.end(); it++) {
480 ossGame << it->first << " (" << it->second << " players)" << endl;
481 al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/2-100, 120+i*15, ALLEGRO_ALIGN_LEFT, ossGame.str().c_str());
482 ossGame.clear();
483 ossGame.str("");
484 i++;
485 }
486 }
487 else if (wndCurrent == wndNewGame)
488 {
489 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 4, ALLEGRO_ALIGN_LEFT, "Players");
490
491 map<unsigned int, Player*>& gamePlayers = game->getPlayers();
492 map<unsigned int, Player*>::iterator it;
493
494 int playerCount = 0;
495 for (it = gamePlayers.begin(); it != gamePlayers.end(); it++)
496 {
497 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 19+(playerCount+1)*15, ALLEGRO_ALIGN_LEFT, it->second->name.c_str());
498 playerCount++;
499 }
500
501 ostringstream ossScoreBlue, ossScoreRed;
502
503 ossScoreBlue << "Blue: " << game->getBlueScore() << endl;
504 ossScoreRed << "Red: " << game->getRedScore() << endl;
505
506 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
507 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
508
509 // update players
510 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
511 {
512 it->second->updateTarget(game->getPlayers());
513 }
514
515 for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++)
516 {
517 it->second->move(game->getMap()); // ignore return value
518 }
519
520 // update projectile positions
521 map<unsigned int, Projectile>::iterator it2;
522 for (it2 = game->getProjectiles().begin(); it2 != game->getProjectiles().end(); it2++)
523 {
524 it2->second.move(game->getPlayers());
525 }
526
527 GameRender::drawMap(game->getMap());
528 GameRender::drawPlayers(game->getPlayers(), font, curPlayerId);
529
530 // draw projectiles
531 for (it2 = game->getProjectiles().begin(); it2 != game->getProjectiles().end(); it2++)
532 {
533 Projectile proj = it2->second;
534
535 FLOAT_POSITION target = game->getPlayers()[proj.target]->pos;
536 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
537
538 POSITION start, end;
539 start.x = cos(angle)*15+proj.pos.x;
540 start.y = sin(angle)*15+proj.pos.y;
541 end.x = proj.pos.x;
542 end.y = proj.pos.y;
543
544 start = mapToScreen(start);
545 end = mapToScreen(end);
546
547 al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
548 }
549 }
550 else if (wndCurrent == wndGame)
551 {
552 if (!debugging)
553 chatConsole.draw(font, al_map_rgb(255,255,255));
554
555 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
556
557 ostringstream ossScoreBlue, ossScoreRed;
558
559 ossScoreBlue << "Blue: " << scoreBlue << endl;
560 ossScoreRed << "Red: " << scoreRed << endl;
561
562 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
563 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
564
565 // update players
566 map<unsigned int, Player*>::iterator it;
567 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
568 {
569 it->second->updateTarget(mapPlayers);
570 }
571
572 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
573 {
574 it->second->move(gameMap); // ignore return value
575 }
576
577 // update projectile positions
578 map<unsigned int, Projectile>::iterator it2;
579 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
580 {
581 it2->second.move(mapPlayers);
582 }
583
584 GameRender::drawMap(gameMap);
585 GameRender::drawPlayers(mapPlayers, font, curPlayerId);
586
587 // draw projectiles
588 for (it2 = mapProjectiles.begin(); it2 != mapProjectiles.end(); it2++)
589 {
590 Projectile proj = it2->second;
591
592 FLOAT_POSITION target = mapPlayers[proj.target]->pos;
593 float angle = atan2(target.y-proj.pos.toFloat().y, target.x-proj.pos.toFloat().x);
594
595 POSITION start, end;
596 start.x = cos(angle)*15+proj.pos.x;
597 start.y = sin(angle)*15+proj.pos.y;
598 end.x = proj.pos.x;
599 end.y = proj.pos.y;
600
601 start = mapToScreen(start);
602 end = mapToScreen(end);
603
604 al_draw_line(start.x, start.y, end.x, end.y, al_map_rgb(0, 0, 0), 4);
605 }
606 }else if (wndCurrent == wndGameSummary) {
607 cout << "Drawing game summary" << endl;
608
609 cout << "blue score from obj: " << gameSummary->getBlueScore() << endl;
610 cout << "red score from obj: " << gameSummary->getRedScore() << endl;
611
612 ostringstream ossBlueScore, ossRedScore;
613
614 cout << "Declared scores" << endl;
615
616 ossBlueScore << "Blue Score: " << gameSummary->getBlueScore();
617 ossRedScore << "Red Score: " << gameSummary->getRedScore();
618
619 cout << "set scores" << endl;
620
621 string strWinner;
622
623 if (gameSummary->getWinner() == 0)
624 strWinner = "Blue Team Wins";
625 else if (gameSummary->getWinner() == 1)
626 strWinner = "Red Team Wins";
627 else
628 strWinner = "winner set to wrong value";
629
630 cout << "Calling the drawing routines" << endl;
631
632 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 40, ALLEGRO_ALIGN_CENTRE, gameSummary->getName().c_str());
633 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossBlueScore.str().c_str());
634 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossRedScore.str().c_str());
635 al_draw_text(font, al_map_rgb(0, 255, 0), 512, 120, ALLEGRO_ALIGN_CENTRE, strWinner.c_str());
636
637 cout << "Done drawing game summary" << endl;
638 }
639
640 if (debugging) {
641 //debugConsole.draw(font, al_map_rgb(255,255,255));
642 drawMessageStatus(font);
643 }
644
645 al_flip_display();
646 }
647 }
648
649 #if defined WINDOWS
650 closesocket(sock);
651 #elif defined LINUX
652 close(sock);
653 #endif
654
655 shutdownWinSock();
656
657 delete wndLogin;
658 delete wndRegister;
659 delete wndLobby;
660 delete wndGame;
661 delete wndGameDebug;
662
663 delete gameMap;
664
665 if (game != NULL)
666 delete game;
667
668 if (gameSummary != NULL)
669 delete gameSummary;
670
671 map<unsigned int, Player*>::iterator it;
672
673 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
674 delete it->second;
675 }
676
677 al_destroy_event_queue(event_queue);
678 al_destroy_display(display);
679 al_destroy_timer(timer);
680
681 outputLog << "Stopped client on " << getCurrentDateTimeString() << endl;
682 outputLog.close();
683
684 return 0;
685}
686
687void initWinSock()
688{
689#if defined WINDOWS
690 WORD wVersionRequested;
691 WSADATA wsaData;
692 int wsaerr;
693
694 wVersionRequested = MAKEWORD(2, 2);
695 wsaerr = WSAStartup(wVersionRequested, &wsaData);
696
697 if (wsaerr != 0) {
698 cout << "The Winsock dll not found." << endl;
699 exit(1);
700 }else
701 cout << "The Winsock dll was found." << endl;
702#endif
703}
704
705void shutdownWinSock()
706{
707#if defined WINDOWS
708 WSACleanup();
709#endif
710}
711
712void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player*>& mapPlayers,
713 map<unsigned int, Projectile>& mapProjectiles, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed)
714{
715 // this is outdated since most messages now don't contain just a text string
716 string response = string(msg.buffer);
717
718 switch(state)
719 {
720 case STATE_START:
721 {
722 cout << "In STATE_START" << endl;
723
724 switch(msg.type)
725 {
726 case MSG_TYPE_REGISTER:
727 {
728 lblRegisterStatus->setText(response);
729 break;
730 }
731 default:
732 {
733 cout << "(STATE_REGISTER) Received invalid message of type " << msg.type << endl;
734 break;
735 }
736 }
737
738 break;
739 }
740 case STATE_LOBBY:
741 cout << "In STATE_LOBBY" << endl;
742 case STATE_GAME:
743 {
744 switch(msg.type)
745 {
746 case MSG_TYPE_LOGIN:
747 {
748 if (response.compare("Player has already logged in.") == 0)
749 {
750 goToLoginScreen();
751 state = STATE_START;
752
753 lblLoginStatus->setText(response);
754 }
755 else if (response.compare("Incorrect username or password") == 0)
756 {
757 goToLoginScreen();
758 state = STATE_START;
759
760 lblLoginStatus->setText(response);
761 }
762 else
763 {
764 wndCurrent = wndLobby;
765
766 // this message should only be sent when a player first logs in so they know their id
767
768 Player* p = new Player("", "");
769 p->deserialize(msg.buffer);
770
771 if (mapPlayers.find(p->id) != mapPlayers.end())
772 delete mapPlayers[p->id];
773 mapPlayers[p->id] = p;
774 curPlayerId = p->id;
775
776 cout << "Got a valid login response with the player" << endl;
777 cout << "Player id: " << curPlayerId << endl;
778 cout << "Player health: " << p->health << endl;
779 cout << "player map size: " << mapPlayers.size() << endl;
780 }
781
782 break;
783 }
784 case MSG_TYPE_LOGOUT:
785 {
786 cout << "Got a logout message" << endl;
787
788 int playerId;
789
790 // Check if it's about you or another player
791 memcpy(&playerId, msg.buffer, 4);
792 response = string(msg.buffer+4);
793
794 if (playerId == curPlayerId)
795 {
796 if (response.compare("You have successfully logged out.") == 0)
797 {
798 cout << "Logged out" << endl;
799 state = STATE_START;
800 goToLoginScreen();
801 }
802
803 // if there was an error logging out, nothing happens
804 }
805 else
806 {
807 delete mapPlayers[playerId];
808 }
809
810 break;
811 }
812 case MSG_TYPE_PLAYER:
813 {
814 cout << "Received MSG_TYPE_PLAYER" << endl;
815
816 Player p("", "");
817 p.deserialize(msg.buffer);
818 p.timeLastUpdated = getCurrentMillis();
819 p.isChasing = false;
820 if (p.health <= 0)
821 p.isDead = true;
822 else
823 p.isDead = false;
824
825 if (mapPlayers.find(p.id) != mapPlayers.end())
826 *(mapPlayers[p.id]) = p;
827 else
828 mapPlayers[p.id] = new Player(p);
829
830 break;
831 }
832 case MSG_TYPE_PLAYER_MOVE:
833 {
834 unsigned int id;
835 int x, y;
836
837 memcpy(&id, msg.buffer, 4);
838 memcpy(&x, msg.buffer+4, 4);
839 memcpy(&y, msg.buffer+8, 4);
840
841 mapPlayers[id]->target.x = x;
842 mapPlayers[id]->target.y = y;
843
844 break;
845 }
846 case MSG_TYPE_CHAT:
847 {
848 chatConsole.addLine(response);
849
850 break;
851 }
852 case MSG_TYPE_OBJECT:
853 {
854 cout << "Received OBJECT message" << endl;
855
856 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
857 o.deserialize(msg.buffer);
858 cout << "object id: " << o.id << endl;
859 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
860
861 break;
862 }
863 case MSG_TYPE_REMOVE_OBJECT:
864 {
865 cout << "Received REMOVE_OBJECT message!" << endl;
866
867 int id;
868 memcpy(&id, msg.buffer, 4);
869
870 cout << "Removing object with id " << id << endl;
871
872 if (!gameMap->removeObject(id))
873 cout << "Did not remove the object" << endl;
874
875 break;
876 }
877 case MSG_TYPE_SCORE:
878 {
879 memcpy(&scoreBlue, msg.buffer, 4);
880 memcpy(&scoreRed, msg.buffer+4, 4);
881
882 break;
883 }
884 case MSG_TYPE_ATTACK:
885 {
886 cout << "Received ATTACK message" << endl;
887
888 break;
889 }
890 case MSG_TYPE_START_ATTACK:
891 {
892 cout << "Received START_ATTACK message" << endl;
893
894 unsigned int id, targetID;
895 memcpy(&id, msg.buffer, 4);
896 memcpy(&targetID, msg.buffer+4, 4);
897
898 cout << "source id: " << id << endl;
899 cout << "target id: " << targetID << endl;
900
901 Player* source = mapPlayers[id];
902 source->targetPlayer = targetID;
903 source->isChasing = true;
904
905 break;
906 }
907 case MSG_TYPE_PROJECTILE:
908 {
909 cout << "Received a PROJECTILE message" << endl;
910
911 unsigned int id, x, y, targetId;
912
913 memcpy(&id, msg.buffer, 4);
914 memcpy(&x, msg.buffer+4, 4);
915 memcpy(&y, msg.buffer+8, 4);
916 memcpy(&targetId, msg.buffer+12, 4);
917
918 cout << "id: " << id << endl;
919 cout << "x: " << x << endl;
920 cout << "y: " << y << endl;
921 cout << "Target: " << targetId << endl;
922
923 Projectile proj(x, y, targetId, 0);
924 proj.setId(id);
925
926 mapProjectiles[id] = proj;
927
928 break;
929 }
930 case MSG_TYPE_REMOVE_PROJECTILE:
931 {
932 cout << "Received a REMOVE_PROJECTILE message" << endl;
933
934 int id;
935 memcpy(&id, msg.buffer, 4);
936
937 mapProjectiles.erase(id);
938
939 break;
940 }
941 case MSG_TYPE_GAME_INFO:
942 {
943 cout << "Received a GAME_INFO message" << endl;
944
945 string gameName(msg.buffer+4);
946 int numPlayers;
947
948 memcpy(&numPlayers, msg.buffer, 4);
949
950 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
951
952 if (numPlayers > 0)
953 mapGames[gameName] = numPlayers;
954 else
955 mapGames.erase(gameName);
956
957 break;
958 }
959 case MSG_TYPE_JOIN_GAME_SUCCESS:
960 {
961 cout << "Received a JOIN_GAME_SUCCESS message" << endl;
962
963 string gameName(msg.buffer);
964 game = new Game(gameName, "../../data/map.txt");
965 cout << "Game name: " << gameName << endl;
966
967 state = STATE_NEW_GAME;
968 wndCurrent = wndNewGame;
969
970 msgTo.type = MSG_TYPE_JOIN_GAME_ACK;
971 strcpy(msgTo.buffer, gameName.c_str());
972
973 msgProcessor.sendMessage(&msgTo, &server);
974
975 break;
976 }
977 case MSG_TYPE_JOIN_GAME_FAILURE:
978 {
979 cout << "Received a JOIN_GAME_FAILURE message" << endl;
980
981 break;
982 }
983 default:
984 {
985 cout << "(STATE_LOBBY) Received invlaid message of type " << msg.type << endl;
986
987 break;
988 }
989 }
990
991 break;
992 }
993 case STATE_NEW_GAME:
994 {
995 cout << "(STATE_NEW_GAME) ";
996 switch(msg.type)
997 {
998 case MSG_TYPE_GAME_INFO:
999 {
1000 cout << "Received a GAME_INFO message" << endl;
1001
1002 string gameName(msg.buffer+4);
1003 int numPlayers;
1004
1005 memcpy(&numPlayers, msg.buffer, 4);
1006
1007 cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
1008
1009 if (numPlayers > 0)
1010 mapGames[gameName] = numPlayers;
1011 else
1012 mapGames.erase(gameName);
1013
1014 break;
1015 }
1016 case MSG_TYPE_SCORE:
1017 {
1018 cout << "Received SCORE message!" << endl;
1019
1020 int blueScore;
1021 memcpy(&blueScore, msg.buffer, 4);
1022 cout << "blue score: " << blueScore << endl;
1023 game->setBlueScore(blueScore);
1024
1025 int redScore;
1026 memcpy(&redScore, msg.buffer+4, 4);
1027 cout << "red score: " << redScore << endl;
1028 game->setRedScore(redScore);
1029
1030 cout << "Processed SCORE message!" << endl;
1031
1032 break;
1033 }
1034 case MSG_TYPE_FINISH_GAME:
1035 {
1036 cout << "Got a finish game message" << endl;
1037 cout << "Should switch to STATE_LOBBY and show the final score" << endl;
1038
1039 unsigned int winner, blueScore, redScore;
1040 memcpy(&winner, msg.buffer, 4);
1041 memcpy(&blueScore, msg.buffer+4, 4);
1042 memcpy(&redScore, msg.buffer+8, 4);
1043
1044 string gameName(msg.buffer+12);
1045
1046 cout << "winner: " << winner << endl;
1047 cout << "blueScore: " << blueScore << endl;
1048 cout << "redScore: " << redScore << endl;
1049 cout << "gameName: " << gameName << endl;
1050
1051 gameSummary = new GameSummary(gameName, winner, blueScore, redScore);
1052
1053 delete game;
1054 game = NULL;
1055 state = STATE_LOBBY;
1056 wndCurrent = wndGameSummary;
1057
1058
1059 cout << "winner from obj: " << gameSummary->getWinner() << endl;
1060 cout << "blueScore from obj: " << gameSummary->getBlueScore() << endl;
1061 cout << "redScore from obj: " << gameSummary->getRedScore() << endl;
1062 cout << "gameName from obj: " << gameSummary->getName() << endl;
1063 break;
1064 }
1065 case MSG_TYPE_PLAYER:
1066 {
1067 cout << "Received MSG_TYPE_PLAYER" << endl;
1068
1069 Player p("", "");
1070 p.deserialize(msg.buffer);
1071 p.timeLastUpdated = getCurrentMillis();
1072 p.isChasing = false;
1073 if (p.health <= 0)
1074 p.isDead = true;
1075 else
1076 p.isDead = false;
1077
1078 if (mapPlayers.find(p.id) != mapPlayers.end())
1079 *(mapPlayers[p.id]) = p;
1080 else
1081 mapPlayers[p.id] = new Player(p);
1082
1083 break;
1084 }
1085 case MSG_TYPE_PLAYER_JOIN_GAME:
1086 {
1087 cout << "Received MSG_TYPE_PLAYER_JOIN_GAME" << endl;
1088
1089 Player p("", "");
1090 p.deserialize(msg.buffer);
1091 p.timeLastUpdated = getCurrentMillis();
1092 p.isChasing = false;
1093 if (p.health <= 0)
1094 p.isDead = true;
1095 else
1096 p.isDead = false;
1097
1098 if (mapPlayers.find(p.id) != mapPlayers.end())
1099 *(mapPlayers[p.id]) = p;
1100 else
1101 mapPlayers[p.id] = new Player(p);
1102
1103 game->addPlayer(mapPlayers[p.id]);
1104
1105 break;
1106 }
1107 case MSG_TYPE_PLAYER_MOVE:
1108 {
1109 cout << "Received PLAYER_MOVE message" << endl;
1110
1111 unsigned int id;
1112 int x, y;
1113
1114 memcpy(&id, msg.buffer, 4);
1115 memcpy(&x, msg.buffer+4, 4);
1116 memcpy(&y, msg.buffer+8, 4);
1117
1118 cout << "id: " << id << endl;
1119
1120 mapPlayers[id]->target.x = x;
1121 mapPlayers[id]->target.y = y;
1122
1123 break;
1124 }
1125 case MSG_TYPE_OBJECT:
1126 {
1127 cout << "Received object message in STATE_NEW_GAME" << endl;
1128
1129 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
1130 o.deserialize(msg.buffer);
1131 cout << "object id: " << o.id << endl;
1132 game->getMap()->updateObject(o.id, o.type, o.pos.x, o.pos.y);
1133
1134 break;
1135 }
1136 case MSG_TYPE_REMOVE_OBJECT:
1137 {
1138 cout << "Received REMOVE_OBJECT message!" << endl;
1139
1140 int id;
1141 memcpy(&id, msg.buffer, 4);
1142
1143 cout << "Removing object with id " << id << endl;
1144
1145 if (!game->getMap()->removeObject(id))
1146 cout << "Did not remove the object" << endl;
1147
1148 break;
1149 }
1150 case MSG_TYPE_ATTACK:
1151 {
1152 cout << "Received ATTACK message" << endl;
1153
1154 break;
1155 }
1156 case MSG_TYPE_START_ATTACK:
1157 {
1158 cout << "Received START_ATTACK message" << endl;
1159
1160 unsigned int id, targetId;
1161 memcpy(&id, msg.buffer, 4);
1162 memcpy(&targetId, msg.buffer+4, 4);
1163
1164 cout << "source id: " << id << endl;
1165 cout << "target id: " << targetId << endl;
1166
1167 // need to check the target exists in the current game
1168 Player* source = game->getPlayers()[id];
1169 source->targetPlayer = targetId;
1170 source->isChasing = true;
1171
1172 break;
1173 }
1174 case MSG_TYPE_PROJECTILE:
1175 {
1176 cout << "Received a PROJECTILE message" << endl;
1177
1178 unsigned int projId, x, y, targetId;
1179
1180 memcpy(&projId, msg.buffer, 4);
1181 memcpy(&x, msg.buffer+4, 4);
1182 memcpy(&y, msg.buffer+8, 4);
1183 memcpy(&targetId, msg.buffer+12, 4);
1184
1185 cout << "projId: " << projId << endl;
1186 cout << "x: " << x << endl;
1187 cout << "y: " << y << endl;
1188 cout << "Target: " << targetId << endl;
1189
1190 Projectile proj(x, y, targetId, 0);
1191 proj.setId(projId);
1192
1193 game->addProjectile(proj);
1194
1195 break;
1196 }
1197 case MSG_TYPE_REMOVE_PROJECTILE:
1198 {
1199 cout << "Received a REMOVE_PROJECTILE message" << endl;
1200
1201 unsigned int id;
1202 memcpy(&id, msg.buffer, 4);
1203
1204 game->removeProjectile(id);
1205
1206 break;
1207 }
1208 default:
1209 {
1210 cout << "Received invalid message of type " << msg.type << endl;
1211
1212 break;
1213 }
1214 }
1215
1216 break;
1217 }
1218 default:
1219 {
1220 cout << "The state has an invalid value: " << state << endl;
1221
1222 break;
1223 }
1224 }
1225}
1226
1227int getRefreshRate(int width, int height)
1228{
1229 int numRefreshRates = al_get_num_display_modes();
1230 ALLEGRO_DISPLAY_MODE displayMode;
1231
1232 for(int i=0; i<numRefreshRates; i++) {
1233 al_get_display_mode(i, &displayMode);
1234
1235 if (displayMode.width == width && displayMode.height == height)
1236 return displayMode.refresh_rate;
1237 }
1238
1239 return 0;
1240}
1241
1242void drawMessageStatus(ALLEGRO_FONT* font)
1243{
1244 int clientMsgOffset = 5;
1245 int serverMsgOffset = 950;
1246
1247 al_draw_text(font, al_map_rgb(0, 255, 255), 0+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1248 al_draw_text(font, al_map_rgb(0, 255, 255), 20+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Type");
1249 al_draw_text(font, al_map_rgb(0, 255, 255), 240+clientMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "Acked?");
1250
1251 al_draw_text(font, al_map_rgb(0, 255, 255), serverMsgOffset, 43, ALLEGRO_ALIGN_LEFT, "ID");
1252
1253 map<unsigned int, map<unsigned long, MessageContainer> >& sentMessages = msgProcessor.getSentMessages();
1254 int id, type;
1255 bool acked;
1256 ostringstream ossId, ossAcked;
1257
1258 map<unsigned int, map<unsigned long, MessageContainer> >::iterator it;
1259
1260 int msgCount = 0;
1261 for (it = sentMessages.begin(); it != sentMessages.end(); it++) {
1262 map<unsigned long, MessageContainer> playerMessage = it->second;
1263 map<unsigned long, MessageContainer>::iterator it2;
1264 for (it2 = playerMessage.begin(); it2 != playerMessage.end(); it2++) {
1265
1266 id = it->first;
1267 ossId.str("");;
1268 ossId << id;
1269
1270 type = it2->second.getMessage()->type;
1271 string typeStr = MessageContainer::getMsgTypeString(type);
1272
1273 acked = it2->second.getAcked();
1274 ossAcked.str("");;
1275 ossAcked << boolalpha << acked;
1276
1277 al_draw_text(font, al_map_rgb(0, 255, 0), clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1278 al_draw_text(font, al_map_rgb(0, 255, 0), 20+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, typeStr.c_str());
1279 al_draw_text(font, al_map_rgb(0, 255, 0), 240+clientMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossAcked.str().c_str());
1280
1281 msgCount++;
1282 }
1283 }
1284
1285 if (msgProcessor.getAckedMessages().size() > 0) {
1286 map<unsigned int, unsigned long long> ackedMessages = msgProcessor.getAckedMessages()[0];
1287 map<unsigned int, unsigned long long>::iterator it3;
1288
1289 msgCount = 0;
1290 for (it3 = ackedMessages.begin(); it3 != ackedMessages.end(); it3++) {
1291 ossId.str("");;
1292 ossId << it3->first;
1293
1294 al_draw_text(font, al_map_rgb(255, 0, 0), 25+serverMsgOffset, 60+15*msgCount, ALLEGRO_ALIGN_LEFT, ossId.str().c_str());
1295
1296 msgCount++;
1297 }
1298 }
1299}
1300
1301// Callback definitions
1302
1303void goToRegisterScreen()
1304{
1305 txtUsernameRegister->clear();
1306 txtPasswordRegister->clear();
1307 lblRegisterStatus->setText("");
1308 rblClasses->setSelectedButton(-1);
1309
1310 wndCurrent = wndRegister;
1311}
1312
1313void goToLoginScreen()
1314{
1315 txtUsername->clear();
1316 txtPassword->clear();
1317 lblLoginStatus->setText("");
1318
1319 wndCurrent = wndLogin;
1320}
1321
1322// maybe need a goToGameScreen function as well and add state changes to these functions as well
1323
1324void registerAccount()
1325{
1326 string username = txtUsernameRegister->getStr();
1327 string password = txtPasswordRegister->getStr();
1328
1329 txtUsernameRegister->clear();
1330 txtPasswordRegister->clear();
1331 // maybe clear rblClasses as well (add a method to RadioButtonList to enable this)
1332
1333 Player::PlayerClass playerClass;
1334
1335 switch (rblClasses->getSelectedButton()) {
1336 case 0:
1337 playerClass = Player::CLASS_WARRIOR;
1338 break;
1339 case 1:
1340 playerClass = Player::CLASS_RANGER;
1341 break;
1342 default:
1343 cout << "Invalid class selection" << endl;
1344 playerClass = Player::CLASS_NONE;
1345 break;
1346 }
1347
1348 msgTo.type = MSG_TYPE_REGISTER;
1349
1350 strcpy(msgTo.buffer, username.c_str());
1351 strcpy(msgTo.buffer+username.size()+1, password.c_str());
1352 memcpy(msgTo.buffer+username.size()+password.size()+2, &playerClass, 4);
1353
1354 msgProcessor.sendMessage(&msgTo, &server);
1355}
1356
1357void login()
1358{
1359 string strUsername = txtUsername->getStr();
1360 string strPassword = txtPassword->getStr();
1361 username = strUsername;
1362
1363 txtUsername->clear();
1364 txtPassword->clear();
1365
1366 msgTo.type = MSG_TYPE_LOGIN;
1367
1368 strcpy(msgTo.buffer, strUsername.c_str());
1369 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
1370
1371 msgProcessor.sendMessage(&msgTo, &server);
1372
1373 state = STATE_LOBBY;
1374}
1375
1376void logout()
1377{
1378 switch(state) {
1379 case STATE_LOBBY:
1380 txtJoinGame->clear();
1381 txtCreateGame->clear();
1382 break;
1383 case STATE_GAME:
1384 txtChat->clear();
1385 chatConsole.clear();
1386 break;
1387 default:
1388 cout << "Logout called from invalid state: " << state << endl;
1389 break;
1390 }
1391
1392 msgTo.type = MSG_TYPE_LOGOUT;
1393
1394 strcpy(msgTo.buffer, username.c_str());
1395
1396 msgProcessor.sendMessage(&msgTo, &server);
1397}
1398
1399void quit()
1400{
1401 doexit = true;
1402}
1403
1404void sendChatMessage()
1405{
1406 string msg = txtChat->getStr();
1407 txtChat->clear();
1408
1409 msgTo.type = MSG_TYPE_CHAT;
1410 strcpy(msgTo.buffer, msg.c_str());
1411
1412 msgProcessor.sendMessage(&msgTo, &server);
1413}
1414
1415void toggleDebugging()
1416{
1417 debugging = !debugging;
1418}
1419
1420void joinGame()
1421{
1422 cout << "Joining game" << endl;
1423
1424 string msg = txtJoinGame->getStr();
1425 txtJoinGame->clear();
1426
1427 msgTo.type = MSG_TYPE_JOIN_GAME;
1428 strcpy(msgTo.buffer, msg.c_str());
1429
1430 msgProcessor.sendMessage(&msgTo, &server);
1431}
1432
1433void createGame()
1434{
1435 cout << "Creating game" << endl;
1436
1437 string msg = txtCreateGame->getStr();
1438 txtCreateGame->clear();
1439
1440 cout << "Sending message: " << msg.c_str() << endl;
1441
1442 msgTo.type = MSG_TYPE_CREATE_GAME;
1443 strcpy(msgTo.buffer, msg.c_str());
1444
1445 msgProcessor.sendMessage(&msgTo, &server);
1446}
1447
1448void leaveGame()
1449{
1450 cout << "Leaving game" << endl;
1451
1452 game = NULL;
1453
1454 state = STATE_LOBBY;
1455 wndCurrent = wndLobby;
1456
1457 msgTo.type = MSG_TYPE_LEAVE_GAME;
1458
1459 msgProcessor.sendMessage(&msgTo, &server);
1460}
1461
1462void closeGameSummary()
1463{
1464 delete gameSummary;
1465 gameSummary = NULL;
1466 wndCurrent = wndLobby;
1467 cout << "Processed button actions" << endl;
1468}
Note: See TracBrowser for help on using the repository browser.