Game Engine Architecture

(Ben Green) #1

106 3. Fundamentals of Software Engineering for Games


z Litt le-endian. If a microprocessor stores the least signifi cant byte (LSB) of
a multi-byte value at a lower memory address than the most signifi cant
byte (MSB), we say that the processor is litt le-endian. On a litt le-endian
machine, the number 0xABCD1234 would be stored in memory using
the consecutive bytes 0x34, 0x12, 0xCD, 0xAB.
z Big-endian. If a microprocessor stores the most signifi cant byte (MSB) of
a multi-byte value at a lower memory address than the least signifi cant
byte (LSB), we say that the processor is big-endian. On a big-endian ma-
chine, the number 0xABCD1234 would be stored in memory using the
bytes 0xAB, 0xCD, 0x12, 0x34.
Most programmers don’t need to think much about endianness. How-
ever, when you’re a game programmer, endianness can become a bit of a thorn
in your side. This is because games are usually developed on a PC or Linux ma-
chine running an Intel Pentium processor (which is litt le-endian), but run on
a console such as the Wii, Xbox 360, or PLAYSTATION 3—all three of which
utilize a variant of the PowerPC processor (which can be confi gured to use
either endianness, but is big-endian by default). Now imagine what happens
when you generate a data fi le for consumption by your game engine on an
Intel processor and then try to load that data fi le into your engine running on
a PowerPC processor. Any multi-byte value that you wrote out into that data
fi le will be stored in litt le-endian format. But when the game engine reads the
fi le, it expects all of its data to be in big-endian format. The result? You’ll write
0xABCD1234, but you’ll read 0x3412CDAB, and that’s clearly not what you
intended!
There are at least two solutions to this problem.


  1. You could write all your data fi les as text and store all multi-byte num-
    bers as sequences of decimal digits, one character (one byte) per digit.
    This would be an ineffi cient use of disk space, but it would work.


U8* pBytes = ( U8*)&value;U32 value = 0 xABCD1234;
Big-endian

0xAB
0xCD

pBytes + 0x0

0x12
0x34

pBytes + 0x1
pBytes + 0x2
pBytes + 0x3

Little-endian

0x34
0x12
0xCD
0xAB

pBytes + 0x0
pBytes + 0x1
pBytes + 0x2
pBytes + 0x3
Figure 3.6. Big- and little-endian representations of the value 0xABCD1234.
Free download pdf