Refact
This commit is contained in:
@@ -21,9 +21,6 @@ assetdef_t ASSET_DEFINITIONS[ASSET_TYPE_COUNT] = {
|
||||
[ASSET_TYPE_CONFIG] = {
|
||||
"DCF", assetConfigLoad, assetConfigDispose
|
||||
},
|
||||
[ASSET_TYPE_RPG_MAP] = {
|
||||
"DRM", assetRPGMapLoad, assetRPGMapDispose
|
||||
},
|
||||
};
|
||||
|
||||
errorret_t assetInit(asset_t *asset, const char_t *filename) {
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
#include "asset/type/assetpaletteimage.h"
|
||||
#include "asset/type/assetalphaimage.h"
|
||||
#include "asset/type/assetconfig.h"
|
||||
#include "asset/type/assetrpgmap.h"
|
||||
|
||||
#define ASSET_HEADER_SIZE 3
|
||||
#define ASSET_REFERENCE_COUNT_MAX 8
|
||||
@@ -34,7 +33,6 @@ typedef enum {
|
||||
ASSET_TYPE_PALETTE_IMAGE,
|
||||
ASSET_TYPE_ALPHA_IMAGE,
|
||||
ASSET_TYPE_CONFIG,
|
||||
ASSET_TYPE_RPG_MAP,
|
||||
|
||||
ASSET_TYPE_COUNT
|
||||
} assettype_t;
|
||||
@@ -51,7 +49,6 @@ typedef struct asset_s {
|
||||
assetpaletteimage_t paletteImage;
|
||||
assetalphaimage_t alphaImage;
|
||||
assetconfig_t config;
|
||||
assetrpgmap_t rpgMap;
|
||||
};
|
||||
} asset_t;
|
||||
|
||||
|
||||
@@ -9,5 +9,4 @@ target_sources(${DUSK_TARGET_NAME}
|
||||
assetalphaimage.c
|
||||
assetconfig.c
|
||||
assetpaletteimage.c
|
||||
assetrpgmap.c
|
||||
)
|
||||
@@ -1,98 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "assetrpgmap.h"
|
||||
#include "asset/asset.h"
|
||||
#include "assert/assert.h"
|
||||
#include "display/tileset/tilesetlist.h"
|
||||
|
||||
errorret_t assetRPGMapLoad(asset_t *asset) {
|
||||
assertNotNull(asset, "Asset cannot be NULL.");
|
||||
assertTrue(
|
||||
asset->type == ASSET_TYPE_RPG_MAP,
|
||||
"Asset is not of type ASSET_TYPE_RPG_MAP."
|
||||
);
|
||||
|
||||
assetrpgmapraw_t raw;
|
||||
|
||||
// Read map header info
|
||||
zip_int64_t bytesRead = zip_fread(
|
||||
asset->file, &raw.header, sizeof(assetrpgmapheader_t)
|
||||
);
|
||||
if(bytesRead != sizeof(raw.header)) {
|
||||
errorThrow("Failed to read RPG map header.");
|
||||
}
|
||||
|
||||
if(raw.header.mapWidth == 0 || raw.header.mapWidth > MAP_WIDTH_MAX) {
|
||||
errorThrow("Invalid RPG map width.");
|
||||
}
|
||||
|
||||
if(raw.header.mapHeight == 0 || raw.header.mapHeight > MAP_HEIGHT_MAX) {
|
||||
errorThrow("Invalid RPG map height.");
|
||||
}
|
||||
|
||||
if(raw.header.tilesetCount == 0) {
|
||||
errorThrow("Invalid RPG map tileset count.");
|
||||
}
|
||||
|
||||
if(raw.header.tilesetCount > ASSET_RPG_MAP_TILESET_COUNT_MAX) {
|
||||
errorThrow("Invalid RPG map tileset count.");
|
||||
}
|
||||
|
||||
if(raw.header.tileLayerCount == 0) {
|
||||
errorThrow("Invalid RPG map layer count.");
|
||||
}
|
||||
|
||||
if(raw.header.tileLayerCount > MAP_LAYER_COUNT_MAX) {
|
||||
errorThrow("Invalid RPG map layer count.");
|
||||
}
|
||||
|
||||
// Read layers
|
||||
for(uint32_t layer = 0; layer < raw.header.tileLayerCount; layer++) {
|
||||
// Read width * height tiles
|
||||
bytesRead = zip_fread(
|
||||
asset->file,
|
||||
&raw.layers[layer].tiles,
|
||||
sizeof(assetrpgmaptile_t) * raw.header.mapWidth * raw.header.mapHeight
|
||||
);
|
||||
if(bytesRead != (
|
||||
sizeof(assetrpgmaptile_t) * raw.header.mapWidth * raw.header.mapHeight
|
||||
)) errorThrow("Failed to read RPG map layer %u.", layer);
|
||||
}
|
||||
|
||||
// For each tileset
|
||||
for(uint32_t tileset = 0; tileset < raw.header.tilesetCount; tileset++) {
|
||||
// Read tileset
|
||||
bytesRead = zip_fread(
|
||||
asset->file,
|
||||
&raw.tilesets[tileset],
|
||||
sizeof(assetrpgmaptileset_t)
|
||||
);
|
||||
if(bytesRead != sizeof(assetrpgmaptileset_t)) {
|
||||
errorThrow("Failed to read RPG map tileset %u.", tileset);
|
||||
}
|
||||
}
|
||||
|
||||
// Map data is loaded, we can load it into the map structure.
|
||||
mapInit(&asset->rpgMap.map);
|
||||
|
||||
entity_t *ent;
|
||||
ent = mapEntityAdd(&asset->rpgMap.map);
|
||||
entityInit(ent, ENTITY_TYPE_PLAYER, &asset->rpgMap.map);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetRPGMapDispose(asset_t *asset) {
|
||||
assertNotNull(asset, "Asset cannot be NULL.");
|
||||
assertTrue(
|
||||
asset->type == ASSET_TYPE_RPG_MAP,
|
||||
"Asset is not of type ASSET_TYPE_RPG_MAP."
|
||||
);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
#include "rpg/world/map.h"
|
||||
|
||||
typedef struct asset_s asset_t;
|
||||
|
||||
#define ASSET_RPG_MAP_TILESET_COUNT_MAX 16
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct {
|
||||
uint32_t mapWidth;
|
||||
uint32_t mapHeight;
|
||||
uint32_t tilesetCount;
|
||||
uint32_t tileLayerCount;
|
||||
} assetrpgmapheader_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t firstGid;
|
||||
uint32_t tilesetIndex;
|
||||
} assetrpgmaptileset_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t tilesetIndex;
|
||||
} assetrpgmaptile_t;
|
||||
|
||||
typedef struct {
|
||||
assetrpgmaptile_t tiles[MAP_TILE_COUNT_MAX];
|
||||
} assetrpgmaplayer_t;
|
||||
|
||||
typedef struct {
|
||||
assetrpgmapheader_t header;
|
||||
assetrpgmaplayer_t layers[MAP_LAYER_COUNT_MAX];
|
||||
assetrpgmaptileset_t tilesets[ASSET_RPG_MAP_TILESET_COUNT_MAX];
|
||||
} assetrpgmapraw_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
typedef struct {
|
||||
map_t map;
|
||||
} assetrpgmap_t;
|
||||
|
||||
/**
|
||||
* Loads an RPG map asset from the given asset structure. The asset must be of
|
||||
* type ASSET_TYPE_RPG_MAP and must be loaded.
|
||||
*
|
||||
* @param asset The asset to load the RPG map from.
|
||||
* @return An error code.
|
||||
*/
|
||||
errorret_t assetRPGMapLoad(asset_t *asset);
|
||||
|
||||
/**
|
||||
* Disposes of an RPG map asset, freeing any allocated resources.
|
||||
*
|
||||
* @param asset The asset to dispose of.
|
||||
* @return An error code.
|
||||
*/
|
||||
errorret_t assetRPGMapDispose(asset_t *asset);
|
||||
Reference in New Issue
Block a user