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


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r3d81c0d r3a79253  
    1414
    1515#include <sys/types.h>
    16 #include <cstdio>
    17 #include <cstdlib>
     16#include <stdio.h>
     17#include <stdlib.h>
    1818#include <string>
    1919#include <iostream>
    20 #include <sstream>
    21 
    22 #include <map>
    2320
    2421#include <map>
     
    2724#include <allegro5/allegro_font.h>
    2825#include <allegro5/allegro_ttf.h>
    29 #include <allegro5/allegro_primitives.h>
    3026
    3127#include "../../common/Message.h"
    3228#include "../../common/Common.h"
    33 #include "../../common/WorldMap.h"
    3429#include "../../common/Player.h"
    3530
     
    4742void initWinSock();
    4843void 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);
     44void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole);
    5445
    5546// callbacks
     
    10899   bool redraw = true;
    109100   doexit = false;
    110    map<unsigned int, Player> mapPlayers;
    111    unsigned int curPlayerId = -1;
    112101
    113102   float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
     
    121110   }
    122111
    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();
    128113   al_init_font_addon();
    129114   al_init_ttf_addon();
    130115
    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 
    137118   if (!font) {
    138119      fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
     
    163144      return -1;
    164145   }
    165 
    166    WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
    167146
    168147   wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
     
    243222   {
    244223      ALLEGRO_EVENT ev;
    245      
    246224      al_wait_for_event(event_queue, &ev);
    247225
     
    313291         }
    314292      }
    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       }
    336293
    337294      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      }
    339299 
    340300      if (redraw && al_is_event_queue_empty(event_queue))
    341301      {
    342302         redraw = false;
    343 
     303 
    344304         wndCurrent->draw(display);
     305 
     306         al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
    345307
    346308         chatConsole.draw(font, al_map_rgb(255,255,255));
     
    352314         else if(wndCurrent == wndMain) {
    353315            al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
    354 
    355             drawMap(gameMap);
    356             drawPlayers(mapPlayers, curPlayerId);
    357316         }
    358317
     
    371330   delete wndLogin;
    372331   delete wndMain;
    373 
    374    delete gameMap;
    375332
    376333   al_destroy_event_queue(event_queue);
     
    415372}
    416373
    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)
     374void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole)
    449375{
    450376   string response = string(msg.buffer);
     377
     378   cout << "Got message: " << msg.type << endl;
    451379
    452380   switch(state)
     
    455383      {
    456384         cout << "In STATE_START" << endl;
     385
     386         chatConsole.addLine(response);
    457387
    458388         switch(msg.type)
     
    478408                  state = STATE_LOGIN;
    479409                  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;
    488411               }
    489 
    490412               break;
    491413            }
     
    496418      case STATE_LOGIN:
    497419      {
    498          switch(msg.type)
     420         chatConsole.addLine(response);
     421
     422          switch(msg.type)
    499423         {
    500424            case MSG_TYPE_REGISTER:
     
    504428            case MSG_TYPE_LOGIN:
    505429            {
    506                chatConsole.addLine(response);
    507 
    508430               if (response.compare("You have successfully logged out.") == 0)
    509431               {
     
    525447               mapPlayers[p.id] = p;
    526448
     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
    527454               break;
    528455            }
    529             case MSG_TYPE_CHAT:
    530             {
    531                chatConsole.addLine(response);
    532 
    533                break;
    534             }
    535          }
    536 
     456         }
     457                     
    537458         break;
    538459      }
     
    546467}
    547468
    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       else
    585          al_draw_filled_circle(pos.x, pos.y, 12, al_map_rgb(191, 0, 0));
    586    }
    587 }
    588 
    589469void registerAccount()
    590470{
Note: See TracChangeset for help on using the changeset viewer.