Do we have to worry about byte orders in the process of programming a game? Do game consoles use different byte orders?
-
1\$\begingroup\$ Are you concerned about a specific project you are working on right now or just in general? Mostly, if you are writing a game for a console, you will likely be using some well documented set of tools like XNA and input and output will be covered in its document. \$\endgroup\$– AturSamsSep 26, 2012 at 12:06
-
4\$\begingroup\$ If you are worried about this I really recommend Rob Pike's The byte order fallacy - trying to worry about byte order often gets you into more trouble than just ignoring it and writing truly endian-independent code. \$\endgroup\$– user744Sep 26, 2012 at 16:31
-
1\$\begingroup\$ It's a good read, but Rob is showing that you can get simplified reads at the expense of needing more complex writes. If you're not the one doing the writes, great! \$\endgroup\$– KylotanSep 26, 2012 at 19:35
4 Answers
If you're writing networking code, this often is something you have to deal with, yes.
Also it's possible that the byte ordering in a file format is not what your platform uses, so sometimes it matters there too.
Do you have to worry about it? Likely not. A vast majority of game programming is going to be at such a level that the endianness is abstracted away. Even in networking, you're almost certainly going to be using a library for networking protocols. It's good that you're aware of it, but I doubt you'd run into an issue with it.
Most of the time, no.
Endianess is usually abstracted away in the high level modules of a game engine, and you don't have to worry about it on a daily basis. If it isn't abstracted, then the engine has a serious issue and should be fixed, because this isn't the kind of details you should be worrying about when making a game.
However if you're working on some low-level parts of a C/C++ multi-platform engine, you might have to deal with it. All three current-gen consoles use a PowerPC architecture, which is big-endian, whereas the x86 architecture used on PC is little-endian. So if you're working on some code that reads raw bytes from somewhere to put them in data structures (binary serialization, networking...), yes you will have to deal with it.
For instance, in C/C++, it's common to see this kind of byte swapping in action (not tested, welcoming corrections):
// Assume bytes comes ordered as big-endian
u16 u16FromBytes(void* data)
{
#if BIG_ENDIAN
return (u16(data[0]) << 8) + u16(data[1]);
#else // LITTLE_ENDIAN
return (u16(data[1]) << 8) + u16(data[0]);
#endif
}
Once again, this is OK in low-level code, but this shouldn't be something used everywhere.
It depends on the platforms you're targetting. For example, I believe that the PS3 is big-endien so if that's one of your targets then it's something you need to be aware of, yes.
In the home computer space the "Big 3" (Windows, Linux, Mac) are all exclusively, or all but excelusively, on Intel x86/x64 architectures these days, so endienness concerns are no longer relevant.
-
\$\begingroup\$ No longer relevant? In my experience (2-3 games across a couple companies and 7 years, both MMOs... so anecdotal rather than statistically valid) each of the PS and Xbox markets are about double the size of the windows market, Macs were a small fraction of windows, and Linux was noticeably smaller than that. Unless your game simply cannot work on a console, you're leaving 80% of your profit on the ground by sticking to PCs. Cross-endian code also means your servers can be on completely different machines than your clients (ex: every client-server console game ever). \$\endgroup\$ Nov 21, 2019 at 13:29
-
\$\begingroup\$ @MarkStorer - I think you need to re-read what I actually wrote. \$\endgroup\$ Nov 22, 2019 at 9:03