summaryrefslogtreecommitdiff
path: root/lib/assetManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/assetManager.cpp')
-rw-r--r--lib/assetManager.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/assetManager.cpp b/lib/assetManager.cpp
new file mode 100644
index 0000000..9c60f9c
--- /dev/null
+++ b/lib/assetManager.cpp
@@ -0,0 +1,60 @@
+#include "assetManager.hpp"
+
+#include <filesystem>
+#include <iostream>
+
+assetManager::assetManager() = default;
+
+assetManager::~assetManager()
+{
+ cleanup();
+}
+
+void assetManager::init(const std::string& dirPath)
+{
+ if (!std::filesystem::exists(dirPath) || !std::filesystem::is_directory(dirPath)) {
+ std::cerr << "assetmanager: directory not found: " << dirPath << std::endl;
+ return;
+ }
+
+ for (const auto& entry : std::filesystem::recursive_directory_iterator(dirPath)) {
+ if (entry.is_regular_file()) {
+ std::string ext = entry.path().extension().string();
+
+ if (ext == ".png" || ext == ".jpg" || ext == ".jpeg") {
+ std::string key = entry.path().stem().string();
+ std::string fullPath = entry.path().string();
+
+ Texture2D tex = LoadTexture(fullPath.c_str());
+
+ if (tex.id != 0) {
+ textures[key] = tex;
+ std::cout << "success loading texture: " << key << std::endl;
+ } else {
+ std::cerr << "failed to load texture: " << key << std::endl;
+ }
+ }
+ }
+ }
+}
+
+Texture2D assetManager::load(const std::string& texName)
+{
+ const auto tex = textures.find(texName);
+ if (tex != textures.end()) {
+ return tex->second;
+ }
+
+ std::cerr << "asset not found: " << texName << std::endl;
+ return Texture2D{0};
+}
+
+void assetManager::cleanup()
+{
+ for (auto& pair : textures) {
+ if (pair.second.id != 0) {
+ UnloadTexture(pair.second);
+ }
+ }
+ textures.clear();
+}