aboutsummaryrefslogtreecommitdiff
path: root/src/assets.c
diff options
context:
space:
mode:
authornathansmithsmith <nathansmith7@mailfence.com>2023-11-08 12:12:29 -0700
committernathansmithsmith <nathansmith7@mailfence.com>2023-11-08 12:12:29 -0700
commitac7470ce5d680caa7fb53c235aea10cba45e1836 (patch)
tree4eeb6912414ac9280cf8c1fad267040b0ada2e98 /src/assets.c
parent10a0fb1c52a50b44bd2df064c977a3a15146facf (diff)
rres asset loading working
Diffstat (limited to 'src/assets.c')
-rw-r--r--src/assets.c131
1 files changed, 112 insertions, 19 deletions
diff --git a/src/assets.c b/src/assets.c
index bdd0d8d..ad8e40a 100644
--- a/src/assets.c
+++ b/src/assets.c
@@ -1,25 +1,27 @@
#include "assets.h"
+#include "rres.h"
+#include "rres-raylib.h"
const char textureAssetPaths[TEXTURE_ASSET_COUNT][ASSET_PATH_MAX] = {
- "/home/nathan/Documents/KillaFacsista/assets/icon.png",
- "/home/nathan/Documents/KillaFacsista/assets/icon128.png",
- "/home/nathan/Documents/KillaFacsista/assets/icon64.png",
- "/home/nathan/Documents/KillaFacsista/assets/gyroscope.png",
- "/home/nathan/Documents/KillaFacsista/assets/skyTexture.png"
+ "icon.png",
+ "icon128.png",
+ "icon64.png",
+ "gyroscope.png",
+ "skyTexture.png"
};
const char modelAssetPaths[MODEL_ASSET_COUNT][ASSET_PATH_MAX] = {
- "/home/nathan/Documents/KillaFacsista/assets/antifaShip.obj",
- "/home/nathan/Documents/KillaFacsista/assets/soldato.obj",
- "/home/nathan/Documents/KillaFacsista/assets/caporale.obj",
- "/home/nathan/Documents/KillaFacsista/assets/sergente.obj",
- "/home/nathan/Documents/KillaFacsista/assets/maresciallo.obj",
- "/home/nathan/Documents/KillaFacsista/assets/generale.obj",
- "/home/nathan/Documents/KillaFacsista/assets/mussolini.obj",
- "/home/nathan/Documents/KillaFacsista/assets/guidedMissile.obj",
- "/home/nathan/Documents/KillaFacsista/assets/missile.obj",
- "/home/nathan/Documents/KillaFacsista/assets/gyroscope.obj",
- "/home/nathan/Documents/KillaFacsista/assets/sky.obj"
+ "antifaShip.obj",
+ "soldato.obj",
+ "caporale.obj",
+ "sergente.obj",
+ "maresciallo.obj",
+ "generale.obj",
+ "mussolini.obj",
+ "guidedMissile.obj",
+ "missile.obj",
+ "gyroscope.obj",
+ "sky.obj"
};
// Some models have textures and other stuff to be set.
@@ -39,21 +41,112 @@ void configModelAssets(Assets * assets) {
);
}
+void loadTextureAsset(Assets * assets, int index, rresCentralDir dir, const char * filePath) {
+ // Load and unpack.
+ rresResourceChunk chunk = rresLoadResourceChunk(filePath, rresGetResourceId(dir, textureAssetPaths[index]));
+ int result = UnpackResourceChunk(&chunk);
+
+ if (result == 0) {
+ Image image = LoadImageFromResource(chunk);
+
+ if (image.data == NULL) {
+ TraceLog(LOG_WARNING, "Issue loading image: %s", textureAssetPaths[index]);
+ } else {
+ // Load texture and unload image.
+ assets->textures[index] = LoadTextureFromImage(image);
+ UnloadImage(image);
+ }
+ } else {
+ TraceLog(LOG_WARNING, "Issue unpacking resource: %s", textureAssetPaths[index]);
+ }
+
+ rresUnloadResourceChunk(chunk);
+}
+
+void loadModelAsset(Assets * assets, int index, rresCentralDir dir, const char * filePath) {
+ // Model chunk.
+ rresResourceChunk modelChunk = rresLoadResourceChunk(filePath, rresGetResourceId(dir, modelAssetPaths[index]));
+
+ // Material chunk.
+ char materialPath[ASSET_PATH_MAX];
+ snprintf(materialPath, ASSET_PATH_MAX, "%s.mtl", GetFileNameWithoutExt(modelAssetPaths[index]));
+ rresResourceChunk materialChunk = rresLoadResourceChunk(filePath, rresGetResourceId(dir, materialPath));
+
+ int result = UnpackResourceChunk(&modelChunk);
+
+ // Load and create temp file for model.
+ if (result == 0) {
+ unsigned int dataSize;
+ void * data = LoadDataFromResource(modelChunk, &dataSize);
+
+ if (data == NULL || data <= 0) {
+ TraceLog(LOG_WARNING, "Issues loading model: %s", modelAssetPaths[index]);
+ } else {
+ FILE * tempFile = fopen("temp.obj", "wb");
+ fwrite(data, 1, dataSize, tempFile);
+ MemFree(data);
+ fclose(tempFile);
+ }
+ } else {
+ TraceLog(LOG_WARNING, "Issue unpacking resource: %s", modelAssetPaths[index]);
+ }
+
+ result = UnpackResourceChunk(&materialChunk);
+
+ // Load and create temp file for material.
+ if (result == 0) {
+ unsigned int dataSize;
+ void * data = LoadDataFromResource(materialChunk, &dataSize);
+
+ if (data == NULL || data <= 0) {
+ TraceLog(LOG_WARNING, "Issues loading material: %s", materialPath);
+ } else {
+ FILE * tempFile = fopen(materialPath, "wb");
+ fwrite(data, 1, dataSize, tempFile);
+ MemFree(data);
+ fclose(tempFile);
+ }
+ } else {
+ TraceLog(LOG_WARNING, "Issue unpacking resource: %s", materialPath);
+ }
+
+ // Load model now (:
+ assets->models[index] = LoadModel("temp.obj");
+
+ // Remove temp files.
+ remove("temp.obj");
+ remove(materialPath);
+
+ // Free this bloody shit.
+ rresUnloadResourceChunk(modelChunk);
+ rresUnloadResourceChunk(materialChunk);
+}
+
void LoadAssets(Assets * assets) {
int i;
+ TraceLog(LOG_INFO, "Loading assets");
+
+ const char * filePath = "assets.rres";
+
+ // Load centeral dir.
+ rresCentralDir dir = rresLoadCentralDirectory(filePath);
+
+ if (dir.count == 0)
+ TraceLog(LOG_WARNING, "No central directory available in %s", filePath);
+
// Textures first because models can use textures.
// Textures.
for (i = 0; i < TEXTURE_ASSET_COUNT; ++i)
- assets->textures[i] = LoadTexture(textureAssetPaths[i]);
+ loadTextureAsset(assets, i, dir, filePath);
// Models.
for (i = 0; i < MODEL_ASSET_COUNT; ++i)
- assets->models[i] = LoadModel(modelAssetPaths[i]);
+ loadModelAsset(assets, i, dir, filePath);
+ rresUnloadCentralDirectory(dir);
configModelAssets(assets);
-
TraceLog(LOG_INFO, "Assets loaded");
}