I’m sending information from my server to clients and back again using packed structs (obviously there are a lot more data in the structs)
#pragma pack(push, 1)
struct other_data_struct
{
int hp;
int wp;
char movetype;
}
struct PlayerStats
{
int playerID;
other_data_struct data;
bool friendly; //<-this one messes up how the others are going to be packed on the 2 systems
}
#pragma pack(pop)
That works fine for all fixed sized variables, ints and chars and even other structs.
The boolean doesn’t work well though when the server is compiled with gcc for Linux and the client is compiled with MSVC for windows…
I have thought of making some sort of container (ie. a unsigned char with 8 boolean get/set functions or similar) but it seems as quirky as inelegant.
Is there some way to ‘pack’ structs containing boolean variables exactly the same on Windows and Linux or should I bite the sour apple and use a char for each boolean?
You could try bitfields, that way your packed struct contains ‘int’, but your code uses a more space-efficient representation.
I believe bitfields have some awful performance characteristics, so they may not be the way to go if you care more about access time than space usage.