summaryrefslogtreecommitdiff
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
parent0a62361737652112a3dbace2c0f3f7fa025c4ca2 (diff)
asset manager first pass
-rw-r--r--CMakeLists.txt2
-rw-r--r--src/asset-manager.cpp83
-rw-r--r--src/asset-manager.hpp25
-rw-r--r--src/main.cpp2
4 files changed, 112 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f832232..8d39ab9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -16,6 +16,8 @@ set(PKE_SOURCE_FILES
src/macros.hpp
src/memory.hpp
src/memory.cpp
+ src/asset-manager.hpp
+ src/asset-manager.cpp
src/window.hpp
src/window.cpp
)
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)];
+}
+
diff --git a/src/asset-manager.hpp b/src/asset-manager.hpp
new file mode 100644
index 0000000..2354cba
--- /dev/null
+++ b/src/asset-manager.hpp
@@ -0,0 +1,25 @@
+#ifndef PKE_ASSET_MANAGER_HPP
+#define PKE_ASSET_MANAGER_HPP
+
+#include "macros.hpp"
+#include "memory.hpp"
+
+#include <cstdint>
+#include <cstring>
+#include <cassert>
+
+TypeSafeInt_H(AssetHandle, uint64_t, UINT64_MAX);
+
+struct Asset{
+ char key[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+ int64_t size = 0;
+ void *ptr = nullptr;
+};
+
+void AssetManagerInit();
+AssetHandle RegisterAsset(const void *data, int64_t size, const char *key);
+AssetHandle RegisterAsset(char *path);
+void DestroyAsset(AssetHandle assetHandle);
+const Asset *GetAsset(AssetHandle assetHandle);
+
+#endif /* PKE_ASSET_MANAGER_HPP */
diff --git a/src/main.cpp b/src/main.cpp
index cf70271..be70826 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2,6 +2,7 @@
#include <cstdio>
#include <exception>
+#include "asset-manager.hpp"
#include "window.hpp"
PKEWindowProperties windowProps{};
@@ -9,6 +10,7 @@ PKEWindowProperties windowProps{};
int main() {
printf("PKE ENTERING\n");
try {
+ AssetManagerInit();
CreateWindow(&windowProps);
} catch (const std::exception &exc) {
printf("EXCEPTION: %s\n", exc.what());