summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/tests/test_assets.cc39
-rw-r--r--src/util/asset_manager.cc79
-rw-r--r--src/util/asset_manager.h12
3 files changed, 109 insertions, 21 deletions
diff --git a/src/tests/test_assets.cc b/src/tests/test_assets.cc
index b7ee8be..dd77b73 100644
--- a/src/tests/test_assets.cc
+++ b/src/tests/test_assets.cc
@@ -47,6 +47,45 @@ int main() {
printf("ASSET_LAST_ID test: SUCCESS\n");
printf("Asset size: %zu bytes\n", size);
+
+ // Test procedural asset
+ printf("\nRunning Procedural Asset test...\n");
+ size_t proc_size = 0;
+ 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
+
+ // Verify first few bytes are not all zero (noise should produce non-zero data)
+ bool non_zero_data = false;
+ for (size_t i = 0; i < 16; ++i) { // Check first 16 bytes
+ if (proc_data_1[i] != 0) {
+ non_zero_data = true;
+ break;
+ }
+ }
+ assert(non_zero_data);
+ printf("Procedural asset content verification: SUCCESS\n");
+
+ // Test DropAsset for procedural asset and re-generation
+ DropAsset(AssetId::ASSET_PROC_NOISE_256, proc_data_1);
+ // After dropping, GetAsset should generate new data
+ const uint8_t* proc_data_2 = GetAsset(AssetId::ASSET_PROC_NOISE_256, &proc_size);
+ assert(proc_data_2 != nullptr);
+ // assert(proc_data_1 != proc_data_2); // Removed: Allocator might reuse the same address
+
+ // Verify content again to ensure it was re-generated correctly
+ non_zero_data = false;
+ for (size_t i = 0; i < 16; ++i) {
+ if (proc_data_2[i] != 0) {
+ non_zero_data = true;
+ break;
+ }
+ }
+ assert(non_zero_data);
+ printf("Procedural asset DropAsset and re-generation test: SUCCESS\n");
+
+ printf("Procedural Asset test PASSED\n");
+
printf("AssetManager test PASSED\n");
return 0;
diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc
index 3874535..2ad8ef9 100644
--- a/src/util/asset_manager.cc
+++ b/src/util/asset_manager.cc
@@ -8,9 +8,21 @@
#include "generated/assets.h"
#endif /* defined(USE_TEST_ASSETS) */
-#include <vector> // For potential dynamic allocation for procedural assets
-#include <new> // For placement new
-#include <cstdlib> // For free
+#include <vector> // For potential dynamic allocation for procedural assets
+#include <new> // For placement new
+#include <cstdlib> // For free
+#include <iostream> // For std::cerr
+#include <map> // For kAssetManagerProcGenFuncMap
+#include <string> // For std::string in map
+
+#include "procedural/generator.h" // For ProcGenFunc and procedural functions
+
+// Map of procedural function names to their pointers (for runtime dispatch)
+static const std::map<std::string, ProcGenFunc> kAssetManagerProcGenFuncMap = {
+ {"gen_noise", procedural::gen_noise},
+ {"gen_grid", procedural::gen_grid},
+ {"make_periodic", procedural::make_periodic},
+};
// These are defined in the generated assets_data.cc
#if defined(USE_TEST_ASSETS)
@@ -47,20 +59,54 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) {
return g_asset_cache[index].data;
}
- // Not in cache, retrieve from static data (packed in binary)
+ // Not in cache, retrieve from static data (packed in binary) or generate procedurally
if (index >= g_assets_count) {
if (out_size)
*out_size = 0;
- return nullptr; // This asset is not in the static packed data either.
+ return nullptr; // Invalid asset_id or asset not in static packed data.
}
- // Store static record in cache for future use
- g_asset_cache[index] = g_assets[index];
- g_asset_cache[index].is_procedural = false;
+ AssetRecord source_record = g_assets[index];
+ AssetRecord cached_record = source_record;
+
+ if (source_record.is_procedural) {
+ // Dynamically generate the asset
+ auto it = kAssetManagerProcGenFuncMap.find(source_record.proc_func_name_str);
+ if (it == kAssetManagerProcGenFuncMap.end()) {
+ std::cerr << "Error: Unknown procedural function at runtime: " << source_record.proc_func_name_str << std::endl;
+ if (out_size) *out_size = 0;
+ return nullptr; // Procedural asset without a generation function.
+ }
+ ProcGenFunc proc_gen_func_ptr = it->second;
+
+ // For this demo, assuming procedural textures are RGBA8 256x256 (for simplicity and bump mapping).
+ // A more generic solution would pass dimensions in proc_params.
+ int width = 256, height = 256;
+ size_t data_size = width * height * 4; // RGBA8
+ uint8_t* generated_data = new (std::nothrow) uint8_t[data_size];
+ if (!generated_data) {
+ std::cerr << "Error: Failed to allocate memory for procedural asset." << std::endl;
+ if (out_size) *out_size = 0;
+ return nullptr;
+ }
+ proc_gen_func_ptr(generated_data, width, height, source_record.proc_params, source_record.num_proc_params);
+
+ cached_record.data = generated_data;
+ cached_record.size = data_size;
+ cached_record.is_procedural = true;
+ // proc_gen_func, proc_params, num_proc_params already copied from source_record
+
+ } else {
+ // Static asset (copy from g_assets)
+ cached_record.is_procedural = false;
+ }
+
+ // Store in cache for future use
+ g_asset_cache[index] = cached_record;
if (out_size)
- *out_size = g_asset_cache[index].size;
- return g_asset_cache[index].data;
+ *out_size = cached_record.size;
+ return cached_record.data;
}
void DropAsset(AssetId asset_id, const uint8_t* asset) {
@@ -69,14 +115,11 @@ void DropAsset(AssetId asset_id, const uint8_t* asset) {
return; // Invalid asset_id
}
- // Only free memory for procedural assets.
- if (g_asset_cache[index].is_procedural && g_asset_cache[index].data == asset) {
- // In a more complex scenario, we might track ref counts.
- // For this demo, we assume a single owner for dynamically allocated assets.
- delete[] g_asset_cache[index].data; // Assuming `new uint8_t[]` was used for procedural
- g_asset_cache[index].data = nullptr;
- g_asset_cache[index].size = 0;
- g_asset_cache[index].is_procedural = false;
+ // Check if the asset is in cache and is procedural, and if the pointer matches.
+ // This prevents accidentally freeing static data or freeing twice.
+ if (g_asset_cache[index].data == asset && g_asset_cache[index].is_procedural) {
+ delete[] g_asset_cache[index].data;
+ g_asset_cache[index] = {}; // Zero out the struct to force re-generation
}
// For static assets, no dynamic memory to free.
}
diff --git a/src/util/asset_manager.h b/src/util/asset_manager.h
index 6b09430..00aafc0 100644
--- a/src/util/asset_manager.h
+++ b/src/util/asset_manager.h
@@ -8,10 +8,16 @@
enum class AssetId : uint16_t; // Forward declaration
+// Type for procedural generation functions: (buffer, width, height, params, num_params)
+typedef void (*ProcGenFunc)(uint8_t*, int, int, const float*, int);
+
struct AssetRecord {
- const uint8_t* data;
- size_t size;
- bool is_procedural; // Flag to indicate if memory was allocated dynamically
+ const uint8_t* data; // Pointer to asset data (static or dynamic)
+ size_t size; // Size of the asset data
+ bool is_procedural; // True if data was dynamically allocated by a procedural generator
+ const char* proc_func_name_str; // Name of procedural generation function (string literal)
+ const float* proc_params; // Parameters for procedural generation (static, from assets.txt)
+ int num_proc_params; // Number of procedural parameters
};
// Generic interface