[4c202e0] | 1 | #include "../../common/Compiler.h"
|
---|
| 2 |
|
---|
[e08572c] | 3 | #if defined WINDOWS
|
---|
[0dde5da] | 4 | #include <winsock2.h>
|
---|
| 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 |
|
---|
[d352805] | 15 | #include <sys/types.h>
|
---|
[88cdae2] | 16 | #include <cstdio>
|
---|
| 17 | #include <cstdlib>
|
---|
[a845faf] | 18 | #include <string>
|
---|
[1912323] | 19 | #include <iostream>
|
---|
[88cdae2] | 20 | #include <sstream>
|
---|
[1912323] | 21 |
|
---|
[eb8adb1] | 22 | #include <map>
|
---|
| 23 |
|
---|
[d352805] | 24 | #include <allegro5/allegro.h>
|
---|
| 25 | #include <allegro5/allegro_font.h>
|
---|
| 26 | #include <allegro5/allegro_ttf.h>
|
---|
[88cdae2] | 27 | #include <allegro5/allegro_primitives.h>
|
---|
[7d7df47] | 28 |
|
---|
[b53c6b3] | 29 | #include "../../common/Message.h"
|
---|
[e607c0f] | 30 | #include "../../common/Common.h"
|
---|
[88cdae2] | 31 | #include "../../common/Player.h"_
|
---|
[7d7df47] | 32 |
|
---|
[87b3ee2] | 33 | #include "Window.h"
|
---|
| 34 | #include "Textbox.h"
|
---|
| 35 | #include "Button.h"
|
---|
[6475138] | 36 | #include "chat.h"
|
---|
| 37 |
|
---|
[a845faf] | 38 | #ifdef WINDOWS
|
---|
[6475138] | 39 | #pragma comment(lib, "ws2_32.lib")
|
---|
[a845faf] | 40 | #endif
|
---|
[1912323] | 41 |
|
---|
| 42 | using namespace std;
|
---|
| 43 |
|
---|
[0dde5da] | 44 | void initWinSock();
|
---|
| 45 | void shutdownWinSock();
|
---|
[88cdae2] | 46 | void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId);
|
---|
| 47 | void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId);
|
---|
[87b3ee2] | 48 |
|
---|
| 49 | // callbacks
|
---|
| 50 | void registerAccount();
|
---|
| 51 | void login();
|
---|
| 52 | void logout();
|
---|
| 53 | void quit();
|
---|
| 54 | void sendChatMessage();
|
---|
[4da5aa3] | 55 |
|
---|
[1912323] | 56 | void error(const char *);
|
---|
[7d7df47] | 57 |
|
---|
[d352805] | 58 | const float FPS = 60;
|
---|
| 59 | const int SCREEN_W = 640;
|
---|
| 60 | const int SCREEN_H = 480;
|
---|
| 61 | const int BOUNCER_SIZE = 32;
|
---|
| 62 | enum MYKEYS {
|
---|
[0cc431d] | 63 | KEY_UP,
|
---|
| 64 | KEY_DOWN,
|
---|
| 65 | KEY_LEFT,
|
---|
| 66 | KEY_RIGHT
|
---|
| 67 | };
|
---|
| 68 |
|
---|
| 69 | enum STATE {
|
---|
| 70 | STATE_START,
|
---|
[87b3ee2] | 71 | STATE_LOGIN // this means you're already logged in
|
---|
[d352805] | 72 | };
|
---|
[87b3ee2] | 73 |
|
---|
| 74 | int state;
|
---|
| 75 |
|
---|
| 76 | bool doexit;
|
---|
| 77 |
|
---|
| 78 | Window* wndLogin;
|
---|
| 79 | Window* wndMain;
|
---|
| 80 | Window* wndCurrent;
|
---|
| 81 |
|
---|
| 82 | Textbox* txtUsername;
|
---|
| 83 | Textbox* txtPassword;
|
---|
| 84 | Textbox* txtChat;
|
---|
| 85 |
|
---|
| 86 | int sock;
|
---|
| 87 | struct sockaddr_in server, from;
|
---|
| 88 | struct hostent *hp;
|
---|
| 89 | NETWORK_MSG msgTo, msgFrom;
|
---|
| 90 | string username;
|
---|
| 91 | chat chatConsole;
|
---|
[d352805] | 92 |
|
---|
| 93 | int main(int argc, char **argv)
|
---|
| 94 | {
|
---|
| 95 | ALLEGRO_DISPLAY *display = NULL;
|
---|
| 96 | ALLEGRO_EVENT_QUEUE *event_queue = NULL;
|
---|
| 97 | ALLEGRO_TIMER *timer = NULL;
|
---|
| 98 | ALLEGRO_BITMAP *bouncer = NULL;
|
---|
| 99 | bool key[4] = { false, false, false, false };
|
---|
| 100 | bool redraw = true;
|
---|
[87b3ee2] | 101 | doexit = false;
|
---|
[eb8adb1] | 102 | map<unsigned int, Player> mapPlayers;
|
---|
[88cdae2] | 103 | unsigned int curPlayerId = -1;
|
---|
[9a3e6b1] | 104 |
|
---|
[87b3ee2] | 105 | float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
|
---|
| 106 | float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
|
---|
[0cc431d] | 107 |
|
---|
[87b3ee2] | 108 | state = STATE_START;
|
---|
[9a3e6b1] | 109 |
|
---|
[d352805] | 110 | if(!al_init()) {
|
---|
| 111 | fprintf(stderr, "failed to initialize allegro!\n");
|
---|
| 112 | return -1;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[88cdae2] | 115 | if (al_init_primitives_addon())
|
---|
| 116 | cout << "Primitives initialized" << endl;
|
---|
| 117 | else
|
---|
| 118 | cout << "Primitives not initialized" << endl;
|
---|
| 119 |
|
---|
[d352805] | 120 | al_init_font_addon();
|
---|
| 121 | al_init_ttf_addon();
|
---|
| 122 |
|
---|
[88cdae2] | 123 | #if defined WINDOWS
|
---|
| 124 | ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
|
---|
| 125 | #elif defined LINUX
|
---|
| 126 | ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
|
---|
| 127 | #endif
|
---|
| 128 |
|
---|
[d352805] | 129 | if (!font) {
|
---|
| 130 | fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
|
---|
| 131 | getchar();
|
---|
| 132 | return -1;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | if(!al_install_keyboard()) {
|
---|
| 136 | fprintf(stderr, "failed to initialize the keyboard!\n");
|
---|
| 137 | return -1;
|
---|
| 138 | }
|
---|
[87b3ee2] | 139 |
|
---|
| 140 | if(!al_install_mouse()) {
|
---|
| 141 | fprintf(stderr, "failed to initialize the mouse!\n");
|
---|
| 142 | return -1;
|
---|
| 143 | }
|
---|
[d352805] | 144 |
|
---|
| 145 | timer = al_create_timer(1.0 / FPS);
|
---|
| 146 | if(!timer) {
|
---|
| 147 | fprintf(stderr, "failed to create timer!\n");
|
---|
| 148 | return -1;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | display = al_create_display(SCREEN_W, SCREEN_H);
|
---|
| 152 | if(!display) {
|
---|
| 153 | fprintf(stderr, "failed to create display!\n");
|
---|
| 154 | al_destroy_timer(timer);
|
---|
| 155 | return -1;
|
---|
| 156 | }
|
---|
[87b3ee2] | 157 |
|
---|
| 158 | wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
| 159 | wndLogin->addComponent(new Textbox(104, 40, 100, 20, font));
|
---|
| 160 | wndLogin->addComponent(new Textbox(104, 70, 100, 20, font));
|
---|
| 161 | wndLogin->addComponent(new Button(22, 100, 90, 20, font, "Register", registerAccount));
|
---|
| 162 | wndLogin->addComponent(new Button(122, 100, 60, 20, font, "Login", login));
|
---|
| 163 | wndLogin->addComponent(new Button(540, 10, 80, 20, font, "Quit", quit));
|
---|
| 164 |
|
---|
| 165 | txtUsername = (Textbox*)wndLogin->getComponent(0);
|
---|
| 166 | txtPassword = (Textbox*)wndLogin->getComponent(1);
|
---|
| 167 |
|
---|
| 168 | wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
|
---|
| 169 | wndMain->addComponent(new Textbox(95, 40, 525, 20, font));
|
---|
| 170 | wndMain->addComponent(new Button(95, 70, 160, 20, font, "Send Message", sendChatMessage));
|
---|
| 171 | wndMain->addComponent(new Button(540, 10, 80, 20, font, "Logout", logout));
|
---|
| 172 |
|
---|
| 173 | txtChat = (Textbox*)wndMain->getComponent(0);
|
---|
| 174 |
|
---|
| 175 | wndCurrent = wndLogin;
|
---|
| 176 |
|
---|
[d352805] | 177 | bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
|
---|
| 178 | if(!bouncer) {
|
---|
| 179 | fprintf(stderr, "failed to create bouncer bitmap!\n");
|
---|
| 180 | al_destroy_display(display);
|
---|
| 181 | al_destroy_timer(timer);
|
---|
| 182 | return -1;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | event_queue = al_create_event_queue();
|
---|
| 186 | if(!event_queue) {
|
---|
| 187 | fprintf(stderr, "failed to create event_queue!\n");
|
---|
| 188 | al_destroy_bitmap(bouncer);
|
---|
| 189 | al_destroy_display(display);
|
---|
| 190 | al_destroy_timer(timer);
|
---|
| 191 | return -1;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | al_set_target_bitmap(bouncer);
|
---|
| 195 |
|
---|
| 196 | al_clear_to_color(al_map_rgb(255, 0, 255));
|
---|
| 197 |
|
---|
| 198 | al_set_target_bitmap(al_get_backbuffer(display));
|
---|
| 199 |
|
---|
| 200 | al_register_event_source(event_queue, al_get_display_event_source(display));
|
---|
| 201 | al_register_event_source(event_queue, al_get_timer_event_source(timer));
|
---|
| 202 | al_register_event_source(event_queue, al_get_keyboard_event_source());
|
---|
[87b3ee2] | 203 | al_register_event_source(event_queue, al_get_mouse_event_source());
|
---|
[d352805] | 204 |
|
---|
| 205 | al_clear_to_color(al_map_rgb(0,0,0));
|
---|
| 206 |
|
---|
| 207 | al_flip_display();
|
---|
[9a3e6b1] | 208 |
|
---|
| 209 | if (argc != 3) {
|
---|
| 210 | cout << "Usage: server port" << endl;
|
---|
| 211 | exit(1);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | initWinSock();
|
---|
| 215 |
|
---|
| 216 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
| 217 | if (sock < 0)
|
---|
| 218 | error("socket");
|
---|
| 219 |
|
---|
[e607c0f] | 220 | set_nonblock(sock);
|
---|
| 221 |
|
---|
[9a3e6b1] | 222 | server.sin_family = AF_INET;
|
---|
| 223 | hp = gethostbyname(argv[1]);
|
---|
| 224 | if (hp==0)
|
---|
| 225 | error("Unknown host");
|
---|
| 226 |
|
---|
| 227 | memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
|
---|
| 228 | server.sin_port = htons(atoi(argv[2]));
|
---|
| 229 |
|
---|
[d352805] | 230 | al_start_timer(timer);
|
---|
[e607c0f] | 231 |
|
---|
[d352805] | 232 | while(!doexit)
|
---|
| 233 | {
|
---|
| 234 | ALLEGRO_EVENT ev;
|
---|
| 235 | al_wait_for_event(event_queue, &ev);
|
---|
[87b3ee2] | 236 |
|
---|
| 237 | if(wndCurrent->handleEvent(ev)) {
|
---|
| 238 | // do nothing
|
---|
| 239 | }
|
---|
| 240 | else if(ev.type == ALLEGRO_EVENT_TIMER) {
|
---|
[d352805] | 241 | if(key[KEY_UP] && bouncer_y >= 4.0) {
|
---|
| 242 | bouncer_y -= 4.0;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0) {
|
---|
| 246 | bouncer_y += 4.0;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | if(key[KEY_LEFT] && bouncer_x >= 4.0) {
|
---|
| 250 | bouncer_x -= 4.0;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0) {
|
---|
| 254 | bouncer_x += 4.0;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | redraw = true;
|
---|
| 258 | }
|
---|
| 259 | else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
|
---|
[9a3e6b1] | 260 | doexit = true;
|
---|
[d352805] | 261 | }
|
---|
| 262 | else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
|
---|
[87b3ee2] | 263 | switch(ev.keyboard.keycode) {
|
---|
| 264 | case ALLEGRO_KEY_UP:
|
---|
| 265 | key[KEY_UP] = true;
|
---|
| 266 | break;
|
---|
[d352805] | 267 |
|
---|
[87b3ee2] | 268 | case ALLEGRO_KEY_DOWN:
|
---|
| 269 | key[KEY_DOWN] = true;
|
---|
| 270 | break;
|
---|
[d352805] | 271 |
|
---|
[87b3ee2] | 272 | case ALLEGRO_KEY_LEFT:
|
---|
| 273 | key[KEY_LEFT] = true;
|
---|
| 274 | break;
|
---|
[d352805] | 275 |
|
---|
[87b3ee2] | 276 | case ALLEGRO_KEY_RIGHT:
|
---|
| 277 | key[KEY_RIGHT] = true;
|
---|
| 278 | break;
|
---|
[d352805] | 279 | }
|
---|
| 280 | }
|
---|
| 281 | else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
|
---|
| 282 | switch(ev.keyboard.keycode) {
|
---|
| 283 | case ALLEGRO_KEY_UP:
|
---|
| 284 | key[KEY_UP] = false;
|
---|
| 285 | break;
|
---|
| 286 |
|
---|
| 287 | case ALLEGRO_KEY_DOWN:
|
---|
| 288 | key[KEY_DOWN] = false;
|
---|
| 289 | break;
|
---|
| 290 |
|
---|
| 291 | case ALLEGRO_KEY_LEFT:
|
---|
| 292 | key[KEY_LEFT] = false;
|
---|
| 293 | break;
|
---|
| 294 |
|
---|
| 295 | case ALLEGRO_KEY_RIGHT:
|
---|
| 296 | key[KEY_RIGHT] = false;
|
---|
| 297 | break;
|
---|
| 298 |
|
---|
| 299 | case ALLEGRO_KEY_ESCAPE:
|
---|
| 300 | doexit = true;
|
---|
| 301 | break;
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
[88cdae2] | 304 | else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
|
---|
[ad5d122] | 305 | if(wndCurrent == wndMain) {
|
---|
| 306 | msgTo.type = MSG_TYPE_PLAYER_MOVE;
|
---|
[88cdae2] | 307 |
|
---|
[ad5d122] | 308 | memcpy(msgTo.buffer, &curPlayerId, 4);
|
---|
| 309 | memcpy(msgTo.buffer+4, &ev.mouse.x, 4);
|
---|
| 310 | memcpy(msgTo.buffer+8, &ev.mouse.y, 4);
|
---|
[88cdae2] | 311 |
|
---|
[ad5d122] | 312 | sendMessage(&msgTo, sock, &server);
|
---|
| 313 | }
|
---|
[88cdae2] | 314 | }
|
---|
[e607c0f] | 315 |
|
---|
| 316 | if (receiveMessage(&msgFrom, sock, &from) >= 0)
|
---|
| 317 | {
|
---|
[88cdae2] | 318 | processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId);
|
---|
[e607c0f] | 319 | cout << "state: " << state << endl;
|
---|
| 320 | }
|
---|
[d352805] | 321 |
|
---|
[e607c0f] | 322 | if (redraw && al_is_event_queue_empty(event_queue))
|
---|
| 323 | {
|
---|
[d352805] | 324 | redraw = false;
|
---|
[88cdae2] | 325 |
|
---|
[87b3ee2] | 326 | wndCurrent->draw(display);
|
---|
[88cdae2] | 327 |
|
---|
| 328 | drawPlayers(mapPlayers, curPlayerId);
|
---|
[9a3e6b1] | 329 |
|
---|
[6475138] | 330 | chatConsole.draw(font, al_map_rgb(255,255,255));
|
---|
[9a3e6b1] | 331 |
|
---|
[87b3ee2] | 332 | if(wndCurrent == wndLogin) {
|
---|
| 333 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Username:");
|
---|
| 334 | al_draw_text(font, al_map_rgb(0, 255, 0), 1, 73, ALLEGRO_ALIGN_LEFT, "Password:");
|
---|
| 335 | }
|
---|
| 336 | else if(wndCurrent == wndMain) {
|
---|
| 337 | al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
|
---|
| 338 | }
|
---|
| 339 |
|
---|
[d352805] | 340 | al_flip_display();
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
[9a3e6b1] | 343 |
|
---|
| 344 | #if defined WINDOWS
|
---|
| 345 | closesocket(sock);
|
---|
| 346 | #elif defined LINUX
|
---|
| 347 | close(sock);
|
---|
| 348 | #endif
|
---|
| 349 |
|
---|
| 350 | shutdownWinSock();
|
---|
[d352805] | 351 |
|
---|
[87b3ee2] | 352 | delete wndLogin;
|
---|
| 353 | delete wndMain;
|
---|
| 354 |
|
---|
[d352805] | 355 | al_destroy_event_queue(event_queue);
|
---|
| 356 | al_destroy_bitmap(bouncer);
|
---|
| 357 | al_destroy_display(display);
|
---|
| 358 | al_destroy_timer(timer);
|
---|
| 359 |
|
---|
| 360 | return 0;
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[4da5aa3] | 363 | // need to make a function like this that works on windows
|
---|
| 364 | void error(const char *msg)
|
---|
| 365 | {
|
---|
| 366 | perror(msg);
|
---|
| 367 | shutdownWinSock();
|
---|
| 368 | exit(1);
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[0dde5da] | 371 | void initWinSock()
|
---|
| 372 | {
|
---|
| 373 | #if defined WINDOWS
|
---|
| 374 | WORD wVersionRequested;
|
---|
| 375 | WSADATA wsaData;
|
---|
| 376 | int wsaerr;
|
---|
| 377 |
|
---|
| 378 | wVersionRequested = MAKEWORD(2, 2);
|
---|
| 379 | wsaerr = WSAStartup(wVersionRequested, &wsaData);
|
---|
| 380 |
|
---|
| 381 | if (wsaerr != 0) {
|
---|
| 382 | cout << "The Winsock dll not found." << endl;
|
---|
| 383 | exit(1);
|
---|
| 384 | }else
|
---|
| 385 | cout << "The Winsock dll was found." << endl;
|
---|
| 386 | #endif
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | void shutdownWinSock()
|
---|
| 390 | {
|
---|
| 391 | #if defined WINDOWS
|
---|
| 392 | WSACleanup();
|
---|
| 393 | #endif
|
---|
[1912323] | 394 | }
|
---|
| 395 |
|
---|
[88cdae2] | 396 | void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId)
|
---|
[1912323] | 397 | {
|
---|
[4da5aa3] | 398 | string response = string(msg.buffer);
|
---|
| 399 |
|
---|
[60776f2] | 400 | cout << "Got message: " << msg.type << endl;
|
---|
| 401 |
|
---|
[4da5aa3] | 402 | switch(state)
|
---|
| 403 | {
|
---|
| 404 | case STATE_START:
|
---|
| 405 | {
|
---|
[e607c0f] | 406 | cout << "In STATE_START" << endl;
|
---|
| 407 |
|
---|
[87b3ee2] | 408 | switch(msg.type)
|
---|
[4da5aa3] | 409 | {
|
---|
[87b3ee2] | 410 | case MSG_TYPE_REGISTER:
|
---|
| 411 | {
|
---|
| 412 | break;
|
---|
| 413 | }
|
---|
| 414 | case MSG_TYPE_LOGIN:
|
---|
| 415 | {
|
---|
| 416 | if (response.compare("Player has already logged in.") == 0)
|
---|
| 417 | {
|
---|
| 418 | username.clear();
|
---|
| 419 | cout << "User login failed" << endl;
|
---|
| 420 | }
|
---|
| 421 | else if (response.compare("Incorrect username or password") == 0)
|
---|
| 422 | {
|
---|
| 423 | username.clear();
|
---|
| 424 | cout << "User login failed" << endl;
|
---|
| 425 | }
|
---|
| 426 | else
|
---|
| 427 | {
|
---|
| 428 | state = STATE_LOGIN;
|
---|
| 429 | wndCurrent = wndMain;
|
---|
[88cdae2] | 430 |
|
---|
| 431 | Player p("", "");
|
---|
| 432 | p.deserialize(msg.buffer);
|
---|
| 433 | mapPlayers[p.id] = p;
|
---|
| 434 | curPlayerId = p.id;
|
---|
| 435 |
|
---|
| 436 | cout << "Got a valid login response with the player" << endl;
|
---|
| 437 | cout << "Player id: " << curPlayerId << endl;
|
---|
[87b3ee2] | 438 | }
|
---|
[88cdae2] | 439 |
|
---|
[87b3ee2] | 440 | break;
|
---|
| 441 | }
|
---|
[4da5aa3] | 442 | }
|
---|
| 443 |
|
---|
| 444 | break;
|
---|
| 445 | }
|
---|
| 446 | case STATE_LOGIN:
|
---|
| 447 | {
|
---|
[eb8adb1] | 448 | switch(msg.type)
|
---|
[4da5aa3] | 449 | {
|
---|
[87b3ee2] | 450 | case MSG_TYPE_REGISTER:
|
---|
| 451 | {
|
---|
| 452 | break;
|
---|
| 453 | }
|
---|
| 454 | case MSG_TYPE_LOGIN:
|
---|
| 455 | {
|
---|
[eb8adb1] | 456 | chatConsole.addLine(response);
|
---|
| 457 |
|
---|
[87b3ee2] | 458 | if (response.compare("You have successfully logged out.") == 0)
|
---|
| 459 | {
|
---|
| 460 | cout << "Logged out" << endl;
|
---|
| 461 | state = STATE_START;
|
---|
| 462 | wndCurrent = wndLogin;
|
---|
| 463 | }
|
---|
| 464 | else
|
---|
| 465 | {
|
---|
| 466 | cout << "Added new line" << endl;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[4c202e0] | 469 | break;
|
---|
| 470 | }
|
---|
| 471 | case MSG_TYPE_PLAYER:
|
---|
| 472 | {
|
---|
[60776f2] | 473 | Player p("", "");
|
---|
| 474 | p.deserialize(msg.buffer);
|
---|
[eb8adb1] | 475 | mapPlayers[p.id] = p;
|
---|
| 476 |
|
---|
[88cdae2] | 477 | cout << "Received MSG_TYPE_PLAYER message" << endl;
|
---|
| 478 |
|
---|
[eb8adb1] | 479 | break;
|
---|
| 480 | }
|
---|
| 481 | case MSG_TYPE_CHAT:
|
---|
| 482 | {
|
---|
| 483 | chatConsole.addLine(response);
|
---|
| 484 |
|
---|
[87b3ee2] | 485 | break;
|
---|
| 486 | }
|
---|
[4da5aa3] | 487 | }
|
---|
[eb8adb1] | 488 |
|
---|
[4da5aa3] | 489 | break;
|
---|
| 490 | }
|
---|
| 491 | default:
|
---|
| 492 | {
|
---|
| 493 | cout << "The state has an invalid value: " << state << endl;
|
---|
| 494 |
|
---|
| 495 | break;
|
---|
| 496 | }
|
---|
| 497 | }
|
---|
[0dde5da] | 498 | }
|
---|
[87b3ee2] | 499 |
|
---|
[88cdae2] | 500 | void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId)
|
---|
| 501 | {
|
---|
| 502 | map<unsigned int, Player>::iterator it;
|
---|
| 503 |
|
---|
| 504 | for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 505 | {
|
---|
| 506 | Player *p = &it->second;
|
---|
| 507 |
|
---|
| 508 | if (p->id == curPlayerId)
|
---|
| 509 | al_draw_filled_circle(p->pos.x, p->pos.y, 15, al_map_rgb(0, 255, 0));
|
---|
| 510 | else
|
---|
| 511 | al_draw_filled_circle(p->pos.x, p->pos.y, 30, al_map_rgb(255, 0, 0));
|
---|
| 512 | }
|
---|
| 513 | }
|
---|
| 514 |
|
---|
[87b3ee2] | 515 | void registerAccount()
|
---|
| 516 | {
|
---|
| 517 | string username = txtUsername->getStr();
|
---|
| 518 | string password = txtPassword->getStr();
|
---|
| 519 |
|
---|
| 520 | txtUsername->clear();
|
---|
| 521 | txtPassword->clear();
|
---|
| 522 |
|
---|
| 523 | msgTo.type = MSG_TYPE_REGISTER;
|
---|
| 524 |
|
---|
| 525 | strcpy(msgTo.buffer, username.c_str());
|
---|
| 526 | strcpy(msgTo.buffer+username.size()+1, password.c_str());
|
---|
| 527 |
|
---|
| 528 | sendMessage(&msgTo, sock, &server);
|
---|
| 529 | }
|
---|
| 530 |
|
---|
| 531 | void login()
|
---|
| 532 | {
|
---|
| 533 | string strUsername = txtUsername->getStr();
|
---|
| 534 | string strPassword = txtPassword->getStr();
|
---|
| 535 | username = strUsername;
|
---|
| 536 |
|
---|
| 537 | txtUsername->clear();
|
---|
| 538 | txtPassword->clear();
|
---|
| 539 |
|
---|
| 540 | msgTo.type = MSG_TYPE_LOGIN;
|
---|
| 541 |
|
---|
| 542 | strcpy(msgTo.buffer, strUsername.c_str());
|
---|
| 543 | strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
|
---|
| 544 |
|
---|
| 545 | sendMessage(&msgTo, sock, &server);
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | void logout()
|
---|
| 549 | {
|
---|
| 550 | txtChat->clear();
|
---|
| 551 |
|
---|
| 552 | msgTo.type = MSG_TYPE_LOGOUT;
|
---|
| 553 |
|
---|
| 554 | strcpy(msgTo.buffer, username.c_str());
|
---|
| 555 |
|
---|
| 556 | sendMessage(&msgTo, sock, &server);
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | void quit()
|
---|
| 560 | {
|
---|
| 561 | doexit = true;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | void sendChatMessage()
|
---|
| 565 | {
|
---|
| 566 | string msg = txtChat->getStr();
|
---|
| 567 |
|
---|
| 568 | txtChat->clear();
|
---|
| 569 |
|
---|
| 570 | msgTo.type = MSG_TYPE_CHAT;
|
---|
| 571 |
|
---|
| 572 | strcpy(msgTo.buffer, msg.c_str());
|
---|
| 573 |
|
---|
| 574 | sendMessage(&msgTo, sock, &server);
|
---|
| 575 | }
|
---|