Changes in client/Client/main.cpp [3a79253:384b7e0] in network-game
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
r3a79253 r384b7e0 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" 31 #include "../../common/WorldMap.h" 29 32 #include "../../common/Player.h" 30 33 … … 42 45 void initWinSock(); 43 46 void shutdownWinSock(); 44 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole); 47 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId); 48 void drawMap(WorldMap* gameMap); 49 void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId); 50 POSITION screenToMap(POSITION pos); 51 POSITION mapToScreen(POSITION pos); 45 52 46 53 // callbacks … … 73 80 bool doexit; 74 81 75 map<unsigned int, Player> mapPlayers;76 77 82 Window* wndLogin; 78 83 Window* wndMain; … … 99 104 bool redraw = true; 100 105 doexit = false; 106 map<unsigned int, Player> mapPlayers; 107 unsigned int curPlayerId = -1; 101 108 102 109 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; … … 110 117 } 111 118 112 al_init_primitives_addon(); 119 if (al_init_primitives_addon()) 120 cout << "Primitives initialized" << endl; 121 else 122 cout << "Primitives not initialized" << endl; 123 113 124 al_init_font_addon(); 114 125 al_init_ttf_addon(); 115 126 116 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0); 117 127 #if defined WINDOWS 128 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0); 129 #elif defined LINUX 130 ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0); 131 #endif 132 118 133 if (!font) { 119 134 fprintf(stderr, "Could not load 'pirulen.ttf'.\n"); … … 144 159 return -1; 145 160 } 161 162 WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt"); 163 //delete gameMap; 164 //gameMap = WorldMap::createDefaultMap(); 146 165 147 166 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H); … … 291 310 } 292 311 } 312 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 313 if(wndCurrent == wndMain) { 314 msgTo.type = MSG_TYPE_PLAYER_MOVE; 315 316 POSITION pos; 317 pos.x = ev.mouse.x; 318 pos.y = ev.mouse.y; 319 pos = screenToMap(pos); 320 321 if (pos.x != -1) 322 { 323 memcpy(msgTo.buffer, &curPlayerId, 4); 324 memcpy(msgTo.buffer+4, &pos.x, 4); 325 memcpy(msgTo.buffer+8, &pos.y, 4); 326 327 sendMessage(&msgTo, sock, &server); 328 } 329 else 330 cout << "Invalid point: User did not click on the map" << endl; 331 } 332 } 293 333 294 334 if (receiveMessage(&msgFrom, sock, &from) >= 0) 295 335 { 296 processMessage(msgFrom, state, chatConsole );336 processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId); 297 337 cout << "state: " << state << endl; 298 338 } … … 301 341 { 302 342 redraw = false; 303 343 304 344 wndCurrent->draw(display); 305 306 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);307 345 308 346 chatConsole.draw(font, al_map_rgb(255,255,255)); … … 314 352 else if(wndCurrent == wndMain) { 315 353 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:"); 354 355 drawMap(gameMap); 356 drawPlayers(mapPlayers, curPlayerId); 316 357 } 317 358 … … 330 371 delete wndLogin; 331 372 delete wndMain; 373 374 delete gameMap; 332 375 333 376 al_destroy_event_queue(event_queue); … … 372 415 } 373 416 374 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole) 417 POSITION screenToMap(POSITION pos) 418 { 419 pos.x = pos.x-300; 420 pos.y = pos.y-100; 421 422 if (pos.x < 0 || pos.y < 0) 423 { 424 pos.x = -1; 425 pos.y = -1; 426 } 427 428 return pos; 429 } 430 431 POSITION mapToScreen(POSITION pos) 432 { 433 pos.x = pos.x+300; 434 pos.y = pos.y+100; 435 436 return pos; 437 } 438 439 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId) 375 440 { 376 441 string response = string(msg.buffer); … … 383 448 { 384 449 cout << "In STATE_START" << endl; 385 386 chatConsole.addLine(response);387 450 388 451 switch(msg.type) … … 408 471 state = STATE_LOGIN; 409 472 wndCurrent = wndMain; 410 cout << "User login successful" << endl; 473 474 Player p("", ""); 475 p.deserialize(msg.buffer); 476 mapPlayers[p.id] = p; 477 curPlayerId = p.id; 478 479 cout << "Got a valid login response with the player" << endl; 480 cout << "Player id: " << curPlayerId << endl; 411 481 } 482 412 483 break; 413 484 } … … 418 489 case STATE_LOGIN: 419 490 { 420 chatConsole.addLine(response); 421 422 switch(msg.type) 491 switch(msg.type) 423 492 { 424 493 case MSG_TYPE_REGISTER: … … 428 497 case MSG_TYPE_LOGIN: 429 498 { 499 chatConsole.addLine(response); 500 430 501 if (response.compare("You have successfully logged out.") == 0) 431 502 { … … 447 518 mapPlayers[p.id] = p; 448 519 449 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; 520 cout << "Received MSG_TYPE_PLAYER message" << endl; 453 521 454 522 break; 455 523 } 456 } 457 524 case MSG_TYPE_CHAT: 525 { 526 chatConsole.addLine(response); 527 528 break; 529 } 530 } 531 458 532 break; 459 533 } … … 467 541 } 468 542 543 void drawMap(WorldMap* gameMap) 544 { 545 POSITION mapPos; 546 mapPos.x = 0; 547 mapPos.y = 0; 548 mapPos = mapToScreen(mapPos); 549 for (int x=0; x<12; x++) 550 { 551 for (int y=0; y<12; y++) 552 { 553 WorldMap::TerrainType el = gameMap->getElement(x, y); 554 555 if (el == WorldMap::TERRAIN_GRASS) 556 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)); 557 else if (el == WorldMap::TERRAIN_OCEAN) 558 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)); 559 else if (el == WorldMap::TERRAIN_ROCK) 560 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)); 561 } 562 } 563 } 564 565 void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId) 566 { 567 map<unsigned int, Player>::iterator it; 568 569 Player* p; 570 POSITION pos; 571 572 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++) 573 { 574 p = &it->second; 575 pos = mapToScreen(p->pos); 576 577 if (p->id == curPlayerId) 578 al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(255, 0, 0)); 579 else 580 al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0)); 581 } 582 } 583 469 584 void registerAccount() 470 585 {
Note:
See TracChangeset
for help on using the changeset viewer.