First texture rendering (if broken)

This commit is contained in:
2026-02-06 12:48:49 -06:00
parent 0d56859d94
commit aa5b41fe31
23 changed files with 226 additions and 179 deletions

View File

@@ -29,11 +29,12 @@ errorret_t assetInit(void) {
DIR *pdir = opendir(*dolphinSearchPath);
if(pdir == NULL) continue;
// Scan if file is present
while(true) {
struct dirent* pent = readdir(pdir);
if(pent == NULL) break;
if(stringCompareInsensitive(pent->d_name, ASSET_FILE) != 0) {
continue;
}
@@ -42,13 +43,13 @@ errorret_t assetInit(void) {
snprintf(
foundPath,
FILENAME_MAX,
"%s%s",
"%s/%s",
*dolphinSearchPath,
ASSET_FILE
);
break;
}
// Close dir.
closedir(pdir);
@@ -56,6 +57,9 @@ errorret_t assetInit(void) {
if(foundPath[0] != '\0') break;
} while(*(++dolphinSearchPath) != NULL);
if(foundPath[0] != '\0') {
}
// Did we find the asset file?
if(foundPath[0] == '\0') {
errorThrow("Failed to find asset file on FAT filesystem.");
@@ -226,27 +230,45 @@ bool_t assetFileExists(const char_t *filename) {
}
errorret_t assetLoad(const char_t *filename, void *output) {
#if DOLPHIN
errorOk();
#endif
assertStrLenMax(filename, FILENAME_MAX, "Filename too long.");
assertNotNull(output, "Output pointer cannot be NULL.");
// Get file size of the asset.
zip_stat_t st;
zip_stat_init(&st);
if(!zip_stat(ASSET.zip, filename, 0, &st) == 0) {
errorThrow("Failed to stat asset file: %s", filename);
}
// Minimum file size.
zip_int64_t fileSize = (zip_int64_t)st.size;
if(fileSize < sizeof(assetheader_t)) {
errorThrow("Asset file too small to contain header: %s", filename);
}
// Try to open the file
zip_file_t *file = zip_fopen(ASSET.zip, filename, 0);
if(file == NULL) {
errorThrow("Failed to open asset file: %s", filename);
}
// Read the header.
zip_int64_t bytesRemaining = fileSize;
assetheader_t header;
memoryZero(&header, sizeof(assetheader_t));
zip_int64_t bytesRead = zip_fread(file, &header, sizeof(assetheader_t));
if(bytesRead != sizeof(assetheader_t)) {
zip_int64_t bytesRead = zip_fread(
file,
&header,
(zip_uint64_t)sizeof(assetheader_t)
);
if((size_t)bytesRead != sizeof(assetheader_t)) {
zip_fclose(file);
errorThrow("Failed to read asset header for: %s", filename);
}
bytesRemaining -= (zip_uint64_t)bytesRead;
assertTrue(sizeof(assetheader_t) == ASSET_HEADER_SIZE, "Asset header size mismatch.");
assertTrue(bytesRead == ASSET_HEADER_SIZE, "Asset header read size mismatch.");
// Find the asset type based on the header
const assettypedef_t *def = NULL;
@@ -276,20 +298,44 @@ errorret_t assetLoad(const char_t *filename, void *output) {
switch(def->loadStrategy) {
case ASSET_LOAD_STRAT_ENTIRE:
assertNotNull(def->entire, "Asset load function cannot be NULL.");
void *data = memoryAllocate(def->dataSize);
bytesRead = zip_fread(file, data, def->dataSize);
if(bytesRead == 0 || bytesRead > def->dataSize) {
// Must have more to read
if(bytesRemaining <= 0) {
zip_fclose(file);
errorThrow("No data remaining to read for asset: %s", filename);
}
if(bytesRemaining > def->dataSize) {
zip_fclose(file);
errorThrow(
"Asset file has too much data remaining after header: %s",
filename
);
}
// Create space to read the entire asset data
void *data = memoryAllocate(bytesRemaining);
if(!data) {
zip_fclose(file);
errorThrow("Failed to allocate memory for asset data of file: %s", filename);
}
// Read in the asset data.
bytesRead = zip_fread(file, data, bytesRemaining);
if(bytesRead == 0 || bytesRead > bytesRemaining) {
memoryFree(data);
zip_fclose(file);
errorThrow("Failed to read asset data for file: %s", filename);
}
bytesRemaining -= bytesRead;
// Close the file now we have the data
zip_fclose(file);
// Pass to the asset type loader
errorret_t ret = def->entire(data, output);
memoryFree(data);
errorChain(ret);
break;

View File

@@ -40,15 +40,23 @@
static const char_t *ASSET_DOLPHIN_PATHS[] = {
"/",
"/Dusk",
"%s/dusk",
"%s/DUSK",
"%s/apps",
"%s/apps/Dusk",
"%s/apps/dusk",
"%s/apps/DUSK",
"/dusk",
"/DUSK",
"/apps",
"/apps/Dusk",
"/apps/dusk",
"/apps/DUSK",
".",
"./",
"./Dusk",
"./dusk",
"./DUSK",
"./apps",
"./apps/Dusk",
"./apps/dusk",
"./apps/DUSK",
NULL
};
#endif
#define ASSET_FILE "dusk.dsk"

View File

@@ -9,13 +9,21 @@
#include "assert/assert.h"
#include "display/texture.h"
#include "debug/debug.h"
errorret_t assetAlphaImageLoad(void *data, void *output) {
assertNotNull(data, "Data pointer cannot be NULL.");
assertNotNull(output, "Output pointer cannot be NULL.");
debugPrint("Loading ASSET_TYPE_ALPHA_IMAGE asset.\n");
assetalphaimage_t *dataPtr = (assetalphaimage_t *)data;
texture_t *outputPtr = (texture_t *)output;
// Fix endian
dataPtr->width = le32toh(dataPtr->width);
dataPtr->height = le32toh(dataPtr->height);
textureInit(
outputPtr,
dataPtr->width,

View File

@@ -43,6 +43,13 @@ errorret_t assetLanguageInit(
errorThrow("Failed to read language asset header.");
}
// Fix the endianness of the header data.
for(uint32_t i = 0; i < LANG_KEY_COUNT; i++) {
lang->header.strings[i].chunk = le32toh(lang->header.strings[i].chunk);
lang->header.strings[i].offset = le32toh(lang->header.strings[i].offset);
lang->header.strings[i].length = le32toh(lang->header.strings[i].length);
}
lang->chunksOffset = zip_ftell(lang->zip);
if(lang->chunksOffset <= 0) {
zip_fclose(lang->zip);

View File

@@ -55,6 +55,9 @@ errorret_t assetMapChunkHandler(assetcustom_t custom) {
errorThrow("Failed to read chunk asset header.");
}
// Fix endianess if necessary
header.tileCount = le32toh(header.tileCount);
if(header.tileCount != CHUNK_TILE_COUNT) {
zip_fclose(custom.zipFile);
errorThrow(
@@ -107,6 +110,9 @@ errorret_t assetMapChunkHandler(assetcustom_t custom) {
errorThrow("Failed to read chunk model header.");
}
// Fix endianess if necessary
modelHeader.vertexCount = le32toh(modelHeader.vertexCount);
if(
vertexIndex + modelHeader.vertexCount >
CHUNK_VERTEX_COUNT_MAX

View File

@@ -15,6 +15,10 @@ errorret_t assetPaletteImageLoad(void *data, void *output) {
assetpaletteimage_t *assetData = (assetpaletteimage_t *)data;
texture_t *texture = (texture_t *)output;
// Fix endian
assetData->width = le32toh(assetData->width);
assetData->height = le32toh(assetData->height);
textureInit(
texture,