diff --git a/src/network/message.cpp b/src/network/message.cpp index 2fe233210..e6b60128e 100644 --- a/src/network/message.cpp +++ b/src/network/message.cpp @@ -143,6 +143,19 @@ short Message::getShort() return ntohs(*(short*)(&m_data[m_pos-sizeof(short)])); } // getShort +// ---------------------------------------------------------------------------- +/** Adds a floating point value to the message. + * \param data Floating point value to add. + */ +void Message::addFloat(const float data) +{ + // The simple approach of using addInt(*(int*)&data) + // does not work (at least with optimisation on certain g++ versions, + // see getFloat for more details) + int n; + memcpy(&n, &data, sizeof(float)); + addInt(n); +} // addFloat // ---------------------------------------------------------------------------- float Message::getFloat() { diff --git a/src/network/message.hpp b/src/network/message.hpp index 39e2ea190..8ace585ea 100644 --- a/src/network/message.hpp +++ b/src/network/message.hpp @@ -61,7 +61,7 @@ public: void addString(const std::string &data); void addStringVector(const std::vector& vs); void addUInt(unsigned int data) { addInt(*(int*)&data); } - void addFloat(float data) { addInt(*(int*)&data); } + void addFloat(const float data); void addBool(bool data) { addChar(data?1:0); } void addChar(char data) { addCharArray((char*)&data,1);} void addCharArray(char *c, unsigned int n=1)