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;