senoutouya / recastnavigation

Automatically exported from code.google.com/p/recastnavigation
zlib License
0 stars 0 forks source link

Bit-fields are not portable #123

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I discovered that the dtPoly::area value was not correct on Xbox 360 or 
Playstation 3. It seems that bit fields are not portable between platforms of 
different endianness. From what I've read different compilers can implement it 
differently.

http://stackoverflow.com/questions/864077/why-does-an-8-bit-field-have-endiannes
s
http://www.naic.edu/~phil/notes/bitfieldStorage.html

This fixed it for me for MSVC on Xbox 360 (_M_PPCBE defined) and GCC on 
Playstation 3 (__BIG_ENDIAN__ defined). I haven't tested SNC on Playstation 3.

// Structure describing the navigation polygon data.
struct dtPoly
{
    unsigned int firstLink;                     // Index to first link in linked list. 
    unsigned short verts[DT_VERTS_PER_POLYGON]; // Indices to vertices of the poly.
    unsigned short neis[DT_VERTS_PER_POLYGON];  // Refs to neighbours of the poly.
    unsigned short flags;                       // Flags (see dtPolyFlags).
    unsigned char vertCount;                    // Number of vertices.
#if defined(_M_PPCBE) || defined(__BIG_ENDIAN__)
    unsigned char type : 2;                     // Polygon type, see dtPolyTypes.
    unsigned char area : 6;                     // Area ID of the polygon.
#else
    unsigned char area : 6;                     // Area ID of the polygon.
    unsigned char type : 2;                     // Polygon type, see dtPolyTypes.
#endif
};

A lot of what I've read has recommended not using bit fields and use bit 
masking and shifts instead. The above seems to work for me though.

Original issue reported on code.google.com by cameron....@gmail.com on 8 Sep 2010 at 4:17

GoogleCodeExporter commented 9 years ago
Fixed in R218.

I changed the area and type to manual packing. I would have liked to add 
another uchar to the mix for clarity, but currently the polygon sizeof() is 
nicely 32bytes so I resorted to not so nice implementation instead.

Original comment by memono...@gmail.com on 17 Sep 2010 at 7:27