- Timestamp:
- Jan 1, 2013, 12:58:18 AM (12 years ago)
- Branches:
- master
- Children:
- 80b3f94
- Parents:
- 594d2e9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
r594d2e9 r88cdae2 14 14 15 15 #include <sys/types.h> 16 #include < stdio.h>17 #include < stdlib.h>16 #include <cstdio> 17 #include <cstdlib> 18 18 #include <string> 19 19 #include <iostream> 20 #include <sstream> 20 21 21 22 #include <map> … … 24 25 #include <allegro5/allegro_font.h> 25 26 #include <allegro5/allegro_ttf.h> 27 #include <allegro5/allegro_primitives.h> 26 28 27 29 #include "../../common/Message.h" 28 30 #include "../../common/Common.h" 29 #include "../../common/Player.h" 31 #include "../../common/Player.h"_ 30 32 31 33 #include "Window.h" … … 42 44 void initWinSock(); 43 45 void shutdownWinSock(); 44 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers); 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); 45 48 46 49 // callbacks … … 98 101 doexit = false; 99 102 map<unsigned int, Player> mapPlayers; 103 unsigned int curPlayerId = -1; 100 104 101 105 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; … … 109 113 } 110 114 111 al_init_primitives_addon(); 115 if (al_init_primitives_addon()) 116 cout << "Primitives initialized" << endl; 117 else 118 cout << "Primitives not initialized" << endl; 119 112 120 al_init_font_addon(); 113 121 al_init_ttf_addon(); 114 122 115 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0); 116 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 117 129 if (!font) { 118 130 fprintf(stderr, "Could not load 'pirulen.ttf'.\n"); … … 290 302 } 291 303 } 304 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 305 mapPlayers[curPlayerId].pos.x = ev.mouse.x; 306 mapPlayers[curPlayerId].pos.y = ev.mouse.y; 307 308 // send the server a MSG_TYPE_PLAYER_MOVE message 309 msgTo.type = MSG_TYPE_PLAYER_MOVE; 310 311 ostringstream oss; 312 oss << ev.mouse.x; 313 oss << ev.mouse.y; 314 315 memcpy(msgTo.buffer, oss.str().c_str(), oss.str().length()); 316 sendMessage(&msgTo, sock, &server); 317 } 292 318 293 319 if (receiveMessage(&msgFrom, sock, &from) >= 0) 294 320 { 295 processMessage(msgFrom, state, chatConsole, mapPlayers );321 processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId); 296 322 cout << "state: " << state << endl; 297 323 } … … 300 326 { 301 327 redraw = false; 302 328 303 329 wndCurrent->draw(display); 304 305 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);330 331 drawPlayers(mapPlayers, curPlayerId); 306 332 307 333 chatConsole.draw(font, al_map_rgb(255,255,255)); … … 371 397 } 372 398 373 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers )399 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId) 374 400 { 375 401 string response = string(msg.buffer); … … 382 408 { 383 409 cout << "In STATE_START" << endl; 384 385 chatConsole.addLine(response);386 410 387 411 switch(msg.type) … … 407 431 state = STATE_LOGIN; 408 432 wndCurrent = wndMain; 409 cout << "User login successful" << endl; 433 434 Player p("", ""); 435 p.deserialize(msg.buffer); 436 mapPlayers[p.id] = p; 437 curPlayerId = p.id; 438 439 cout << "Got a valid login response with the player" << endl; 440 cout << "Player id: " << curPlayerId << endl; 410 441 } 442 411 443 break; 412 444 } … … 444 476 Player p("", ""); 445 477 p.deserialize(msg.buffer); 446 447 cout << "p.id: " << p.id << endl;448 cout << "p.name: " << p.name << endl;449 cout << "p.pos.x: " << p.pos.x << endl;450 cout << "p.pos.y: " << p.pos.y << endl;451 452 478 mapPlayers[p.id] = p; 479 480 cout << "Received MSG_TYPE_PLAYER message" << endl; 453 481 454 482 break; … … 473 501 } 474 502 503 void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId) 504 { 505 map<unsigned int, Player>::iterator it; 506 507 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++) 508 { 509 Player *p = &it->second; 510 511 if (p->id == curPlayerId) 512 al_draw_filled_circle(p->pos.x, p->pos.y, 15, al_map_rgb(0, 255, 0)); 513 else 514 al_draw_filled_circle(p->pos.x, p->pos.y, 30, al_map_rgb(255, 0, 0)); 515 } 516 } 517 475 518 void registerAccount() 476 519 {
Note:
See TracChangeset
for help on using the changeset viewer.