Map loading
This commit is contained in:
@@ -10,6 +10,8 @@
|
||||
#include "type/assetalphaimage.h"
|
||||
#include "type/assetlanguage.h"
|
||||
#include "type/assetscript.h"
|
||||
#include "type/assetmap.h"
|
||||
#include "type/assetmapchunk.h"
|
||||
#include <zip.h>
|
||||
|
||||
typedef enum {
|
||||
@@ -19,6 +21,8 @@ typedef enum {
|
||||
ASSET_TYPE_ALPHA_IMAGE,
|
||||
ASSET_TYPE_LANGUAGE,
|
||||
ASSET_TYPE_SCRIPT,
|
||||
ASSET_TYPE_MAP,
|
||||
ASSET_TYPE_MAP_CHUNK,
|
||||
|
||||
ASSET_TYPE_COUNT,
|
||||
} assettype_t;
|
||||
@@ -73,4 +77,16 @@ static const assettypedef_t ASSET_TYPE_DEFINITIONS[ASSET_TYPE_COUNT] = {
|
||||
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
||||
.custom = assetScriptHandler
|
||||
},
|
||||
|
||||
[ASSET_TYPE_MAP] = {
|
||||
.header = "DMF",
|
||||
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
||||
.custom = assetMapHandler
|
||||
},
|
||||
|
||||
[ASSET_TYPE_MAP_CHUNK] = {
|
||||
.header = "DMC",
|
||||
.loadStrategy = ASSET_LOAD_STRAT_CUSTOM,
|
||||
.custom = assetMapChunkHandler
|
||||
},
|
||||
};
|
||||
@@ -10,4 +10,6 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
assetpaletteimage.c
|
||||
assetlanguage.c
|
||||
assetscript.c
|
||||
assetmap.c
|
||||
assetmapchunk.c
|
||||
)
|
||||
15
src/asset/type/assetmap.c
Normal file
15
src/asset/type/assetmap.c
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "asset/asset.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
errorret_t assetMapHandler(assetcustom_t custom) {
|
||||
printf("Map Loaded from asset!\n");
|
||||
errorOk();
|
||||
}
|
||||
20
src/asset/type/assetmap.h
Normal file
20
src/asset/type/assetmap.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
|
||||
typedef struct assetcustom_s assetcustom_t;
|
||||
|
||||
/**
|
||||
* Loads a map asset from the given data pointer into the output map structure.
|
||||
*
|
||||
* @param data Pointer to the raw assetmap_t data.
|
||||
* @param output Pointer to the map_t to load the map into.
|
||||
* @return An error code.
|
||||
*/
|
||||
errorret_t assetMapHandler(assetcustom_t custom);
|
||||
178
src/asset/type/assetmapchunk.c
Normal file
178
src/asset/type/assetmapchunk.c
Normal file
@@ -0,0 +1,178 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "asset/asset.h"
|
||||
#include "assert/assert.h"
|
||||
#include "map/mapchunk.h"
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint32_t tileCount;
|
||||
uint8_t modelCount;
|
||||
uint8_t entityCount;
|
||||
} assetchunkheader_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
maptile_t tile;
|
||||
} assetchunktiledata_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint32_t vertexCount;
|
||||
} assetchunkmodelheader_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint8_t entityType;
|
||||
uint8_t localX;
|
||||
uint8_t localY;
|
||||
uint8_t localZ;
|
||||
} assetchunkentityheader_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
errorret_t assetMapChunkHandler(assetcustom_t custom) {
|
||||
assertNotNull(custom.output, "Output pointer cannot be NULL");
|
||||
assertNotNull(custom.zipFile, "Zip file pointer cannot be NULL");
|
||||
|
||||
mapchunk_t *chunk = (mapchunk_t *)custom.output;
|
||||
assertTrue(chunk->meshCount == 0, "Chunk is not in a good state");
|
||||
|
||||
// Read header
|
||||
assetchunkheader_t header;
|
||||
size_t bytesRead = zip_fread(
|
||||
custom.zipFile, &header, sizeof(assetchunkheader_t)
|
||||
);
|
||||
if(bytesRead != sizeof(assetchunkheader_t)) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow("Failed to read chunk asset header.");
|
||||
}
|
||||
|
||||
if(header.tileCount != CHUNK_TILE_COUNT) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow(
|
||||
"Chunk asset has invalid tile count: %d (expected %d).",
|
||||
header.tileCount,
|
||||
CHUNK_TILE_COUNT
|
||||
);
|
||||
}
|
||||
|
||||
if(header.modelCount > CHUNK_MESH_COUNT_MAX) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow(
|
||||
"Chunk asset has too many models: %d (max %d).",
|
||||
header.modelCount,
|
||||
CHUNK_MESH_COUNT_MAX
|
||||
);
|
||||
}
|
||||
|
||||
if(header.entityCount > CHUNK_ENTITY_COUNT_MAX) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow(
|
||||
"Chunk asset has too many entities: %d (max %d).",
|
||||
header.entityCount,
|
||||
CHUNK_ENTITY_COUNT_MAX
|
||||
);
|
||||
}
|
||||
|
||||
chunk->meshCount = header.modelCount;
|
||||
|
||||
// Read tile data
|
||||
bytesRead = zip_fread(
|
||||
custom.zipFile,
|
||||
chunk->tiles,
|
||||
sizeof(assetchunktiledata_t) * header.tileCount
|
||||
);
|
||||
if(bytesRead != sizeof(assetchunktiledata_t) * header.tileCount) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow("Failed to read chunk tile data.");
|
||||
}
|
||||
|
||||
// For each model...
|
||||
uint32_t vertexIndex = 0;
|
||||
for(uint8_t i = 0; i < header.modelCount; i++) {
|
||||
assetchunkmodelheader_t modelHeader;
|
||||
bytesRead = zip_fread(
|
||||
custom.zipFile, &modelHeader, sizeof(assetchunkmodelheader_t)
|
||||
);
|
||||
if(bytesRead != sizeof(assetchunkmodelheader_t)) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow("Failed to read chunk model header.");
|
||||
}
|
||||
|
||||
if(
|
||||
vertexIndex + modelHeader.vertexCount >
|
||||
CHUNK_VERTEX_COUNT_MAX
|
||||
) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow("Chunk model vertex count exceeds maximum.");
|
||||
}
|
||||
|
||||
// Read vertex data.
|
||||
bytesRead = zip_fread(
|
||||
custom.zipFile,
|
||||
&chunk->vertices[vertexIndex],
|
||||
sizeof(meshvertex_t) * modelHeader.vertexCount
|
||||
);
|
||||
if(bytesRead != sizeof(meshvertex_t) * modelHeader.vertexCount) {
|
||||
zip_fclose(custom.zipFile);
|
||||
errorThrow("Failed to read chunk model vertex data.");
|
||||
}
|
||||
|
||||
// Init the mesh
|
||||
if(modelHeader.vertexCount > 0) {
|
||||
mesh_t *mesh = &chunk->meshes[i];
|
||||
meshInit(
|
||||
mesh,
|
||||
MESH_PRIMITIVE_TRIANGLES,
|
||||
modelHeader.vertexCount,
|
||||
&chunk->vertices[vertexIndex]
|
||||
);
|
||||
|
||||
vertexIndex += modelHeader.vertexCount;
|
||||
} else {
|
||||
chunk->meshes[i].vertexCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Read entity data
|
||||
// for(uint8_t i = 0; i < header.entityCount; i++) {
|
||||
// assetchunkentityheader_t entityHeader;
|
||||
// bytesRead = zip_fread(
|
||||
// custom.zipFile, &entityHeader, sizeof(assetchunkentityheader_t)
|
||||
// );
|
||||
// if(bytesRead != sizeof(assetchunkentityheader_t)) {
|
||||
// zip_fclose(custom.zipFile);
|
||||
// errorThrow("Failed to read chunk entity header.");
|
||||
// }
|
||||
|
||||
// uint8_t entityIndex = entityGetAvailable();
|
||||
// if(entityIndex == 0xFF) {
|
||||
// zip_fclose(custom.zipFile);
|
||||
// errorThrow("No available entity slots.");
|
||||
// }
|
||||
|
||||
// entity_t *entity = &ENTITIES[entityIndex];
|
||||
// entityInit(entity, (entitytype_t)entityHeader.entityType);
|
||||
// entity->position.x = (
|
||||
// (chunk->position.x * CHUNK_WIDTH) + entityHeader.localX
|
||||
// );
|
||||
// entity->position.y = (
|
||||
// (chunk->position.y * CHUNK_HEIGHT) + entityHeader.localY
|
||||
// );
|
||||
// entity->position.z = (
|
||||
// (chunk->position.z * CHUNK_DEPTH) + entityHeader.localZ
|
||||
// );
|
||||
|
||||
// chunk->entities[i] = entityIndex;
|
||||
// }
|
||||
|
||||
errorOk();
|
||||
}
|
||||
19
src/asset/type/assetmapchunk.h
Normal file
19
src/asset/type/assetmapchunk.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
|
||||
typedef struct assetcustom_s assetcustom_t;
|
||||
|
||||
/**
|
||||
* Handles loading of map chunk data from a map chunk asset file.
|
||||
*
|
||||
* @param custom The custom asset loading parameters.
|
||||
* @return An error code.
|
||||
*/
|
||||
errorret_t assetMapChunkHandler(assetcustom_t custom);
|
||||
Reference in New Issue
Block a user