Changes in client/Client/main.cpp [3d81c0d:3a79253] in network-game
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/Client/main.cpp
r3d81c0d 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 22 #include <map>23 20 24 21 #include <map> … … 27 24 #include <allegro5/allegro_font.h> 28 25 #include <allegro5/allegro_ttf.h> 29 #include <allegro5/allegro_primitives.h>30 26 31 27 #include "../../common/Message.h" 32 28 #include "../../common/Common.h" 33 #include "../../common/WorldMap.h"34 29 #include "../../common/Player.h" 35 30 … … 47 42 void initWinSock(); 48 43 void shutdownWinSock(); 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); 44 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole); 54 45 55 46 // callbacks … … 108 99 bool redraw = true; 109 100 doexit = false; 110 map<unsigned int, Player> mapPlayers;111 unsigned int curPlayerId = -1;112 101 113 102 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; … … 121 110 } 122 111 123 if (al_init_primitives_addon()) 124 cout << "Primitives initialized" << endl; 125 else 126 cout << "Primitives not initialized" << endl; 127 112 al_init_primitives_addon(); 128 113 al_init_font_addon(); 129 114 al_init_ttf_addon(); 130 115 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 116 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0); 117 137 118 if (!font) { 138 119 fprintf(stderr, "Could not load 'pirulen.ttf'.\n"); … … 163 144 return -1; 164 145 } 165 166 WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");167 146 168 147 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H); … … 243 222 { 244 223 ALLEGRO_EVENT ev; 245 246 224 al_wait_for_event(event_queue, &ev); 247 225 … … 313 291 } 314 292 } 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 else333 cout << "Invalid point: User did not click on the map" << endl;334 }335 }336 293 337 294 if (receiveMessage(&msgFrom, sock, &from) >= 0) 338 processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId); 295 { 296 processMessage(msgFrom, state, chatConsole); 297 cout << "state: " << state << endl; 298 } 339 299 340 300 if (redraw && al_is_event_queue_empty(event_queue)) 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 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) 374 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole) 449 375 { 450 376 string response = string(msg.buffer); 377 378 cout << "Got message: " << msg.type << endl; 451 379 452 380 switch(state) … … 455 383 { 456 384 cout << "In STATE_START" << endl; 385 386 chatConsole.addLine(response); 457 387 458 388 switch(msg.type) … … 478 408 state = STATE_LOGIN; 479 409 wndCurrent = wndMain; 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; 410 cout << "User login successful" << endl; 488 411 } 489 490 412 break; 491 413 } … … 496 418 case STATE_LOGIN: 497 419 { 498 switch(msg.type) 420 chatConsole.addLine(response); 421 422 switch(msg.type) 499 423 { 500 424 case MSG_TYPE_REGISTER: … … 504 428 case MSG_TYPE_LOGIN: 505 429 { 506 chatConsole.addLine(response);507 508 430 if (response.compare("You have successfully logged out.") == 0) 509 431 { … … 525 447 mapPlayers[p.id] = p; 526 448 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 527 454 break; 528 455 } 529 case MSG_TYPE_CHAT: 530 { 531 chatConsole.addLine(response); 532 533 break; 534 } 535 } 536 456 } 457 537 458 break; 538 459 } … … 546 467 } 547 468 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 else585 al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0));586 }587 }588 589 469 void registerAccount() 590 470 {
Note:
See TracChangeset
for help on using the changeset viewer.