It would be nice to make some SFML classes like sf::Vector, sf::Rect, sf::Color, sf::Vertex, sf::VertexArray, ..., serializable.
It's sometimes paintfull to send complex entities throught the network or to save them into a file. (Especially when entities have a lot of objects and vectors)
template <typename Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & position.x;
ar & position.y;
ar & position.z;
ar & color.r;
ar & color.g;
ar & color.b;
ar & color.a;
ar & texCoords.x;
ar & texCoords.y;
}
Ok this code is good but I have to change everything because SFML classes are not serializable so :
class ODFAEG_GRAPHICS_API Material {
friend class boost::serialization::access;
private :
struct TextureInfo {
private :
const Texture* texture;
sf::IntRect rect;
std::string texId;
public :
TextureInfo (const Texture* texture, sf::IntRect rect, std::string texId="") {
this->texture = texture;
this->rect = rect;
this->texId = texId;
}
void setTexId(std::string texId) {
this->texId = texId;
}
std::string getTexId() {
return texId;
}
bool operator== (TextureInfo& info) {
return texture == info.texture;
}
bool operator!= (TextureInfo& info) {
return texture != info.texture;
}
const Texture* getTexture() const {
return texture;
}
sf::IntRect getTexRect() {
return rect;
}
template <typename Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & texInfos;
ar & color.r;
ar & color.g;
ar & color.b;
ar & color.a;
}
};
std::vector<TextureInfo*> texInfos;
sf::Color color;
public :
int getNbTextures () {
return texInfos.size();
}
void addTexture (const Texture* texture, sf::IntRect rect) {
texInfos.push_back(new TextureInfo(texture, rect));
}
sf::IntRect getTexRect(int texUnit = 0) {
return (texInfos.size() > 0) ? texInfos[texUnit]->getTexRect() : sf::IntRect(0, 0, 0, 0);
}
const Texture* getTexture(int texUnit = 0) {
return (texInfos.size() > 0) ? texInfos[texUnit]->getTexture() : nullptr;
}
std::string getTexId(int texUnit = 0) {
return (texInfos.size() > 0) ? texInfos[texUnit]->getTexId() : nullptr;
}
void setTexId(std::string texId, int texUnit = 0) {
if (texInfos.size() > 0) {
texInfos[texUnit]->setTexId(texId);
}
}
bool useSameTextures (Material& material) {
if (texInfos.size() != material.texInfos.size())
return false;
for (unsigned int i = 0; i < texInfos.size(); i++) {
if (*texInfos[i] != *material.texInfos[i])
return false;
}
return true;
}
bool hasSameColor (Material& material) {
return color == material.color;
}
bool operator== (Material& material) {
return useSameTextures(material) && hasSameColor(material);
}
bool operator!= (Material& material) {
return !useSameTextures(material) || !hasSameColor(material);
}
};
It'll not work because IntRect is not serializable.
For the texture it doesn't matter because I can't send the pointer to the texture directly, but, I send a string instead which tells me which texture to use.
I can make the sf::Texture class serializable and send the image throught the network but I don't thing that's a good idea.