Half a year ago I decided to develop a sequel to my successful single-player game. My game was made in an existing game engine. This time I decided to create my own game engine with c++ 17 and directx 11 in visual studio 2019, because I have far too much free time nowadays and want to deal with the technical stuff that is otherwise hidden in existing game engines. I have almost implemented everything that I need. I need to store geometry data and so forth on disk in binary format so that my game can read these files when the game is shipped. The game also needs to be able to read/write binary save data. I am currently using text files for reading and writing... If I compile my win32 application on my intel cpu on windows 10 and ship my game on steam store, do my game need to be endian aware for binary files? My game will obviously require Windows and x86 CPU (Intel, AMD). I know that x86 CPU's use little-endian byte ordering.
Example code
static_assert(CHAR_BIT * sizeof(std::uint8_t) == 8);
std::wstring filename = L"MyBinaryFile.dat";
std::uint32_t dataOut = ...;
//My computer writes the data.
std::ofstream out(filename.c_str(), std::ios::out | std::ios::binary);
out.write(reinterpret_cast<char*>(&dataOut), sizeof(std::uint32_t));
out.close();
/**********************************************************************/
//Different computers running my game and reads the data.
std::uint32_t dataIn;
std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary);
in.read(reinterpret_cast<char*>(&dataIn), sizeof(std::uint32_t));
in.close();
Is this safe for my game?
Or should I do it like this? is this safer?
static_assert(CHAR_BIT * sizeof(std::uint8_t) == 8);
std::wstring filename = L"MyBinaryFile.dat";
std::uint32_t dataOut = ...;
//My computer writes the data.
std::uint8_t bufferOut[4];
bufferOut[0] = static_cast<std::uint8_t>((dataOut & 0xff000000) >> 24);
bufferOut[1] = static_cast<std::uint8_t>((dataOut & 0x00ff0000) >> 16);
bufferOut[2] = static_cast<std::uint8_t>((dataOut & 0x0000ff00) >> 8);
bufferOut[3] = static_cast<std::uint8_t>((dataOut & 0x000000ff));
std::ofstream out(filename.c_str(), std::ios::out | std::ios::binary);
out.write(reinterpret_cast<char*>(bufferOut), 4 * sizeof(std::uint8_t));
out.close();
/**********************************************************************/
//Different computers running my game and reads the data.
std::uint8_t bufferIn[4];
std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary);
in.read(reinterpret_cast<char*>(bufferIn), 4 * sizeof(std::uint8_t));
in.close();
std::uint32_t dataIn;
dataIn = static_cast<std::uint32_t>(bufferIn[0]) << 24;
dataIn |= static_cast<std::uint32_t>(bufferIn[1]) << 16;
dataIn |= static_cast<std::uint32_t>(bufferIn[2]) << 8;
dataIn |= static_cast<std::uint32_t>(bufferIn[3]);