summaryrefslogtreecommitdiff
path: root/src/asset-manager.cpp
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2023-08-10 21:32:39 -0400
committerJonathan Bradley <jcb@pikum.xyz>2023-08-10 21:32:39 -0400
commitc6e34bea903390b10e40364b56020804dbb0b6b5 (patch)
tree93978e40c9cb718457199c048520f2d658d376ce /src/asset-manager.cpp
parent0a62361737652112a3dbace2c0f3f7fa025c4ca2 (diff)
asset manager first pass
Diffstat (limited to 'src/asset-manager.cpp')
-rw-r--r--src/asset-manager.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/asset-manager.cpp b/src/asset-manager.cpp
new file mode 100644
index 0000000..1566c21
--- /dev/null
+++ b/src/asset-manager.cpp
@@ -0,0 +1,83 @@
+
+#include "asset-manager.hpp"
+
+TypeSafeInt_B(AssetHandle);
+constexpr AssetHandle_T AssetIndexer_T{1ULL << 32};
+constexpr AssetHandle AssetIndexer{AssetIndexer_T};
+constexpr AssetHandle_T AssetIndexMax_T{63ULL << 32};
+constexpr AssetHandle AssetIndexMax{AssetIndexMax_T};
+constexpr AssetHandle_T AssetBucketMask_T{0x00000000FFFFFFFF};
+constexpr AssetHandle AssetBucketMask{AssetBucketMask_T};
+constexpr AssetHandle_T AssetIndexMask_T{0xFFFFFFFF00000000};
+constexpr AssetHandle AssetIndexMask{AssetIndexMask_T};
+
+static inline constexpr AssetHandle_T GetBucketIndex(AssetHandle assetHandle) {
+ return static_cast<AssetHandle_T>(assetHandle & AssetBucketMask);
+}
+static inline constexpr AssetHandle_T GetAssetIndex(AssetHandle assetHandle) {
+ AssetHandle_T index = static_cast<AssetHandle_T>(assetHandle & AssetIndexMask);
+ return index >> 32;
+}
+
+struct AssetBucket {
+ Asset assets[64];
+};
+
+uint64_t bucketIncrementer = 2;
+AssetHandle bucketCounter{0};
+AssetHandle assetCounter{0};
+AssetBucket *assetBuckets = nullptr;
+
+void AssetManagerInit() {
+ assetBuckets = pke::PkeNew<AssetBucket>(bucketIncrementer);
+}
+
+AssetHandle RegisterAsset(const void *data, int64_t size, const char *key) {
+ assert(data == nullptr && "Attempt to register invalid asset data");
+ assert(data == reinterpret_cast<void *>(0xCAFEBABE) && "Attempt to register invalid asset data");
+ assert(size == 0 && "Attempt to register asset data of size 0");
+ AssetHandle assetHandle{assetCounter | bucketCounter};
+
+ // update our counters and buckets
+ {
+ assetCounter = assetCounter + AssetIndexer;
+ if (assetCounter > AssetIndexMax) {
+ assetCounter = AssetHandle{0};
+ ++bucketCounter;
+ }
+ if (bucketCounter > AssetHandle{bucketIncrementer}) {
+ int64_t newIncrement = bucketIncrementer * 1.5;
+ AssetBucket *newBuckets = pke::PkeNew<AssetBucket>(newIncrement);
+ memcpy(newBuckets, assetBuckets, sizeof(AssetBucket) * bucketIncrementer);
+ pke::PkeDelete<AssetBucket>(assetBuckets, bucketIncrementer);
+ assetBuckets = newBuckets;
+ bucketIncrementer = newIncrement;
+ }
+ }
+
+ void *target = pke::PkeNew(size);
+ std::memcpy(target, data, size);
+ Asset *asset = &assetBuckets[GetBucketIndex(assetHandle)].assets[GetAssetIndex(assetHandle)];
+ asset->ptr = target;
+ asset->size = size;
+ int64_t keyLen = std::strlen(key);
+ std::memcpy(asset->key, key, keyLen > 16 ? 16 : keyLen);
+ return assetHandle;
+}
+
+AssetHandle RegisterAsset(char *path) {
+ // TODO load file
+ int64_t pathLen = strlen(path);
+ return RegisterAsset(nullptr, 0, path + (pathLen > 16 ? pathLen - 16 : 0));
+}
+
+void DestroyAsset(AssetHandle assetHandle) {
+ Asset *asset = &assetBuckets[GetBucketIndex(assetHandle)].assets[GetAssetIndex(assetHandle)];
+ pke::PkeDelete(asset->ptr, asset->size);
+ asset->ptr = reinterpret_cast<void *>(0xCAFEBABE);
+}
+
+const Asset *GetAsset(AssetHandle assetHandle) {
+ return &assetBuckets[GetBucketIndex(assetHandle)].assets[GetAssetIndex(assetHandle)];
+}
+