tags:

views:

228

answers:

7

I have seen countless references about endianness and what it means. I got no problems about that... However, my coding project is a simple game to run on linux and windows, on standard "gamer" hardware. Do I need to worry about endianness in this case? When should I need to worry about it? My code is simple C and SDL+GL, the only complex data are basic media files (png+wav+xm) and the game data is mostly strings, integer booleans (for flags and such) and static-sized arrays. So far no user has had issues, so I am wondering if adding checks is necessary (will be done later, but there are more urgent issues IMO).

+1  A: 

You only need to worry about it if your game needs to run on different hardware architectures. If you are positive that it will always run on Intel hardware then you can forget about it. If it will run on Linux though many people use different architectures than Intel and you may end up having to think about it.

Simon
Additionally it's only important when you exchange data between computers or when you store data in binary format.
Georg
@Georg Ah good point. I had forgotten to mention that.
Simon
or when you transfer data serially and you must choose which byte to send first.
JustJeff
+1  A: 

Are you distributing you game in source code form?

Because if you are distributing you game as a binary only, then you know exactly which processor families your game will run on. Also, the media files, are they user generated (possibly via a level editor) or are they really only ment to be supplied by yourself?

If this is a truly closed environment (your distribute binaries and the game assets are not intended to be customized) then you know your own risks to endians and I personally wouldn't fool with it.

However, if you are either distributing source and/or hoping people will customize their game, then you have a potential for concern. However, with most of the desktop/laptop computers around these days moving to x86 I would think this is a diminishing concern.

Jason Whitehorn
I have the data files "just laying out there" intentionally for users to take/edit/modify/add stuff. Levels use an editor but the output is a seed and a long string again (I kinda got used to them). The rest is procedural.Admittedly the version most users will touch will be the win32 one, which will be precompiled at distribution time.(as anecdote only I have used the linux or source builds)Thank you for your answer.
GAKOKO
+1  A: 

If you mean a PC by "standard gamer hardware", then you don't have to worry about endianness as it will always be little endian on x86/x64. But if you want to port the project to other architectures, then you should design it endianness-independently.

AndiDog
This is glossing over the most important area of endian issues: Network programming. Always convert between host and network byte order when sending multi-byte values (like int or long).
gnud
Good point, you must use `hton` and similar functions when programming sockets. But you would usually use libraries that abstract away these differences.
AndiDog
@gnud: no, that's wrong. If you expect that all of your servers and clients will be little-endian, it's convenient to define a format or protocol including little-endian multi-byte values. This happens every time a ZIP file is sent over the network, since magic numbers and sizes in PKZIP are little-endian, not network order. It hasn't crashed yet. If you ever need a big-endian client or server, you can safely byte-swap as necessary. `hton` is needed for specifying IP addresses and ports, and is one convenient way to specify endian-safe data formats, but it is not the only way.
Steve Jessop
Yes, I should have said 'unless the wire format defines endianess'.
gnud
A: 

The problem occurs with networking and how the data is sent and when you are doing bit fiddling on different processors since different processors may store the data differently in memory.

Rob
+1  A: 

Whenever you recieve/transmit data from a network, remeber to convert to/from network and host byte order. The C functions htons, htonl etc, or equivalients in your language, should be used here.

Whenever you read multi-byte values (like UTF-16 characters or 32 bit ints) from a file, since that file might have originated on a system with different endianness. If the file is UTF 16 or 32 it probably has a BOM (byte-order mark). Otherwise, the file format will have to specify endianness in some way.

gnud
A: 

I believe Power PC has the opposite endianness of the Intel boards. Might be able to have a routine that sets the endianness dependant on the architecture? I'm not sure if you can actually tell what the hardware architecture is in code...maybe someone smarter then me does know the answer to that question.

Now in reference to your statement "standard" Gamer H/W, I would say typically you're going to look at Consumer Off the Shelf solutions are really what most any Standard Gamer is using, so you're almost going to for sure get the same endian across the board. I'm sure someone will disagree with me but that's my $.02

Ha...I just noticed on the right there is a link that is showing up related to the suggestion I had above.

Find Endianness through a c program

onaclov2000
+3  A: 

The times when you need to worry about endianess:

  • you are sending binary data between machines or processes (using a network or file). If the machines may have different byte order or the protocol used specifies a particular byte order (which it should), you'll need to deal with endianess.
  • you have code that access memory though pointers of different types (say you access a unsigned int variable through a char*).

If you do these things you're dealing with byte order whether you know it or not - it might be that you're dealing with it by assuming it's one way or the other, which may work fine as long as your code doesn't have to deal with a different platform.

In a similar vein, you generally need to deal with alignment issues in those same cases and for similar reasons. Once again, you might be dealing with it by doing nothing and having everything work fine because you don't have to cross platform boundaries (which may come back to bite you down the road if that does become a requirement).

Michael Burr