aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/AssetManager.cpp22
-rw-r--r--src/crepe/api/AssetManager.h47
-rw-r--r--src/crepe/api/CMakeLists.txt4
-rw-r--r--src/crepe/api/Sprite.cpp4
-rw-r--r--src/crepe/api/Sprite.h4
-rw-r--r--src/crepe/api/Texture.cpp2
6 files changed, 37 insertions, 46 deletions
diff --git a/src/crepe/api/AssetManager.cpp b/src/crepe/api/AssetManager.cpp
index 0ecdae5..f6cc369 100644
--- a/src/crepe/api/AssetManager.cpp
+++ b/src/crepe/api/AssetManager.cpp
@@ -1,25 +1,23 @@
-#include "resource_manager.h"
-#include <string>
-#include <unordered_map>
+#include "AssetManager.h"
+#include "util/log.h"
+
using namespace crepe::api;
-ResourceManager& ResourceManager::get_instance(){
- static ResourceManager instance;
+AssetManager& AssetManager::get_instance(){
+ static AssetManager instance;
return instance;
}
-ResourceManager::~ResourceManager(){
- m_resources.clear();
+AssetManager::~AssetManager(){
+ dbg_trace();
+ this->asset_cache.clear();
}
-
-void ResourceManager::Unload(const std::string& file_path){
- if(m_resources.find(file_path) != m_resources.end()){
- m_resources.erase(file_path);
- }
+AssetManager::AssetManager(){
+ dbg_trace();
}
diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h
index a646d95..1b8b86f 100644
--- a/src/crepe/api/AssetManager.h
+++ b/src/crepe/api/AssetManager.h
@@ -2,56 +2,49 @@
+#include <any>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
-#include "api/baseResource.h"
-
namespace crepe::api{
-class ResourceManager{
+class AssetManager{
private:
+ std::unordered_map< std::string, std::any> asset_cache;
- std::unordered_map< std::string, std::unique_ptr<BaseResource>> m_resources;
-
-
-protected:
- ResourceManager() = default;
- ~ResourceManager();
+private:
+ AssetManager();
+ virtual ~AssetManager();
public:
- ResourceManager(const ResourceManager &) = delete;
- ResourceManager(ResourceManager &&) = delete;
- ResourceManager &operator=(const ResourceManager &) = delete;
- ResourceManager &operator=(ResourceManager &&) = delete;
+ AssetManager(const AssetManager &) = delete;
+ AssetManager(AssetManager &&) = delete;
+ AssetManager &operator=(const AssetManager &) = delete;
+ AssetManager &operator=(AssetManager &&) = delete;
- static ResourceManager& get_instance();
+ static AssetManager& get_instance();
public:
- template<typename T>
- T* Load(const std::string& file_path){
-
- if (m_resources.find(file_path) != m_resources.end()) {
- return static_cast<T*>(m_resources[file_path].get());
- }
+ template<typename asset>
+ std::shared_ptr<asset> cache(const std::string& file_path, bool reload = false){
+ auto it = asset_cache.find(file_path);
- auto resource = std::make_unique<T>(file_path.c_str());
- if (resource) {
- m_resources[file_path] = std::move(resource);
- return static_cast<T*>(m_resources[file_path].get() );
+ if (!reload && it != asset_cache.end()) {
+ return std::any_cast<std::shared_ptr<asset>>(it->second);
}
- return nullptr;
- }
+ std::shared_ptr<asset> new_asset = std::make_shared<asset>(file_path.c_str());
- void Unload(const std::string& file_path);
+ asset_cache[file_path] = new_asset;
+ return new_asset;
+ }
};
}
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index b046301..d29a771 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -6,6 +6,7 @@ target_sources(crepe PUBLIC
Texture.cpp
Sprite.cpp
Transform.cpp
+ AssetManager.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
@@ -16,5 +17,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES
Transform.h
Color.h
Sprite.h
- Texture.h
+ Texture.h
+ AssetManager.h
)
diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp
index b0c0971..d9e26ab 100644
--- a/src/crepe/api/Sprite.cpp
+++ b/src/crepe/api/Sprite.cpp
@@ -10,8 +10,8 @@ using namespace std;
using namespace crepe;
using namespace crepe::api;
-Sprite::Sprite(unique_ptr<Texture> image, const Color & color,
- const flip_settings & flip) : color(color), flip(flip), sprite_image(std::move(image)) {
+Sprite::Sprite(shared_ptr<Texture> image, const Color & color,
+ const flip_settings & flip) : color(color), flip(flip), sprite_image(image) {
dbg_trace();
}
diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h
index 3dd9b4a..920f91e 100644
--- a/src/crepe/api/Sprite.h
+++ b/src/crepe/api/Sprite.h
@@ -17,9 +17,9 @@ struct flip_settings{
class Sprite : public Component {
public:
- Sprite(std::unique_ptr<Texture> image, const Color& color, const flip_settings& flip );
+ Sprite(std::shared_ptr<Texture> image, const Color& color, const flip_settings& flip );
~Sprite();
- std::unique_ptr<Texture> sprite_image;
+ std::shared_ptr<Texture> sprite_image;
Color color;
flip_settings flip;
uint8_t sorting_in_layer;
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp
index 2d170c3..ba06c6d 100644
--- a/src/crepe/api/Texture.cpp
+++ b/src/crepe/api/Texture.cpp
@@ -6,8 +6,6 @@
#include "Texture.h"
#include <SDL2/SDL_render.h>
-#include <iostream>
-#include <string>
using namespace crepe::api;