Changes in client/Client/main.cpp [384b7e0:3a79253] in network-game
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
r384b7e0 r3a79253 14 14 15 15 #include <sys/types.h> 16 #include < cstdio>17 #include < cstdlib>16 #include <stdio.h> 17 #include <stdlib.h> 18 18 #include <string> 19 19 #include <iostream> 20 #include <sstream>21 20 22 21 #include <map> … … 25 24 #include <allegro5/allegro_font.h> 26 25 #include <allegro5/allegro_ttf.h> 27 #include <allegro5/allegro_primitives.h>28 26 29 27 #include "../../common/Message.h" 30 28 #include "../../common/Common.h" 31 #include "../../common/WorldMap.h"32 29 #include "../../common/Player.h" 33 30 … … 45 42 void initWinSock(); 46 43 void shutdownWinSock(); 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); 44 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole); 52 45 53 46 // callbacks … … 80 73 bool doexit; 81 74 75 map<unsigned int, Player> mapPlayers; 76 82 77 Window* wndLogin; 83 78 Window* wndMain; … … 104 99 bool redraw = true; 105 100 doexit = false; 106 map<unsigned int, Player> mapPlayers;107 unsigned int curPlayerId = -1;108 101 109 102 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; … … 117 110 } 118 111 119 if (al_init_primitives_addon()) 120 cout << "Primitives initialized" << endl; 121 else 122 cout << "Primitives not initialized" << endl; 123 112 al_init_primitives_addon(); 124 113 al_init_font_addon(); 125 114 al_init_ttf_addon(); 126 115 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 116 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0); 117 133 118 if (!font) { 134 119 fprintf(stderr, "Could not load 'pirulen.ttf'.\n"); … … 159 144 return -1; 160 145 } 161 162 WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");163 //delete gameMap;164 //gameMap = WorldMap::createDefaultMap();165 146 166 147 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H); … … 310 291 } 311 292 } 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 else330 cout << "Invalid point: User did not click on the map" << endl;331 }332 }333 293 334 294 if (receiveMessage(&msgFrom, sock, &from) >= 0) 335 295 { 336 processMessage(msgFrom, state, chatConsole , mapPlayers, curPlayerId);296 processMessage(msgFrom, state, chatConsole); 337 297 cout << "state: " << state << endl; 338 298 } … … 341 301 { 342 302 redraw = false; 343 303 344 304 wndCurrent->draw(display); 305 306 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0); 345 307 346 308 chatConsole.draw(font, al_map_rgb(255,255,255)); … … 352 314 else if(wndCurrent == wndMain) { 353 315 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:"); 354 355 drawMap(gameMap);356 drawPlayers(mapPlayers, curPlayerId);357 316 } 358 317 … … 371 330 delete wndLogin; 372 331 delete wndMain; 373 374 delete gameMap;375 332 376 333 al_destroy_event_queue(event_queue); … … 415 372 } 416 373 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) 374 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole) 440 375 { 441 376 string response = string(msg.buffer); … … 448 383 { 449 384 cout << "In STATE_START" << endl; 385 386 chatConsole.addLine(response); 450 387 451 388 switch(msg.type) … … 471 408 state = STATE_LOGIN; 472 409 wndCurrent = wndMain; 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; 410 cout << "User login successful" << endl; 481 411 } 482 483 412 break; 484 413 } … … 489 418 case STATE_LOGIN: 490 419 { 491 switch(msg.type) 420 chatConsole.addLine(response); 421 422 switch(msg.type) 492 423 { 493 424 case MSG_TYPE_REGISTER: … … 497 428 case MSG_TYPE_LOGIN: 498 429 { 499 chatConsole.addLine(response);500 501 430 if (response.compare("You have successfully logged out.") == 0) 502 431 { … … 518 447 mapPlayers[p.id] = p; 519 448 520 cout << "Received MSG_TYPE_PLAYER message" << endl; 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; 521 453 522 454 break; 523 455 } 524 case MSG_TYPE_CHAT: 525 { 526 chatConsole.addLine(response); 527 528 break; 529 } 530 } 531 456 } 457 532 458 break; 533 459 } … … 541 467 } 542 468 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 else580 al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0));581 }582 }583 584 469 void registerAccount() 585 470 {
Note:
See TracChangeset
for help on using the changeset viewer.