From bf46e44e1cb6027a072819a2a3aa3be32651f6e1 Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 3 Feb 2026 18:44:41 +0100 Subject: refactor: Task #20 - Platform & Code Hygiene - Consolidated all WebGPU shims and platform-specific logic into src/platform.h. - Refactored platform_init to return PlatformState by value and platform_poll to automatically refresh time and aspect_ratio. - Removed STL dependencies (std::map, std::vector, std::string) from AssetManager and Procedural subsystems. - Fixed Windows cross-compilation by adjusting include paths and linker flags in CMakeLists.txt and updating build_win.sh. - Removed redundant direct inclusions of GLFW/glfw3.h and WebGPU headers across the project. - Applied clang-format and updated documentation. handoff(Gemini): Completed Task #20 and 20.1. Platform abstraction is now unified, and core paths are STL-free. Windows build is stable. --- src/util/asset_manager.cc | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'src/util/asset_manager.cc') diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc index d9ecfe1..9294560 100644 --- a/src/util/asset_manager.cc +++ b/src/util/asset_manager.cc @@ -8,21 +8,26 @@ #include "generated/assets.h" #endif /* defined(USE_TEST_ASSETS) */ -#include // For free -#include // For std::cerr -#include // For kAssetManagerProcGenFuncMap -#include // For placement new -#include // For std::string in map -#include // For potential dynamic allocation for procedural assets +#include // For fprintf +#include // For free +#include // For strcmp +#include // For placement new #include "procedural/generator.h" // For ProcGenFunc and procedural functions // Map of procedural function names to their pointers (for runtime dispatch) -static const std::map kAssetManagerProcGenFuncMap = { +struct ProcGenEntry { + const char* name; + ProcGenFunc func; +}; + +static const ProcGenEntry kAssetManagerProcGenFuncs[] = { {"gen_noise", procedural::gen_noise}, {"gen_grid", procedural::gen_grid}, {"make_periodic", procedural::make_periodic}, }; +static const size_t kNumProcGenFuncs = + sizeof(kAssetManagerProcGenFuncs) / sizeof(kAssetManagerProcGenFuncs[0]); // Array-based cache for assets. // Initialized to all zeros (nullptr data, 0 size, false is_procedural) @@ -63,31 +68,37 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) { } AssetRecord source_record = 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; + ProcGenFunc proc_gen_func_ptr = nullptr; + for (size_t i = 0; i < kNumProcGenFuncs; ++i) { + if (strcmp(source_record.proc_func_name_str, + kAssetManagerProcGenFuncs[i].name) == 0) { + proc_gen_func_ptr = kAssetManagerProcGenFuncs[i].func; + break; + } + } + + if (proc_gen_func_ptr == nullptr) { + fprintf(stderr, "Error: Unknown procedural function at runtime: %s\n", + source_record.proc_func_name_str); 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 + size_t data_size = (size_t)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; + fprintf(stderr, + "Error: Failed to allocate memory for procedural asset.\n"); if (out_size) *out_size = 0; return nullptr; -- cgit v1.2.3