Changes in client/Client/main.cpp [3a79253:3d81c0d] in network-game
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
r3a79253 r3d81c0d 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"); 146 167 147 168 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H); … … 222 243 { 223 244 ALLEGRO_EVENT ev; 245 224 246 al_wait_for_event(event_queue, &ev); 225 247 … … 291 313 } 292 314 } 315 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 316 if(wndCurrent == wndMain) { 317 msgTo.type = MSG_TYPE_PLAYER_MOVE; 318 319 POSITION pos; 320 pos.x = ev.mouse.x; 321 pos.y = ev.mouse.y; 322 pos = screenToMap(pos); 323 324 if (pos.x != -1) 325 { 326 memcpy(msgTo.buffer, &curPlayerId, 4); 327 memcpy(msgTo.buffer+4, &pos.x, 4); 328 memcpy(msgTo.buffer+8, &pos.y, 4); 329 330 sendMessage(&msgTo, sock, &server); 331 } 332 else 333 cout << "Invalid point: User did not click on the map" << endl; 334 } 335 } 293 336 294 337 if (receiveMessage(&msgFrom, sock, &from) >= 0) 295 { 296 processMessage(msgFrom, state, chatConsole); 297 cout << "state: " << state << endl; 298 } 338 processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId); 299 339 300 340 if (redraw && al_is_event_queue_empty(event_queue)) 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 POSITION mapToScreen(FLOAT_POSITION pos) 440 { 441 POSITION p; 442 p.x = pos.x+300; 443 p.y = pos.y+100; 444 445 return p; 446 } 447 448 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId) 375 449 { 376 450 string response = string(msg.buffer); 377 378 cout << "Got message: " << msg.type << endl;379 451 380 452 switch(state) … … 383 455 { 384 456 cout << "In STATE_START" << endl; 385 386 chatConsole.addLine(response);387 457 388 458 switch(msg.type) … … 408 478 state = STATE_LOGIN; 409 479 wndCurrent = wndMain; 410 cout << "User login successful" << endl; 480 481 Player p("", ""); 482 p.deserialize(msg.buffer); 483 mapPlayers[p.id] = p; 484 curPlayerId = p.id; 485 486 cout << "Got a valid login response with the player" << endl; 487 cout << "Player id: " << curPlayerId << endl; 411 488 } 489 412 490 break; 413 491 } … … 418 496 case STATE_LOGIN: 419 497 { 420 chatConsole.addLine(response); 421 422 switch(msg.type) 498 switch(msg.type) 423 499 { 424 500 case MSG_TYPE_REGISTER: … … 428 504 case MSG_TYPE_LOGIN: 429 505 { 506 chatConsole.addLine(response); 507 430 508 if (response.compare("You have successfully logged out.") == 0) 431 509 { … … 447 525 mapPlayers[p.id] = p; 448 526 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;453 454 527 break; 455 528 } 456 } 457 529 case MSG_TYPE_CHAT: 530 { 531 chatConsole.addLine(response); 532 533 break; 534 } 535 } 536 458 537 break; 459 538 } … … 467 546 } 468 547 548 void drawMap(WorldMap* gameMap) 549 { 550 POSITION mapPos; 551 mapPos.x = 0; 552 mapPos.y = 0; 553 mapPos = mapToScreen(mapPos); 554 for (int x=0; x<12; x++) 555 { 556 for (int y=0; y<12; y++) 557 { 558 WorldMap::TerrainType el = gameMap->getElement(x, y); 559 560 if (el == WorldMap::TERRAIN_GRASS) 561 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)); 562 else if (el == WorldMap::TERRAIN_OCEAN) 563 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)); 564 else if (el == WorldMap::TERRAIN_ROCK) 565 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)); 566 } 567 } 568 } 569 570 void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId) 571 { 572 map<unsigned int, Player>::iterator it; 573 574 Player* p; 575 POSITION pos; 576 577 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++) 578 { 579 p = &it->second; 580 pos = mapToScreen(p->pos); 581 582 if (p->id == curPlayerId) 583 al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(255, 0, 0)); 584 else 585 al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0)); 586 } 587 } 588 469 589 void registerAccount() 470 590 {
Note:
See TracChangeset
for help on using the changeset viewer.