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
375 stars 36 forks source link

ogt_vox: implemented endian handling #44

Closed mgerhardy closed 2 years ago

mgerhardy commented 2 years 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)