summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-03 18:44:41 +0100
committerskal <pascal.massimino@gmail.com>2026-02-03 18:44:41 +0100
commitbf46e44e1cb6027a072819a2a3aa3be32651f6e1 (patch)
tree21267e7ef52fd91e7b99271ed87e275e91b3de3c /src/util
parent815c428dea14a6a1ea5c421c400985d0c14d473d (diff)
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.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/asset_manager.cc45
1 files changed, 28 insertions, 17 deletions
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 <cstdlib> // For free
-#include <iostream> // For std::cerr
-#include <map> // For kAssetManagerProcGenFuncMap
-#include <new> // For placement new
-#include <string> // For std::string in map
-#include <vector> // For potential dynamic allocation for procedural assets
+#include <cstdio> // For fprintf
+#include <cstdlib> // For free
+#include <cstring> // For strcmp
+#include <new> // 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<std::string, ProcGenFunc> 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;