Changeset ca44f82 in network-game for client/Client/main.cpp
- Timestamp:
- Feb 24, 2013, 1:28:32 AM (12 years ago)
- Branches:
- master
- Children:
- 7b43385
- Parents:
- 3a79253 (diff), 8f85180 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
r3a79253 rca44f82 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> 21 22 #include <map> 20 23 21 24 #include <map> … … 24 27 #include <allegro5/allegro_font.h> 25 28 #include <allegro5/allegro_ttf.h> 29 #include <allegro5/allegro_primitives.h> 26 30 27 31 #include "../../common/Message.h" 28 32 #include "../../common/Common.h" 33 #include "../../common/WorldMap.h" 29 34 #include "../../common/Player.h" 30 35 … … 42 47 void initWinSock(); 43 48 void shutdownWinSock(); 44 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole); 49 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId); 50 void drawMap(WorldMap* gameMap); 51 void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId); 52 POSITION screenToMap(POSITION pos); 53 POSITION mapToScreen(POSITION pos); 45 54 46 55 // callbacks … … 99 108 bool redraw = true; 100 109 doexit = false; 110 map<unsigned int, Player> mapPlayers; 111 unsigned int curPlayerId = -1; 101 112 102 113 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; … … 110 121 } 111 122 112 al_init_primitives_addon(); 123 if (al_init_primitives_addon()) 124 cout << "Primitives initialized" << endl; 125 else 126 cout << "Primitives not initialized" << endl; 127 113 128 al_init_font_addon(); 114 129 al_init_ttf_addon(); 115 130 116 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0); 117 131 #if defined WINDOWS 132 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0); 133 #elif defined LINUX 134 ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0); 135 #endif 136 118 137 if (!font) { 119 138 fprintf(stderr, "Could not load 'pirulen.ttf'.\n"); … … 144 163 return -1; 145 164 } 165 166 WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt"); 167 //delete gameMap; 168 //gameMap = WorldMap::createDefaultMap(); 146 169 147 170 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H); … … 291 314 } 292 315 } 316 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 317 if(wndCurrent == wndMain) { 318 msgTo.type = MSG_TYPE_PLAYER_MOVE; 319 320 POSITION pos; 321 pos.x = ev.mouse.x; 322 pos.y = ev.mouse.y; 323 pos = screenToMap(pos); 324 325 if (pos.x != -1) 326 { 327 memcpy(msgTo.buffer, &curPlayerId, 4); 328 memcpy(msgTo.buffer+4, &pos.x, 4); 329 memcpy(msgTo.buffer+8, &pos.y, 4); 330 331 sendMessage(&msgTo, sock, &server); 332 } 333 else 334 cout << "Invalid point: User did not click on the map" << endl; 335 } 336 } 293 337 294 338 if (receiveMessage(&msgFrom, sock, &from) >= 0) 295 339 { 296 processMessage(msgFrom, state, chatConsole );340 processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId); 297 341 cout << "state: " << state << endl; 298 342 } … … 301 345 { 302 346 redraw = false; 303 347 304 348 wndCurrent->draw(display); 305 306 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);307 349 308 350 chatConsole.draw(font, al_map_rgb(255,255,255)); … … 314 356 else if(wndCurrent == wndMain) { 315 357 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:"); 358 359 drawMap(gameMap); 360 drawPlayers(mapPlayers, curPlayerId); 316 361 } 317 362 … … 330 375 delete wndLogin; 331 376 delete wndMain; 377 378 delete gameMap; 332 379 333 380 al_destroy_event_queue(event_queue); … … 372 419 } 373 420 374 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole) 421 POSITION screenToMap(POSITION pos) 422 { 423 pos.x = pos.x-300; 424 pos.y = pos.y-100; 425 426 if (pos.x < 0 || pos.y < 0) 427 { 428 pos.x = -1; 429 pos.y = -1; 430 } 431 432 return pos; 433 } 434 435 POSITION mapToScreen(POSITION pos) 436 { 437 pos.x = pos.x+300; 438 pos.y = pos.y+100; 439 440 return pos; 441 } 442 443 POSITION mapToScreen(FLOAT_POSITION pos) 444 { 445 POSITION p; 446 p.x = pos.x+300; 447 p.y = pos.y+100; 448 449 return p; 450 } 451 452 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId) 375 453 { 376 454 string response = string(msg.buffer); … … 383 461 { 384 462 cout << "In STATE_START" << endl; 385 386 chatConsole.addLine(response);387 463 388 464 switch(msg.type) … … 408 484 state = STATE_LOGIN; 409 485 wndCurrent = wndMain; 410 cout << "User login successful" << endl; 486 487 Player p("", ""); 488 p.deserialize(msg.buffer); 489 mapPlayers[p.id] = p; 490 curPlayerId = p.id; 491 492 cout << "Got a valid login response with the player" << endl; 493 cout << "Player id: " << curPlayerId << endl; 411 494 } 495 412 496 break; 413 497 } … … 418 502 case STATE_LOGIN: 419 503 { 420 chatConsole.addLine(response); 421 422 switch(msg.type) 504 switch(msg.type) 423 505 { 424 506 case MSG_TYPE_REGISTER: … … 428 510 case MSG_TYPE_LOGIN: 429 511 { 512 chatConsole.addLine(response); 513 430 514 if (response.compare("You have successfully logged out.") == 0) 431 515 { … … 448 532 449 533 cout << "p.id: " << p.id << endl; 450 cout << "p.name: " << p.name << endl;451 cout << "p.pos.x: " << p.pos.x << endl;452 cout << "p.pos.y: " << p.pos.y << endl;453 534 454 535 break; 455 536 } 456 } 457 537 case MSG_TYPE_CHAT: 538 { 539 chatConsole.addLine(response); 540 541 break; 542 } 543 } 544 458 545 break; 459 546 } … … 467 554 } 468 555 556 void drawMap(WorldMap* gameMap) 557 { 558 POSITION mapPos; 559 mapPos.x = 0; 560 mapPos.y = 0; 561 mapPos = mapToScreen(mapPos); 562 for (int x=0; x<12; x++) 563 { 564 for (int y=0; y<12; y++) 565 { 566 WorldMap::TerrainType el = gameMap->getElement(x, y); 567 568 if (el == WorldMap::TERRAIN_GRASS) 569 al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 255, 0)); 570 else if (el == WorldMap::TERRAIN_OCEAN) 571 al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 0, 255)); 572 else if (el == WorldMap::TERRAIN_ROCK) 573 al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(100, 100, 0)); 574 } 575 } 576 } 577 578 void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId) 579 { 580 map<unsigned int, Player>::iterator it; 581 582 Player* p; 583 POSITION pos; 584 585 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++) 586 { 587 p = &it->second; 588 pos = mapToScreen(p->pos); 589 590 if (p->id == curPlayerId) 591 al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(255, 0, 0)); 592 else 593 al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0)); 594 } 595 } 596 469 597 void registerAccount() 470 598 {
Note:
See TracChangeset
for help on using the changeset viewer.