Commit ffd3df34 by Anselme

fixed text encoding issues

parent c34d5253
......@@ -98,7 +98,7 @@ void Client::handlePackets()
case MAP_INIT:
packet >> m_map;
m_graphics->initCrates(m_map.getCrates());
m_graphics->setTurn(0);
m_graphics->setTurn(m_map.getTurn());
break;
case MAP_DIFF:
{
......@@ -111,7 +111,7 @@ void Client::handlePackets()
{
PlayerAdd msg;
packet >> msg;
m_graphics->addPlayer(msg, (m_name.compare(msg.id) == 0));
m_graphics->addPlayer(msg, m_name == msg.id);
break;
}
case PLAYER_REMOVE:
......@@ -130,7 +130,7 @@ void Client::handlePackets()
}
}
void Client::connectToServer(Client* client, const std::string &address, unsigned short port, const std::string &name, unsigned int color)
void Client::connectToServer(Client* client, const std::string &address, unsigned short port, const sf::String &name, unsigned int color)
{
// add an artificial waiting time ?
// sf::sleep(sf::seconds(1));
......
......@@ -2,6 +2,7 @@
#define CLIENT_H
#include <SFML/Network/TcpSocket.hpp>
#include <SFML/System/String.hpp>
#include <common/mapkoban.h>
class Graphics;
......@@ -20,14 +21,14 @@ class Client
std::string m_address;
unsigned short m_port;
std::string m_name;
sf::String m_name;
MapKoban m_map;
bool m_running;
bool m_connected;
bool m_authenticated;
static void connectToServer(Client* client, const std::string &address, unsigned short port, const std::string &name, unsigned int color);
static void connectToServer(Client* client, const std::string &address, unsigned short port, const sf::String &name, unsigned int color);
bool safeReceive(sf::Packet& packet);
void handlePackets();
......
......@@ -81,7 +81,7 @@ void Graphics::addPlayer(const PlayerAdd& msg, bool isMainPlayer)
}
}
void Graphics::removePlayer(const std::string& name)
void Graphics::removePlayer(const sf::String& name)
{
PlayerEntity* player = m_players[name];
if(player != nullptr)
......
......@@ -2,6 +2,7 @@
#define GRAPHICS_H
#include <SFML/System/Vector2.hpp>
#include <SFML/System/String.hpp>
#include <SFML/Graphics/Color.hpp>
#include <common/defines.h>
#include <common/mapdiff.h>
......@@ -16,6 +17,16 @@ class PlayerAdd;
class Crate;
class Menu;
namespace std {
template <> struct hash<sf::String>
{
size_t operator()(const sf::String & str) const
{
return hash<std::string>()(std::string(str));
}
};
}
namespace sf
{
class Texture;
......@@ -40,7 +51,7 @@ class Graphics
std::vector<CrateEntity*> m_crates;
std::unordered_map<std::string, PlayerEntity*> m_players;
std::unordered_map<sf::String, PlayerEntity*> m_players;
PlayerEntity* m_main_player;
unsigned int m_current_turn;
......@@ -61,7 +72,7 @@ public:
void initCrates(const std::vector<Crate> &crates);
void addPlayer(const PlayerAdd& msg, bool isMainPlayer);
void removePlayer(const std::string& name);
void removePlayer(const sf::String& name);
void initMenu(Menu* menu);
......
......@@ -368,7 +368,7 @@ void Menu::textLeft()
--m_textCursor;
}
std::string Menu::getText()
sf::String Menu::getText()
{
return m_textStr;
}
......
......@@ -71,7 +71,7 @@ public:
void textDelete();
void textRight();
void textLeft();
std::string getText();
sf::String getText();
void updateTextSprite(bool isRed = false);
......
......@@ -7,14 +7,14 @@
const int NAME_TAG_SIZE = 12;
PlayerEntity::PlayerEntity(const std::string &name, sf::Color color) :
PlayerEntity::PlayerEntity(const sf::String &name, sf::Color color) :
m_name(name)
{
m_animationOffset = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
m_sprite = SpriteAtlas::createSprite(color);
m_nameTag = new sf::Text(sf::String::fromUtf8(m_name.begin(), m_name.end()), Fonts::getArialFont(), NAME_TAG_SIZE*2);
m_nameTag = new sf::Text(m_name, Fonts::getArialFont(), NAME_TAG_SIZE*2);
m_nameTag->scale(0.5f, 0.5f);
setAction(IDLE);
......
......@@ -2,6 +2,7 @@
#define PLAYERENTITY_H
#include <SFML/System/Vector2.hpp>
#include <SFML/System/String.hpp>
#include <string>
#include <common/defines.h>
......@@ -19,15 +20,15 @@ class PlayerEntity
sf::Text* m_nameTag;
sf::Vector2i m_position;
std::string m_name;
sf::String m_name;
sf::Vector2i m_moveDir;
float m_animationOffset;
public:
PlayerEntity(const std::string & name, sf::Color color);
PlayerEntity(const sf::String & name, sf::Color color);
~PlayerEntity();
const std::string & getName() { return m_name; }
const sf::String & getName() { return m_name; }
const sf::Vector2i& getPosition() { return m_position; }
void setPosition(const sf::Vector2i& pos) { m_position = pos; }
......
......@@ -2,9 +2,10 @@
#define AUTHMESSAGE_H
#include <string>
#include <SFML/System/String.hpp>
struct AuthMessage {
std::string name;
sf::String name;
unsigned int color;
friend sf::Packet& operator <<(sf::Packet& packet, const AuthMessage& auth){
......
......@@ -21,7 +21,7 @@ struct CrateMove{
struct PlayerDiff{
std::string id;
sf::String id;
int dir;
PlayerAction action;
......
......@@ -3,10 +3,11 @@
#include <string>
#include <SFML/System/Vector2.hpp>
#include <SFML/System/String.hpp>
#include <SFML/Network/Packet.hpp>
struct PlayerAdd {
std::string id;
sf::String id;
unsigned int color;
sf::Vector2i pos;
unsigned int turn_id;
......@@ -22,7 +23,7 @@ struct PlayerAdd {
};
struct PlayerRemove {
std::string id;
sf::String id;
friend sf::Packet& operator <<(sf::Packet& packet, const PlayerRemove& p_rm){
return packet << p_rm.id;
}
......
......@@ -3,16 +3,17 @@
#include <string>
#include <SFML/System/Vector2.hpp>
#include <SFML/System/String.hpp>
#include "common/defines.h"
struct ClientData{
std::string id;
sf::String id;
bool authenticated;
unsigned int socket_error_count;
};
struct CharacterData{
std::string id;
sf::String id;
unsigned int color;
sf::Vector2i position;
Direction direction;
......
......@@ -8,7 +8,6 @@
#include "server.h"
#include "sockethandler.h"
MapInstance::MapInstance(Server* s):
m_server(s)
{
......@@ -43,7 +42,7 @@ void MapInstance::update()
}
}
bool MapInstance::isAvailable(std::string id)
bool MapInstance::isAvailable(sf::String id)
{
return (m_characters.find(id) == m_characters.end());
}
......@@ -53,7 +52,7 @@ void MapInstance::addCharacter(CharacterData *charac)
m_characters[charac->id]=charac;
}
void MapInstance::removeCharacter(std::string id)
void MapInstance::removeCharacter(const sf::String& id)
{
m_characters.erase(id);
}
......@@ -95,8 +94,8 @@ void MapInstance::filterMapDiff()
case MOVING:
{
CharacterData* charac = nullptr;
for(std::pair<std::string,CharacterData*> c : m_characters)
if(c.second->id.compare(p_diff.id) == 0)
for(std::pair<sf::String, CharacterData*> c : m_characters)
if(c.second->id == p_diff.id)
charac = m_characters[p_diff.id];
sf::Vector2i target = charac->position + dir;
p_diff.action = resolveMove(charac->position,dir);
......@@ -128,7 +127,7 @@ PlayerAction MapInstance::resolveMove(sf::Vector2i pos, Direction dir)
{
//check if there is a crate on this tile
int crateId = m_map->findCrate(target);
std::string playerId = findPlayer(target);
sf::String playerId = findPlayer(target);
if(crateId != NO_CRATE)
{
sf::Vector2i dest = target+dir;
......@@ -137,8 +136,8 @@ PlayerAction MapInstance::resolveMove(sf::Vector2i pos, Direction dir)
case FLOOR:
{
int targetCrateId = m_map->findCrate(dest);
std::string targetPlayerId = findPlayer(dest);
if(targetCrateId == NO_CRATE && targetPlayerId.empty())
sf::String targetPlayerId = findPlayer(dest);
if(targetCrateId == NO_CRATE && targetPlayerId.isEmpty())
{
m_map->getCrate(crateId).position = dest;
return PUSHING;
......@@ -149,7 +148,7 @@ PlayerAction MapInstance::resolveMove(sf::Vector2i pos, Direction dir)
default:
return BLOCKED;
}
}else if(!playerId.empty())
}else if(!playerId.isEmpty())
{
return BLOCKED;
}
......@@ -164,19 +163,19 @@ PlayerAction MapInstance::resolveMove(sf::Vector2i pos, Direction dir)
return IDLE;
}
std::string MapInstance::findPlayer(sf::Vector2i pos)
sf::String MapInstance::findPlayer(sf::Vector2i pos)
{
for(std::pair<std::string,CharacterData*> c : m_characters){
for(std::pair<sf::String, CharacterData*> c : m_characters){
if(c.second->position == pos)
return c.second->id;
}
return std::string("");
return sf::String("");
}
sf::Packet& MapInstance::packCharacterData(sf::Packet &packet)
{
for(std::pair<std::string,CharacterData*> charac : m_characters){
std::cout << "Packing player " << charac.second->id << std::endl;
for(std::pair<sf::String, CharacterData*> charac : m_characters){
std::cout << "Packing player " << std::string(charac.second->id) << std::endl;
PlayerAdd pa;
pa.id = charac.second->id;
pa.color = charac.second->color;
......
......@@ -2,7 +2,10 @@
#define MAPMANAGER_H
#include <unordered_map>
#include <SFML/System/String.hpp>
#include <mutex>
#include <string>
#include <functional>
#include "common/mapkoban.h"
#include "common/mapdiff.h"
......@@ -10,14 +13,24 @@
class Server;
struct CharacterData;
namespace std {
template <> struct hash<sf::String>
{
size_t operator()(const sf::String & str) const
{
return hash<std::string>()(std::string(str));
}
};
}
class MapInstance
{
Server* m_server;
MapKoban* m_map;
MapDiff m_diff;
std::unordered_map<std::string,CharacterData*> m_characters;
std::unordered_map<sf::String, CharacterData*> m_characters;
unsigned int m_current_turn_id;
std::unordered_map<int,MapDiff*> m_mapdiffs;
std::unordered_map<int, MapDiff*> m_mapdiffs;
std::mutex m_mutex;
public:
MapInstance(Server* s);
......@@ -32,8 +45,8 @@ public:
TileType getTile(sf::Vector2i pos){return m_map->getTile(pos);}
void addCharacter(CharacterData* charac);
void removeCharacter(std::string id);
CharacterData* getCharacter(std::string id){return m_characters[id];}
void removeCharacter(const sf::String &id);
CharacterData* getCharacter(const sf::String &id){ return m_characters[id]; }
sf::Packet& packCharacterData(sf::Packet &packet);
void pushPlayerAction(PlayerDiff p_diff);
......@@ -42,8 +55,8 @@ public:
unsigned int getCurrentTurn(){return m_current_turn_id;}
void filterMapDiff();
PlayerAction resolveMove(sf::Vector2i pos, Direction dir);
std::string findPlayer(sf::Vector2i pos);
bool isAvailable(std::string);
sf::String findPlayer(sf::Vector2i pos);
bool isAvailable(sf::String);
};
#endif // MAPMANAGER_H
......@@ -18,7 +18,7 @@ class Server
SocketHandler* m_socket_handler;
sf::Clock m_clock;
// std::set<std::string> m_id_list; // m
// std::set<sf::String> m_id_list; // m
// MapKoban m_map; //m
MapDiff m_diff; //m
//std::mutex m_mutex; // m?
......@@ -39,7 +39,7 @@ public:
// PlayerAction resolveMove(sf::Vector2i pos, Direction dir);
// void stackAction(PlayerDiff p);
void initMap();
// std::string findPlayer(sf::Vector2i pos);
// sf::String findPlayer(sf::Vector2i pos);
// void filterMapDiff();
// void removeDisconnectedPlayers();
};
......
......@@ -108,7 +108,7 @@ void SocketHandler::handleSocketMessage(sf::TcpSocket& socket)
new_charac->position = sf::Vector2i(std::rand()%map->getWidth(), std::rand()%map->getHeight());
}while(map->getTile(new_charac->position) != FLOOR);
m_server->getMap()->addCharacter(new_charac);
std::cout << "Spawning new player \"" << new_charac->id << "\" at position (" << new_charac->position.x << "," << new_charac->position.y << ")" << std::endl;
std::cout << "Spawning new player \"" << std::string(new_charac->id) << "\" at position (" << new_charac->position.x << "," << new_charac->position.y << ")" << std::endl;
}
else
auth_answer.isOk = false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment