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


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r384b7e0 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>
    2120
    2221#include <map>
     
    2524#include <allegro5/allegro_font.h>
    2625#include <allegro5/allegro_ttf.h>
    27 #include <allegro5/allegro_primitives.h>
    2826
    2927#include "../../common/Message.h"
    3028#include "../../common/Common.h"
    31 #include "../../common/WorldMap.h"
    3229#include "../../common/Player.h"
    3330
     
    4542void initWinSock();
    4643void shutdownWinSock();
    47 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId);
    48 void drawMap(WorldMap* gameMap);
    49 void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId);
    50 POSITION screenToMap(POSITION pos);
    51 POSITION mapToScreen(POSITION pos);
     44void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole);
    5245
    5346// callbacks
     
    8073bool doexit;
    8174
     75map<unsigned int, Player> mapPlayers;
     76
    8277Window* wndLogin;
    8378Window* wndMain;
     
    10499   bool redraw = true;
    105100   doexit = false;
    106    map<unsigned int, Player> mapPlayers;
    107    unsigned int curPlayerId = -1;
    108101
    109102   float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
     
    117110   }
    118111
    119    if (al_init_primitives_addon())
    120       cout << "Primitives initialized" << endl;
    121    else
    122       cout << "Primitives not initialized" << endl;
    123 
     112   al_init_primitives_addon();
    124113   al_init_font_addon();
    125114   al_init_ttf_addon();
    126115
    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 
     116   ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
     117 
    133118   if (!font) {
    134119      fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
     
    159144      return -1;
    160145   }
    161 
    162    WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
    163    //delete gameMap;
    164    //gameMap = WorldMap::createDefaultMap();
    165146
    166147   wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
     
    310291         }
    311292      }
    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       }
    333293
    334294      if (receiveMessage(&msgFrom, sock, &from) >= 0)
    335295      {
    336          processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId);
     296         processMessage(msgFrom, state, chatConsole);
    337297         cout << "state: " << state << endl;
    338298      }
     
    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 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)
    440375{
    441376   string response = string(msg.buffer);
     
    448383      {
    449384         cout << "In STATE_START" << endl;
     385
     386         chatConsole.addLine(response);
    450387
    451388         switch(msg.type)
     
    471408                  state = STATE_LOGIN;
    472409                  wndCurrent = wndMain;
    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;
     410                  cout << "User login successful" << endl;
    481411               }
    482 
    483412               break;
    484413            }
     
    489418      case STATE_LOGIN:
    490419      {
    491          switch(msg.type)
     420         chatConsole.addLine(response);
     421
     422          switch(msg.type)
    492423         {
    493424            case MSG_TYPE_REGISTER:
     
    497428            case MSG_TYPE_LOGIN:
    498429            {
    499                chatConsole.addLine(response);
    500 
    501430               if (response.compare("You have successfully logged out.") == 0)
    502431               {
     
    518447               mapPlayers[p.id] = p;
    519448
    520                cout << "Received MSG_TYPE_PLAYER message" << endl;
     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;
    521453
    522454               break;
    523455            }
    524             case MSG_TYPE_CHAT:
    525             {
    526                chatConsole.addLine(response);
    527 
    528                break;
    529             }
    530          }
    531 
     456         }
     457                     
    532458         break;
    533459      }
     
    541467}
    542468
    543 void 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 
    565 void 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 
    584469void registerAccount()
    585470{
Note: See TracChangeset for help on using the changeset viewer.