Changes in client/Client/main.cpp [3a79253:384b7e0] in network-game


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r3a79253 r384b7e0  
    1414
    1515#include <sys/types.h>
    16 #include <stdio.h>
    17 #include <stdlib.h>
     16#include <cstdio>
     17#include <cstdlib>
    1818#include <string>
    1919#include <iostream>
     20#include <sstream>
    2021
    2122#include <map>
     
    2425#include <allegro5/allegro_font.h>
    2526#include <allegro5/allegro_ttf.h>
     27#include <allegro5/allegro_primitives.h>
    2628
    2729#include "../../common/Message.h"
    2830#include "../../common/Common.h"
     31#include "../../common/WorldMap.h"
    2932#include "../../common/Player.h"
    3033
     
    4245void initWinSock();
    4346void shutdownWinSock();
    44 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole);
     47void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId);
     48void drawMap(WorldMap* gameMap);
     49void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId);
     50POSITION screenToMap(POSITION pos);
     51POSITION mapToScreen(POSITION pos);
    4552
    4653// callbacks
     
    7380bool doexit;
    7481
    75 map<unsigned int, Player> mapPlayers;
    76 
    7782Window* wndLogin;
    7883Window* wndMain;
     
    99104   bool redraw = true;
    100105   doexit = false;
     106   map<unsigned int, Player> mapPlayers;
     107   unsigned int curPlayerId = -1;
    101108
    102109   float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
     
    110117   }
    111118
    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
    113124   al_init_font_addon();
    114125   al_init_ttf_addon();
    115126
    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
    118133   if (!font) {
    119134      fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
     
    144159      return -1;
    145160   }
     161
     162   WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
     163   //delete gameMap;
     164   //gameMap = WorldMap::createDefaultMap();
    146165
    147166   wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
     
    291310         }
    292311      }
     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      }
    293333
    294334      if (receiveMessage(&msgFrom, sock, &from) >= 0)
    295335      {
    296          processMessage(msgFrom, state, chatConsole);
     336         processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId);
    297337         cout << "state: " << state << endl;
    298338      }
     
    301341      {
    302342         redraw = false;
    303  
     343
    304344         wndCurrent->draw(display);
    305  
    306          al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
    307345
    308346         chatConsole.draw(font, al_map_rgb(255,255,255));
     
    314352         else if(wndCurrent == wndMain) {
    315353            al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
     354
     355            drawMap(gameMap);
     356            drawPlayers(mapPlayers, curPlayerId);
    316357         }
    317358
     
    330371   delete wndLogin;
    331372   delete wndMain;
     373
     374   delete gameMap;
    332375
    333376   al_destroy_event_queue(event_queue);
     
    372415}
    373416
    374 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole)
     417POSITION 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
     431POSITION mapToScreen(POSITION pos)
     432{
     433   pos.x = pos.x+300;
     434   pos.y = pos.y+100;
     435
     436   return pos;
     437}
     438
     439void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId)
    375440{
    376441   string response = string(msg.buffer);
     
    383448      {
    384449         cout << "In STATE_START" << endl;
    385 
    386          chatConsole.addLine(response);
    387450
    388451         switch(msg.type)
     
    408471                  state = STATE_LOGIN;
    409472                  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;
    411481               }
     482
    412483               break;
    413484            }
     
    418489      case STATE_LOGIN:
    419490      {
    420          chatConsole.addLine(response);
    421 
    422           switch(msg.type)
     491         switch(msg.type)
    423492         {
    424493            case MSG_TYPE_REGISTER:
     
    428497            case MSG_TYPE_LOGIN:
    429498            {
     499               chatConsole.addLine(response);
     500
    430501               if (response.compare("You have successfully logged out.") == 0)
    431502               {
     
    447518               mapPlayers[p.id] = p;
    448519
    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;
    453521
    454522               break;
    455523            }
    456          }
    457                      
     524            case MSG_TYPE_CHAT:
     525            {
     526               chatConsole.addLine(response);
     527
     528               break;
     529            }
     530         }
     531
    458532         break;
    459533      }
     
    467541}
    468542
     543void 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
     565void 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
    469584void registerAccount()
    470585{
Note: See TracChangeset for help on using the changeset viewer.