From 371e68c5284359daea0446dfc674473a1461614a Mon Sep 17 00:00:00 2001 From: skal Date: Thu, 5 Feb 2026 16:41:14 +0100 Subject: feat(assets): Add Texture Asset support (Task #18.0 prep) - Integrated stb_image for image decompression in asset_packer. - Added GetTextureAsset helper in asset_manager. - Updated procedural asset generation to include dimensions header for consistency. - Updated test_assets to verify new asset format. --- src/tests/test_assets.cc | 15 +++++++++------ src/util/asset_manager.cc | 23 +++++++++++++++++++++-- src/util/asset_manager.h | 9 +++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/tests/test_assets.cc b/src/tests/test_assets.cc index 128bb1c..a1ae795 100644 --- a/src/tests/test_assets.cc +++ b/src/tests/test_assets.cc @@ -54,13 +54,15 @@ int main() { const uint8_t* proc_data_1 = GetAsset(AssetId::ASSET_PROC_NOISE_256, &proc_size); assert(proc_data_1 != nullptr); - assert(proc_size == 256 * 256 * 4); // 256x256 RGBA8 + // Expect 256x256 RGBA8 + 8 byte header + assert(proc_size == 256 * 256 * 4 + 8); - // Verify first few bytes are not all zero (noise should produce non-zero - // data) + // Verify first few bytes of DATA (skip header) + // Header is 8 bytes + const uint8_t* pixel_data_1 = proc_data_1 + 8; bool non_zero_data = false; - for (size_t i = 0; i < 16; ++i) { // Check first 16 bytes - if (proc_data_1[i] != 0) { + for (size_t i = 0; i < 16; ++i) { // Check first 16 bytes of pixels + if (pixel_data_1[i] != 0) { non_zero_data = true; break; } @@ -79,8 +81,9 @@ int main() { // Verify content again to ensure it was re-generated correctly non_zero_data = false; + const uint8_t* pixel_data_2 = proc_data_2 + 8; for (size_t i = 0; i < 16; ++i) { - if (proc_data_2[i] != 0) { + if (pixel_data_2[i] != 0) { non_zero_data = true; break; } 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(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(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); -- cgit v1.2.3