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


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r3a79253 r3d81c0d  
    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>
     21
     22#include <map>
    2023
    2124#include <map>
     
    2427#include <allegro5/allegro_font.h>
    2528#include <allegro5/allegro_ttf.h>
     29#include <allegro5/allegro_primitives.h>
    2630
    2731#include "../../common/Message.h"
    2832#include "../../common/Common.h"
     33#include "../../common/WorldMap.h"
    2934#include "../../common/Player.h"
    3035
     
    4247void initWinSock();
    4348void shutdownWinSock();
    44 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole);
     49void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId);
     50void drawMap(WorldMap* gameMap);
     51void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId);
     52POSITION screenToMap(POSITION pos);
     53POSITION mapToScreen(POSITION pos);
    4554
    4655// callbacks
     
    99108   bool redraw = true;
    100109   doexit = false;
     110   map<unsigned int, Player> mapPlayers;
     111   unsigned int curPlayerId = -1;
    101112
    102113   float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
     
    110121   }
    111122
    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
    113128   al_init_font_addon();
    114129   al_init_ttf_addon();
    115130
    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
    118137   if (!font) {
    119138      fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
     
    144163      return -1;
    145164   }
     165
     166   WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
    146167
    147168   wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
     
    222243   {
    223244      ALLEGRO_EVENT ev;
     245     
    224246      al_wait_for_event(event_queue, &ev);
    225247
     
    291313         }
    292314      }
     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      }
    293336
    294337      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);
    299339 
    300340      if (redraw && al_is_event_queue_empty(event_queue))
    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
     439POSITION 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
     448void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId)
    375449{
    376450   string response = string(msg.buffer);
    377 
    378    cout << "Got message: " << msg.type << endl;
    379451
    380452   switch(state)
     
    383455      {
    384456         cout << "In STATE_START" << endl;
    385 
    386          chatConsole.addLine(response);
    387457
    388458         switch(msg.type)
     
    408478                  state = STATE_LOGIN;
    409479                  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;
    411488               }
     489
    412490               break;
    413491            }
     
    418496      case STATE_LOGIN:
    419497      {
    420          chatConsole.addLine(response);
    421 
    422           switch(msg.type)
     498         switch(msg.type)
    423499         {
    424500            case MSG_TYPE_REGISTER:
     
    428504            case MSG_TYPE_LOGIN:
    429505            {
     506               chatConsole.addLine(response);
     507
    430508               if (response.compare("You have successfully logged out.") == 0)
    431509               {
     
    447525               mapPlayers[p.id] = p;
    448526
    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 
    454527               break;
    455528            }
    456          }
    457                      
     529            case MSG_TYPE_CHAT:
     530            {
     531               chatConsole.addLine(response);
     532
     533               break;
     534            }
     535         }
     536
    458537         break;
    459538      }
     
    467546}
    468547
     548void 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
     570void 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
    469589void registerAccount()
    470590{
Note: See TracChangeset for help on using the changeset viewer.