diff options
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/asset_manager.cc | 23 | ||||
| -rw-r--r-- | src/util/asset_manager.h | 9 |
2 files changed, 30 insertions, 2 deletions
diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc index ce495ac..e5d0291 100644 --- a/src/util/asset_manager.cc +++ b/src/util/asset_manager.cc @@ -95,7 +95,8 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) { // simplicity and bump mapping). A more generic solution would pass // dimensions in proc_params. int width = 256, height = 256; - size_t data_size = (size_t)width * height * 4; // RGBA8 + size_t header_size = sizeof(uint32_t) * 2; + size_t data_size = header_size + (size_t)width * height * 4; // RGBA8 uint8_t* generated_data = new (std::nothrow) uint8_t[data_size]; if (!generated_data) { fprintf(stderr, @@ -104,7 +105,14 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) { *out_size = 0; return nullptr; } - if (!proc_gen_func_ptr(generated_data, width, height, + + // Write header + uint32_t* header = reinterpret_cast<uint32_t*>(generated_data); + header[0] = (uint32_t)width; + header[1] = (uint32_t)height; + + // Generate data after header + if (!proc_gen_func_ptr(generated_data + header_size, width, height, source_record.proc_params, source_record.num_proc_params)) { fprintf(stderr, "Error: Procedural generation failed for asset: %s\n", @@ -134,6 +142,17 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) { return cached_record.data; } +TextureAsset GetTextureAsset(AssetId asset_id) { + size_t size = 0; + const uint8_t* data = GetAsset(asset_id, &size); + if (!data || size < 8) { + return {0, 0, nullptr}; + } + + const uint32_t* header = reinterpret_cast<const uint32_t*>(data); + return { (int)header[0], (int)header[1], data + 8 }; +} + void DropAsset(AssetId asset_id, const uint8_t* asset) { uint16_t index = (uint16_t)asset_id; if (index >= (uint16_t)AssetId::ASSET_LAST_ID) { diff --git a/src/util/asset_manager.h b/src/util/asset_manager.h index 964b7af..7d332f2 100644 --- a/src/util/asset_manager.h +++ b/src/util/asset_manager.h @@ -32,3 +32,12 @@ struct AssetRecord { // - 'out_size' returns the original asset size (excluding the null terminator). const uint8_t* GetAsset(AssetId asset_id, size_t* out_size = nullptr); void DropAsset(AssetId asset_id, const uint8_t* asset); + +struct TextureAsset { + int width; + int height; + const uint8_t* pixels; +}; + +// Helper to retrieve and parse a simple texture asset (from packer's [w][h][pixels] format) +TextureAsset GetTextureAsset(AssetId asset_id); |
