Changeset 62ee2ce in network-game for client/Client/main.cpp


Ignore:
Timestamp:
Feb 5, 2013, 7:02:32 PM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
384b7e0, 60017fc
Parents:
60b77d2
Message:

The client shows the map and converts between screen and map coordinates

File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r60b77d2 r62ee2ce  
    2929#include "../../common/Message.h"
    3030#include "../../common/Common.h"
    31 #include "../../common/Player.h"_
     31#include "../../common/Player.h"
     32#include "../../common/WorldMap.h"
    3233
    3334#include "Window.h"
     
    4546void shutdownWinSock();
    4647void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId);
     48void drawMap(WorldMap* gameMap);
    4749void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId);
     50POSITION screenToMap(POSITION pos);
     51POSITION mapToScreen(POSITION pos);
    4852
    4953// callbacks
     
    156160   }
    157161
     162   WorldMap* gameMap = WorldMap::createDefaultMap();
     163
    158164   wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
    159165   wndLogin->addComponent(new Textbox(104, 40, 100, 20, font));
     
    306312            msgTo.type = MSG_TYPE_PLAYER_MOVE;
    307313
    308             memcpy(msgTo.buffer, &curPlayerId, 4);
    309             memcpy(msgTo.buffer+4, &ev.mouse.x, 4);
    310             memcpy(msgTo.buffer+8, &ev.mouse.y, 4);
    311 
    312             sendMessage(&msgTo, sock, &server);
     314            POSITION pos;
     315            pos.x = ev.mouse.x;
     316            pos.y = ev.mouse.y;
     317            pos = screenToMap(pos);
     318
     319            if (pos.x != -1)
     320            {
     321               memcpy(msgTo.buffer, &curPlayerId, 4);
     322               memcpy(msgTo.buffer+4, &pos.x, 4);
     323               memcpy(msgTo.buffer+8, &pos.y, 4);
     324
     325               sendMessage(&msgTo, sock, &server);
     326            }
     327            else
     328               cout << "Invalid point: User did not click on the map" << endl;
    313329         }
    314330      }
     
    326342         wndCurrent->draw(display);
    327343
    328          drawPlayers(mapPlayers, curPlayerId);
    329 
    330344         chatConsole.draw(font, al_map_rgb(255,255,255));
    331345
     
    336350         else if(wndCurrent == wndMain) {
    337351            al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
     352
     353            drawMap(gameMap);
     354            drawPlayers(mapPlayers, curPlayerId);
    338355         }
    339356
     
    352369   delete wndLogin;
    353370   delete wndMain;
     371
     372   delete gameMap;
    354373
    355374   al_destroy_event_queue(event_queue);
     
    392411   WSACleanup();
    393412#endif
     413}
     414
     415POSITION screenToMap(POSITION pos)
     416{
     417   pos.x = pos.x-300;
     418   pos.y = pos.y-100;
     419
     420   if (pos.x < 0 || pos.y < 0)
     421   {
     422      pos.x = -1;
     423      pos.y = -1;
     424   }
     425
     426   return pos;
     427}
     428
     429POSITION mapToScreen(POSITION pos)
     430{
     431   pos.x = pos.x+300;
     432   pos.y = pos.y+100;
     433
     434   return pos;
    394435}
    395436
     
    498539}
    499540
     541void drawMap(WorldMap* gameMap)
     542{
     543   POSITION mapPos;
     544   mapPos.x = 0;
     545   mapPos.y = 0;
     546   mapPos = mapToScreen(mapPos);
     547   for (int x=0; x<12; x++)
     548   {
     549      for (int y=0; y<12; y++)
     550      {
     551         WorldMap::TerrainType el = gameMap->getElement(x, y);
     552
     553         if (el == WorldMap::TERRAIN_GRASS)
     554            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));
     555         else if (el == WorldMap::TERRAIN_OCEAN)
     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, 0, 255));
     557         else if (el == WorldMap::TERRAIN_ROCK)
     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(100, 100, 0));
     559      }
     560   }
     561}
     562
    500563void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId)
    501564{
    502565   map<unsigned int, Player>::iterator it;
     566
     567   Player* p;
     568   POSITION pos;
    503569
    504570   for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
    505571   {
    506       Player *p = &it->second;
     572      p = &it->second;
     573      pos = mapToScreen(p->pos);
    507574
    508575      if (p->id == curPlayerId)
    509          al_draw_filled_circle(p->pos.x, p->pos.y, 15, al_map_rgb(0, 255, 0));
     576         al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(255, 0, 0));
    510577      else
    511          al_draw_filled_circle(p->pos.x, p->pos.y, 30, al_map_rgb(255, 0, 0));
     578         al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0));
    512579   }
    513580}
Note: See TracChangeset for help on using the changeset viewer.