summary refs log tree commit diff
path: root/src/Assets.hpp
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2024-04-14 22:47:08 +0200
committerMel <einebeere@gmail.com>2024-04-14 22:47:08 +0200
commitdc21ce532b56b56b1b60ff1bbe5726eff83e4dd5 (patch)
tree6166201c0543e72ecb77c715105e9a4bba06e796 /src/Assets.hpp
parent0b2474a476b8d54967c7362c2d6bdfc76af1f05b (diff)
downloadmeowcraft-dc21ce532b56b56b1b60ff1bbe5726eff83e4dd5.tar.zst
meowcraft-dc21ce532b56b56b1b60ff1bbe5726eff83e4dd5.zip
Fix SIOF [1] in asset system and rework for better flexibility
[1]: https://en.cppreference.com/w/cpp/language/siof
Diffstat (limited to 'src/Assets.hpp')
-rw-r--r--src/Assets.hpp56
1 files changed, 37 insertions, 19 deletions
diff --git a/src/Assets.hpp b/src/Assets.hpp
index 0de6d90..0d1c635 100644
--- a/src/Assets.hpp
+++ b/src/Assets.hpp
@@ -1,30 +1,48 @@
 #pragma once
 
 #include "Common/Sizes.hpp"
+#include "Common/Assert.hpp"
+#include "Common/Unique.hpp"
 
-#define MC_DECLARE_ASSET(ns, name, raw_name) \
-    namespace Files { extern char const* raw_name; } \
-    namespace ns { extern Char const* name; }
+#include "AllAssets.hpp"
 
-#define MC_DECLARE_SHADER_ASSET(name) \
-    MC_DECLARE_ASSET(Shaders::name, vertex, shaders_##name##_vertex) \
-    MC_DECLARE_ASSET(Shaders::name, fragment, shaders_##name##_fragment)
+namespace MC {
 
-namespace MC::Assets {
+using Asset = Char const* const;
+using AssetToken = U64;
 
-MC_DECLARE_SHADER_ASSET(terrain)
-MC_DECLARE_SHADER_ASSET(clouds)
-MC_DECLARE_SHADER_ASSET(block_outline)
-MC_DECLARE_SHADER_ASSET(image_viewer)
+#define X(ns, _name, file_path) namespace Assets::Files { extern Asset file_path; }
+ALL_ASSETS
+#undef X
 
-MC_DECLARE_ASSET(Images, atlas, images_atlas_ppm)
+namespace Assets {
+    constexpr AssetToken invalid_asset = 0;
 
+    #define X(ns, name, _file_path) namespace ns { constexpr AssetToken name = UNIQUE_NUMBER; }
+    ALL_ASSETS
+    #undef X
 }
 
-// NOTE: We need to declare dependencies both in Assets.cpp and and here
-// in Assets.hpp but only because we rename the variable name from it's
-// file name to a more readable name. If we didn't do that we could just
-// declare the variable in the hpp file, without needing another compilation
-// unit at all.
-// This would work easily if there was a using x = y; directive that didn't
-// just work for types but for variables as well. But there isn't, I think.. :(
\ No newline at end of file
+constexpr Asset lookup(AssetToken token) {
+    switch (token) {
+        #define X(ns, name, file_path) case Assets::ns::name: return Assets::Files::file_path;
+        ALL_ASSETS
+        #undef X
+
+        default: UNREACHABLE("Invalid asset token.");
+    }
+}
+
+template <AssetToken T = Assets::invalid_asset>
+constexpr Asset asset(AssetToken t = Assets::invalid_asset) {
+    static_assert(T != Assets::invalid_asset, "Invalid asset token.");
+    ASSERT(t == Assets::invalid_asset || t == T, "Can't lookup asset both dynamically and statically at the same time.");
+    
+    return lookup(T);
+}
+
+template <> inline Asset asset<Assets::invalid_asset>(AssetToken token) {
+    return lookup(token);
+}
+
+}