Use internal endian tool
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
#include "asset/assettype.h"
|
#include "asset/assettype.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "display/texture.h"
|
#include "display/texture.h"
|
||||||
|
#include "util/endian.h"
|
||||||
|
|
||||||
errorret_t assetTextureLoad(assetentire_t entire) {
|
errorret_t assetTextureLoad(assetentire_t entire) {
|
||||||
assertNotNull(entire.data, "Data pointer cannot be NULL.");
|
assertNotNull(entire.data, "Data pointer cannot be NULL.");
|
||||||
@@ -32,8 +33,8 @@ errorret_t assetTextureLoad(assetentire_t entire) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fix endian
|
// Fix endian
|
||||||
assetData->width = le32toh(assetData->width);
|
assetData->width = endianLittleToHost32(assetData->width);
|
||||||
assetData->height = le32toh(assetData->height);
|
assetData->height = endianLittleToHost32(assetData->height);
|
||||||
|
|
||||||
// Check dimensions.
|
// Check dimensions.
|
||||||
if(
|
if(
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
#include "display/tileset/tileset.h"
|
#include "display/tileset/tileset.h"
|
||||||
#include "util/memory.h"
|
#include "util/memory.h"
|
||||||
|
#include "util/endian.h"
|
||||||
|
|
||||||
errorret_t assetTilesetLoad(assetentire_t entire) {
|
errorret_t assetTilesetLoad(assetentire_t entire) {
|
||||||
assertNotNull(entire.data, "Asset data cannot be null");
|
assertNotNull(entire.data, "Asset data cannot be null");
|
||||||
@@ -30,12 +31,12 @@ errorret_t assetTilesetLoad(assetentire_t entire) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fix endianness
|
// Fix endianness
|
||||||
tilesetData->tileWidth = le16toh(tilesetData->tileWidth);
|
tilesetData->tileWidth = endianLittleToHost16(tilesetData->tileWidth);
|
||||||
tilesetData->tileHeight = le16toh(tilesetData->tileHeight);
|
tilesetData->tileHeight = endianLittleToHost16(tilesetData->tileHeight);
|
||||||
tilesetData->columnCount = le16toh(tilesetData->columnCount);
|
tilesetData->columnCount = endianLittleToHost16(tilesetData->columnCount);
|
||||||
tilesetData->rowCount = le16toh(tilesetData->rowCount);
|
tilesetData->rowCount = endianLittleToHost16(tilesetData->rowCount);
|
||||||
tilesetData->right = le16toh(tilesetData->right);
|
tilesetData->right = endianLittleToHost16(tilesetData->right);
|
||||||
tilesetData->bottom = le16toh(tilesetData->bottom);
|
tilesetData->bottom = endianLittleToHost16(tilesetData->bottom);
|
||||||
|
|
||||||
if(tilesetData->tileWidth == 0) {
|
if(tilesetData->tileWidth == 0) {
|
||||||
errorThrow("Tile width cannot be 0");
|
errorThrow("Tile width cannot be 0");
|
||||||
@@ -49,19 +50,9 @@ errorret_t assetTilesetLoad(assetentire_t entire) {
|
|||||||
if(tilesetData->rowCount == 0) {
|
if(tilesetData->rowCount == 0) {
|
||||||
errorThrow("Row count cannot be 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) {
|
tilesetData->u0 = endianLittleToHostFloat(tilesetData->u0);
|
||||||
errorThrow("Invalid u0 value in tileset");
|
tilesetData->v0 = endianLittleToHostFloat(tilesetData->v0);
|
||||||
}
|
|
||||||
|
|
||||||
memoryCopy(&temp, &tilesetData->v0, sizeof(float_t));
|
|
||||||
temp = le32toh(temp);
|
|
||||||
memoryCopy(&tilesetData->v0, &temp, sizeof(float_t));
|
|
||||||
|
|
||||||
if(tilesetData->v0 < 0.0f || tilesetData->v0 > 1.0f) {
|
if(tilesetData->v0 < 0.0f || tilesetData->v0 > 1.0f) {
|
||||||
errorThrow("Invalid v0 value in tileset");
|
errorThrow("Invalid v0 value in tileset");
|
||||||
|
|||||||
@@ -33,11 +33,6 @@
|
|||||||
#include <ogcsys.h>
|
#include <ogcsys.h>
|
||||||
#include <gccore.h>
|
#include <gccore.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <sys/endian.h>
|
|
||||||
#else
|
|
||||||
#ifndef le32toh
|
|
||||||
#define le32toh(x) (x)
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef bool bool_t;
|
typedef bool bool_t;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
PUBLIC
|
PUBLIC
|
||||||
array.c
|
array.c
|
||||||
|
endian.c
|
||||||
memory.c
|
memory.c
|
||||||
string.c
|
string.c
|
||||||
math.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