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

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

The global projectile map and related code is gone from main.cpp, the client processes LOGOUT messages about other players from STATE_GAME, and the map drawing code in GameRender uses switches instead of ifs

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