78 lines
1.9 KiB
C
78 lines
1.9 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "assettexture.h"
|
|
#include "asset/assettype.h"
|
|
#include "assert/assert.h"
|
|
#include "display/texture/texture.h"
|
|
#include "util/endian.h"
|
|
|
|
errorret_t assetTextureLoad(assetentire_t entire) {
|
|
assertNotNull(entire.data, "Data pointer cannot be NULL.");
|
|
assertNotNull(entire.output, "Output pointer cannot be NULL.");
|
|
|
|
assettexture_t *assetData = (assettexture_t *)entire.data;
|
|
texture_t *texture = (texture_t *)entire.output;
|
|
|
|
// Read header and version (first 4 bytes)
|
|
if(
|
|
assetData->header[0] != 'D' ||
|
|
assetData->header[1] != 'T' ||
|
|
assetData->header[2] != 'X'
|
|
) {
|
|
errorThrow("Invalid texture header");
|
|
}
|
|
|
|
// Version (can only be 1 atm)
|
|
if(assetData->version != 0x01) {
|
|
errorThrow("Unsupported texture version");
|
|
}
|
|
|
|
// Fix endian
|
|
assetData->width = endianLittleToHost32(assetData->width);
|
|
assetData->height = endianLittleToHost32(assetData->height);
|
|
|
|
// Check dimensions.
|
|
if(
|
|
assetData->width == 0 || assetData->width > ASSET_TEXTURE_WIDTH_MAX ||
|
|
assetData->height == 0 || assetData->height > ASSET_TEXTURE_HEIGHT_MAX
|
|
) {
|
|
errorThrow("Invalid texture dimensions");
|
|
}
|
|
|
|
// Validate format
|
|
textureformat_t format;
|
|
texturedata_t data;
|
|
|
|
switch(assetData->type) {
|
|
case 0x00: // RGBA8888
|
|
format = TEXTURE_FORMAT_RGBA;
|
|
data.rgbaColors = (color_t *)assetData->data;
|
|
break;
|
|
|
|
// case 0x01:
|
|
// format = TEXTURE_FORMAT_RGB;
|
|
// break;
|
|
|
|
// case 0x02:
|
|
// format = TEXTURE_FORMAT_RGB565;
|
|
// break;
|
|
|
|
// case 0x03:
|
|
// format = TEXTURE_FORMAT_RGB5A3;
|
|
// break;
|
|
|
|
default:
|
|
errorThrow("Unsupported texture format");
|
|
}
|
|
|
|
errorChain(textureInit(
|
|
texture, assetData->width, assetData->height, format, data
|
|
));
|
|
|
|
errorOk();
|
|
} |