jpaver / opengametools

A set of open c++ game development tools that are lightweight, easy-to-integrate and free to use. Currently hosting a magicavoxel .vox full scene loader.
MIT License
373 stars 35 forks source link

ogt_vox: implemented endian handling #44

Closed mgerhardy closed 1 year ago

mgerhardy commented 1 year ago

This PR implements the ability to switch the default endianness for ogt_vox from little endian to big endian

For SDL users, this could e.g. look like this:

#include <SDL_endian.h>
#define OGT_VOX_BIGENDIAN_SWAP32  SDL_SwapLE32
#define OGT_VOX_IMPLEMENTATION
#include "external/ogt_vox.h"

Runtime detection example by jpaver:

inline bool is_big_endian()
{
   union {
     uint32_t u32;
     uint8_t  u8[4];
   } u;
   u.u32 = 0x01020304;
   return u.u8[0] == 0x01;
}
inline uint32_t byte_swap_if_big_endian_32(uint32_t x)
{
  return is_big_endian() ? __builtin_swap32(x) : x;
}
#define OGT_VOX_BIGENDIAN_SWAP32(x) byte_swap_if_big_endian_32(x)