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


Ignore:
Timestamp:
Feb 24, 2013, 1:28:32 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
7b43385
Parents:
3a79253 (diff), 8f85180 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Updated files to correctly compile on Windows

File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r3a79253 rca44f82  
    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");
     167   //delete gameMap;
     168   //gameMap = WorldMap::createDefaultMap();
    146169
    147170   wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
     
    291314         }
    292315      }
     316      else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
     317         if(wndCurrent == wndMain) {
     318            msgTo.type = MSG_TYPE_PLAYER_MOVE;
     319
     320            POSITION pos;
     321            pos.x = ev.mouse.x;
     322            pos.y = ev.mouse.y;
     323            pos = screenToMap(pos);
     324
     325            if (pos.x != -1)
     326            {
     327               memcpy(msgTo.buffer, &curPlayerId, 4);
     328               memcpy(msgTo.buffer+4, &pos.x, 4);
     329               memcpy(msgTo.buffer+8, &pos.y, 4);
     330
     331               sendMessage(&msgTo, sock, &server);
     332            }
     333            else
     334               cout << "Invalid point: User did not click on the map" << endl;
     335         }
     336      }
    293337
    294338      if (receiveMessage(&msgFrom, sock, &from) >= 0)
    295339      {
    296          processMessage(msgFrom, state, chatConsole);
     340         processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId);
    297341         cout << "state: " << state << endl;
    298342      }
     
    301345      {
    302346         redraw = false;
    303  
     347
    304348         wndCurrent->draw(display);
    305  
    306          al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
    307349
    308350         chatConsole.draw(font, al_map_rgb(255,255,255));
     
    314356         else if(wndCurrent == wndMain) {
    315357            al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
     358
     359            drawMap(gameMap);
     360            drawPlayers(mapPlayers, curPlayerId);
    316361         }
    317362
     
    330375   delete wndLogin;
    331376   delete wndMain;
     377
     378   delete gameMap;
    332379
    333380   al_destroy_event_queue(event_queue);
     
    372419}
    373420
    374 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole)
     421POSITION screenToMap(POSITION pos)
     422{
     423   pos.x = pos.x-300;
     424   pos.y = pos.y-100;
     425
     426   if (pos.x < 0 || pos.y < 0)
     427   {
     428      pos.x = -1;
     429      pos.y = -1;
     430   }
     431
     432   return pos;
     433}
     434
     435POSITION mapToScreen(POSITION pos)
     436{
     437   pos.x = pos.x+300;
     438   pos.y = pos.y+100;
     439
     440   return pos;
     441}
     442
     443POSITION mapToScreen(FLOAT_POSITION pos)
     444{
     445   POSITION p;
     446   p.x = pos.x+300;
     447   p.y = pos.y+100;
     448
     449   return p;
     450}
     451
     452void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId)
    375453{
    376454   string response = string(msg.buffer);
     
    383461      {
    384462         cout << "In STATE_START" << endl;
    385 
    386          chatConsole.addLine(response);
    387463
    388464         switch(msg.type)
     
    408484                  state = STATE_LOGIN;
    409485                  wndCurrent = wndMain;
    410                   cout << "User login successful" << endl;
     486                 
     487                  Player p("", "");
     488                  p.deserialize(msg.buffer);
     489                  mapPlayers[p.id] = p;
     490                  curPlayerId = p.id;
     491
     492                  cout << "Got a valid login response with the player" << endl;
     493                  cout << "Player id: " << curPlayerId << endl;
    411494               }
     495
    412496               break;
    413497            }
     
    418502      case STATE_LOGIN:
    419503      {
    420          chatConsole.addLine(response);
    421 
    422           switch(msg.type)
     504         switch(msg.type)
    423505         {
    424506            case MSG_TYPE_REGISTER:
     
    428510            case MSG_TYPE_LOGIN:
    429511            {
     512               chatConsole.addLine(response);
     513
    430514               if (response.compare("You have successfully logged out.") == 0)
    431515               {
     
    448532
    449533               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;
    453534
    454535               break;
    455536            }
    456          }
    457                      
     537            case MSG_TYPE_CHAT:
     538            {
     539               chatConsole.addLine(response);
     540
     541               break;
     542            }
     543         }
     544
    458545         break;
    459546      }
     
    467554}
    468555
     556void drawMap(WorldMap* gameMap)
     557{
     558   POSITION mapPos;
     559   mapPos.x = 0;
     560   mapPos.y = 0;
     561   mapPos = mapToScreen(mapPos);
     562   for (int x=0; x<12; x++)
     563   {
     564      for (int y=0; y<12; y++)
     565      {
     566         WorldMap::TerrainType el = gameMap->getElement(x, y);
     567
     568         if (el == WorldMap::TERRAIN_GRASS)
     569            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));
     570         else if (el == WorldMap::TERRAIN_OCEAN)
     571            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));
     572         else if (el == WorldMap::TERRAIN_ROCK)
     573            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));
     574      }
     575   }
     576}
     577
     578void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId)
     579{
     580   map<unsigned int, Player>::iterator it;
     581
     582   Player* p;
     583   POSITION pos;
     584
     585   for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
     586   {
     587      p = &it->second;
     588      pos = mapToScreen(p->pos);
     589
     590      if (p->id == curPlayerId)
     591         al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(255, 0, 0));
     592      else
     593         al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0));
     594   }
     595}
     596
    469597void registerAccount()
    470598{
Note: See TracChangeset for help on using the changeset viewer.