diff options
| author | skal <pascal.massimino@gmail.com> | 2026-01-28 09:45:18 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-01-28 09:45:18 +0100 |
| commit | 438333cf6f1dcd9f8d6a94fc702a952b070353b4 (patch) | |
| tree | b3690e9fa22037d9fbb8c2f0ffa77767ac6b5db2 /src | |
| parent | b8d42b0ccdc3ead63e541c9d38ca33bfe7bf7c65 (diff) | |
refactor(assets): Optimize asset retrieval using array lookup
This refactors the asset management system to be more efficient and cleaner.
- Moved common GetAsset/DropAsset logic to src/util/asset_manager.cc.
- Changed retrieval to use an array of records (AssetRecord) for O(1) lookups instead of a switch statement.
- Updated asset_packer to generate only raw data and the record array.
Diffstat (limited to 'src')
| -rw-r--r-- | src/util/asset_manager.cc | 30 | ||||
| -rw-r--r-- | src/util/asset_manager.h | 19 |
2 files changed, 49 insertions, 0 deletions
diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc new file mode 100644 index 0000000..80f8f85 --- /dev/null +++ b/src/util/asset_manager.cc @@ -0,0 +1,30 @@ +// This file is part of the 64k demo project. +// It implements the generic asset retrieval logic. +// Uses an array lookup for O(1) access to embedded data. + +#include "util/asset_manager.h" +#include "assets.h" + +// These are defined in the generated assets_data.cc +extern const AssetRecord g_assets[]; +extern const size_t g_assets_count; + +const uint8_t *GetAsset(AssetId asset_id, size_t *out_size) { + uint16_t index = static_cast<uint16_t>(asset_id); + if (index >= g_assets_count) { + if (out_size) + *out_size = 0; + return nullptr; + } + + const AssetRecord &record = g_assets[index]; + if (out_size) + *out_size = record.size; + return record.data; +} + +void DropAsset(AssetId asset_id, const uint8_t *asset) { + (void)asset_id; + (void)asset; + // Implementation for lazy decompression will go here +} diff --git a/src/util/asset_manager.h b/src/util/asset_manager.h new file mode 100644 index 0000000..1b5e510 --- /dev/null +++ b/src/util/asset_manager.h @@ -0,0 +1,19 @@ +// This file is part of the 64k demo project. +// It defines the core structures and interface for asset management. +// Used for efficient retrieval of embedded binary resources. + +#pragma once +#include <cstddef> +#include <cstdint> + +// Forward declaration of the generated enum +enum class AssetId : uint16_t; + +struct AssetRecord { + const uint8_t *data; + size_t size; +}; + +// Generic interface +const uint8_t *GetAsset(AssetId asset_id, size_t *out_size = nullptr); +void DropAsset(AssetId asset_id, const uint8_t *asset); |
