Changeset 88cdae2 in network-game for client


Ignore:
Timestamp:
Jan 1, 2013, 12:58:18 AM (12 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
master
Children:
80b3f94
Parents:
594d2e9
Message:

The user can now move around the screen by clicking once they're logged in. A new message of type MSG_TYPE_PLAYER_MOVE is sent to the server.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r594d2e9 r88cdae2  
    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"
    29 #include "../../common/Player.h"
     31#include "../../common/Player.h"_
    3032
    3133#include "Window.h"
     
    4244void initWinSock();
    4345void shutdownWinSock();
    44 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers);
     46void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId);
     47void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId);
    4548
    4649// callbacks
     
    98101   doexit = false;
    99102   map<unsigned int, Player> mapPlayers;
     103   unsigned int curPlayerId = -1;
    100104
    101105   float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
     
    109113   }
    110114
    111    al_init_primitives_addon();
     115   if (al_init_primitives_addon())
     116      cout << "Primitives initialized" << endl;
     117   else
     118      cout << "Primitives not initialized" << endl;
     119
    112120   al_init_font_addon();
    113121   al_init_ttf_addon();
    114122
    115    ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
    116  
     123   #if defined WINDOWS
     124      ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
     125   #elif defined LINUX
     126      ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
     127   #endif
     128
    117129   if (!font) {
    118130      fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
     
    290302         }
    291303      }
     304      else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
     305         mapPlayers[curPlayerId].pos.x = ev.mouse.x;
     306         mapPlayers[curPlayerId].pos.y = ev.mouse.y;
     307
     308         // send the server a MSG_TYPE_PLAYER_MOVE message
     309         msgTo.type = MSG_TYPE_PLAYER_MOVE;
     310
     311         ostringstream oss;
     312         oss << ev.mouse.x;
     313         oss << ev.mouse.y;
     314
     315         memcpy(msgTo.buffer, oss.str().c_str(), oss.str().length());
     316         sendMessage(&msgTo, sock, &server);
     317      }
    292318
    293319      if (receiveMessage(&msgFrom, sock, &from) >= 0)
    294320      {
    295          processMessage(msgFrom, state, chatConsole, mapPlayers);
     321         processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId);
    296322         cout << "state: " << state << endl;
    297323      }
     
    300326      {
    301327         redraw = false;
    302  
     328
    303329         wndCurrent->draw(display);
    304  
    305          al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
     330
     331         drawPlayers(mapPlayers, curPlayerId);
    306332
    307333         chatConsole.draw(font, al_map_rgb(255,255,255));
     
    371397}
    372398
    373 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers)
     399void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId)
    374400{
    375401   string response = string(msg.buffer);
     
    382408      {
    383409         cout << "In STATE_START" << endl;
    384 
    385          chatConsole.addLine(response);
    386410
    387411         switch(msg.type)
     
    407431                  state = STATE_LOGIN;
    408432                  wndCurrent = wndMain;
    409                   cout << "User login successful" << endl;
     433                 
     434                  Player p("", "");
     435                  p.deserialize(msg.buffer);
     436                  mapPlayers[p.id] = p;
     437                  curPlayerId = p.id;
     438
     439                  cout << "Got a valid login response with the player" << endl;
     440                  cout << "Player id: " << curPlayerId << endl;
    410441               }
     442
    411443               break;
    412444            }
     
    444476               Player p("", "");
    445477               p.deserialize(msg.buffer);
    446 
    447                cout << "p.id: " << p.id << endl;
    448                cout << "p.name: " << p.name << endl;
    449                cout << "p.pos.x: " << p.pos.x << endl;
    450                cout << "p.pos.y: " << p.pos.y << endl;
    451 
    452478               mapPlayers[p.id] = p;
     479
     480               cout << "Received MSG_TYPE_PLAYER message" << endl;
    453481
    454482               break;
     
    473501}
    474502
     503void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId)
     504{
     505   map<unsigned int, Player>::iterator it;
     506
     507   for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
     508   {
     509      Player *p = &it->second;
     510
     511      if (p->id == curPlayerId)
     512         al_draw_filled_circle(p->pos.x, p->pos.y, 15, al_map_rgb(0, 255, 0));
     513      else
     514         al_draw_filled_circle(p->pos.x, p->pos.y, 30, al_map_rgb(255, 0, 0));
     515   }
     516}
     517
    475518void registerAccount()
    476519{
Note: See TracChangeset for help on using the changeset viewer.