Use internal endian tool
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "asset/assettype.h"
|
||||
#include "assert/assert.h"
|
||||
#include "display/texture.h"
|
||||
#include "util/endian.h"
|
||||
|
||||
errorret_t assetTextureLoad(assetentire_t entire) {
|
||||
assertNotNull(entire.data, "Data pointer cannot be NULL.");
|
||||
@@ -32,8 +33,8 @@ errorret_t assetTextureLoad(assetentire_t entire) {
|
||||
}
|
||||
|
||||
// Fix endian
|
||||
assetData->width = le32toh(assetData->width);
|
||||
assetData->height = le32toh(assetData->height);
|
||||
assetData->width = endianLittleToHost32(assetData->width);
|
||||
assetData->height = endianLittleToHost32(assetData->height);
|
||||
|
||||
// Check dimensions.
|
||||
if(
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "assert/assert.h"
|
||||
#include "display/tileset/tileset.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/endian.h"
|
||||
|
||||
errorret_t assetTilesetLoad(assetentire_t entire) {
|
||||
assertNotNull(entire.data, "Asset data cannot be null");
|
||||
@@ -30,12 +31,12 @@ errorret_t assetTilesetLoad(assetentire_t entire) {
|
||||
}
|
||||
|
||||
// Fix endianness
|
||||
tilesetData->tileWidth = le16toh(tilesetData->tileWidth);
|
||||
tilesetData->tileHeight = le16toh(tilesetData->tileHeight);
|
||||
tilesetData->columnCount = le16toh(tilesetData->columnCount);
|
||||
tilesetData->rowCount = le16toh(tilesetData->rowCount);
|
||||
tilesetData->right = le16toh(tilesetData->right);
|
||||
tilesetData->bottom = le16toh(tilesetData->bottom);
|
||||
tilesetData->tileWidth = endianLittleToHost16(tilesetData->tileWidth);
|
||||
tilesetData->tileHeight = endianLittleToHost16(tilesetData->tileHeight);
|
||||
tilesetData->columnCount = endianLittleToHost16(tilesetData->columnCount);
|
||||
tilesetData->rowCount = endianLittleToHost16(tilesetData->rowCount);
|
||||
tilesetData->right = endianLittleToHost16(tilesetData->right);
|
||||
tilesetData->bottom = endianLittleToHost16(tilesetData->bottom);
|
||||
|
||||
if(tilesetData->tileWidth == 0) {
|
||||
errorThrow("Tile width cannot be 0");
|
||||
@@ -49,19 +50,9 @@ errorret_t assetTilesetLoad(assetentire_t entire) {
|
||||
if(tilesetData->rowCount == 0) {
|
||||
errorThrow("Row count cannot be 0");
|
||||
}
|
||||
|
||||
uint32_t temp;
|
||||
memoryCopy(&temp, &tilesetData->u0, sizeof(float_t));
|
||||
temp = le32toh(temp);
|
||||
memoryCopy(&tilesetData->u0, &temp, sizeof(float_t));
|
||||
|
||||
if(tilesetData->u0 < 0.0f || tilesetData->u0 > 1.0f) {
|
||||
errorThrow("Invalid u0 value in tileset");
|
||||
}
|
||||
|
||||
memoryCopy(&temp, &tilesetData->v0, sizeof(float_t));
|
||||
temp = le32toh(temp);
|
||||
memoryCopy(&tilesetData->v0, &temp, sizeof(float_t));
|
||||
tilesetData->u0 = endianLittleToHostFloat(tilesetData->u0);
|
||||
tilesetData->v0 = endianLittleToHostFloat(tilesetData->v0);
|
||||
|
||||
if(tilesetData->v0 < 0.0f || tilesetData->v0 > 1.0f) {
|
||||
errorThrow("Invalid v0 value in tileset");
|
||||
|
||||
@@ -33,11 +33,6 @@
|
||||
#include <ogcsys.h>
|
||||
#include <gccore.h>
|
||||
#include <malloc.h>
|
||||
#include <sys/endian.h>
|
||||
#else
|
||||
#ifndef le32toh
|
||||
#define le32toh(x) (x)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef bool bool_t;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
array.c
|
||||
endian.c
|
||||
memory.c
|
||||
string.c
|
||||
math.c
|
||||
|
||||
67
src/util/endian.c
Normal file
67
src/util/endian.c
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "util/endian.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
bool_t isHostLittleEndian(void) {
|
||||
uint32_t value = ENDIAN_MAGIC;
|
||||
uint8_t *bytePtr = (uint8_t *)&value;
|
||||
return bytePtr[0] == 0x04;
|
||||
}
|
||||
|
||||
uint32_t endianLittleToHost32(uint32_t value) {
|
||||
if(isHostLittleEndian()) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return (
|
||||
((value & 0x000000FF) << 24) |
|
||||
((value & 0x0000FF00) << 8) |
|
||||
((value & 0x00FF0000) >> 8) |
|
||||
((value & 0xFF000000) >> 24)
|
||||
);
|
||||
}
|
||||
|
||||
uint16_t endianLittleToHost16(uint16_t value) {
|
||||
if(isHostLittleEndian()) {
|
||||
return value;
|
||||
}
|
||||
return (uint16_t)(((value & 0x00FF) << 8) |
|
||||
((value & 0xFF00) >> 8));
|
||||
}
|
||||
|
||||
uint64_t endianLittleToHost64(uint64_t value) {
|
||||
if(isHostLittleEndian()) {
|
||||
return value;
|
||||
}
|
||||
return (
|
||||
((value & 0x00000000000000FFULL) << 56) |
|
||||
((value & 0x000000000000FF00ULL) << 40) |
|
||||
((value & 0x0000000000FF0000ULL) << 24) |
|
||||
((value & 0x00000000FF000000ULL) << 8) |
|
||||
((value & 0x000000FF00000000ULL) >> 8) |
|
||||
((value & 0x0000FF0000000000ULL) >> 24) |
|
||||
((value & 0x00FF000000000000ULL) >> 40) |
|
||||
((value & 0xFF00000000000000ULL) >> 56)
|
||||
);
|
||||
}
|
||||
|
||||
float_t endianLittleToHostFloat(float_t value) {
|
||||
if(isHostLittleEndian()) {
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32_t temp;
|
||||
float_t result;
|
||||
memoryCopy(&temp, &value, sizeof(uint32_t));
|
||||
temp = endianLittleToHost32(temp);
|
||||
memoryCopy(&result, &temp, sizeof(uint32_t));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
48
src/util/endian.h
Normal file
48
src/util/endian.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
static const uint32_t ENDIAN_MAGIC = 0x01020304;
|
||||
|
||||
/**
|
||||
* Checks if the host system is little-endian.
|
||||
*
|
||||
* @return true if the host is little-endian, false otherwise.
|
||||
*/
|
||||
bool_t isHostLittleEndian(void);
|
||||
|
||||
/**
|
||||
* Converts a 32-bit integer from little-endian to host byte order.
|
||||
*
|
||||
* @param value The little-endian value to convert.
|
||||
* @return The value in host byte order.
|
||||
*/
|
||||
uint32_t endianLittleToHost32(uint32_t value);
|
||||
|
||||
/**
|
||||
* Converts a 16-bit integer from little-endian to host byte order.
|
||||
* @param value The little-endian value to convert.
|
||||
* @return The value in host byte order.
|
||||
*/
|
||||
uint16_t endianLittleToHost16(uint16_t value);
|
||||
|
||||
/**
|
||||
* Converts a 64-bit integer from little-endian to host byte order.
|
||||
* @param value The little-endian value to convert.
|
||||
* @return The value in host byte order.
|
||||
*/
|
||||
uint64_t endianLittleToHost64(uint64_t value);
|
||||
|
||||
/**
|
||||
* Converts a float from little-endian to host byte order.
|
||||
*
|
||||
* @param value The little-endian value to convert.
|
||||
* @return The value in host byte order.
|
||||
*/
|
||||
float_t endianLittleToHostFloat(float_t value);
|
||||
Reference in New Issue
Block a user