aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/AssetManager.h35
-rw-r--r--src/crepe/facade/SDLContext.cpp2
-rw-r--r--src/example/rendering.cpp11
3 files changed, 39 insertions, 9 deletions
diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h
index fefbed9..dbfaef3 100644
--- a/src/crepe/api/AssetManager.h
+++ b/src/crepe/api/AssetManager.h
@@ -7,9 +7,19 @@
namespace crepe {
+/**
+ * \brief The AssetManager is responsible for storing and managing assets over
+ * multiple scenes.
+ *
+ * The AssetManager ensures that assets are loaded once and can be accessed
+ * across different scenes. It caches assets to avoid reloading them every time
+ * a scene is loaded. Assets are retained in memory until the AssetManager is
+ * destroyed, at which point the cached assets are cleared.
+ */
class AssetManager {
private:
+ //! A cache that holds all the assets, accessible by their file path, over multiple scenes.
std::unordered_map<std::string, std::any> asset_cache;
private:
@@ -22,12 +32,31 @@ public:
AssetManager & operator=(const AssetManager &) = delete;
AssetManager & operator=(AssetManager &&) = delete;
+ /**
+ * \brief Retrieves the singleton instance of the AssetManager.
+ *
+ * \return A reference to the single instance of the AssetManager.
+ */
static AssetManager & get_instance();
public:
- template <typename asset>
- std::shared_ptr<asset> cache(const std::string & file_path,
- bool reload = false);
+ /**
+ * \brief Caches an asset by loading it from the given file path.
+ *
+ * \param file_path The path to the asset file to load.
+ * \param reload If true, the asset will be reloaded from the file, even if
+ * it is already cached.
+ * \tparam T The type of asset to cache (e.g., texture, sound, etc.).
+ *
+ * \return A shared pointer to the cached asset.
+ *
+ * This template function caches the asset at the given file path. If the
+ * asset is already cached and `reload` is false, the existing cached version
+ * will be returned. Otherwise, the asset will be reloaded and added to the
+ * cache.
+ */
+ template <typename T>
+ std::shared_ptr<T> cache(const std::string & file_path, bool reload = false);
};
} // namespace crepe
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 1cb2936..aa15ef6 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -159,7 +159,7 @@ SDLContext::texture_from_path(const std::string & path) {
SDL_Surface * tmp = IMG_Load(path.c_str());
if (tmp == nullptr) {
- throw Exception("surface cannot be load from {}", path);
+ tmp = IMG_Load("../asset/texture/ERROR.png");
}
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>
diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp
index cbd7de7..ae21f9a 100644
--- a/src/example/rendering.cpp
+++ b/src/example/rendering.cpp
@@ -1,3 +1,4 @@
+#include "api/Camera.h"
#include <crepe/ComponentManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/system/RenderSystem.h>
@@ -23,26 +24,26 @@ int main() {
auto obj1 = GameObject(1, "name", "tag", Vector2{500, 0}, 1, 0.1);
auto obj2 = GameObject(2, "name", "tag", Vector2{800, 0}, 1, 0.1);
- auto & mgr = AssetManager::get_instance();
// Normal adding components
{
Color color(0, 0, 0, 0);
obj.add_component<Sprite>(
make_shared<Texture>("../asset/texture/img.png"), color,
- FlipSettings{true, true});
+ FlipSettings{false, false});
+ obj.add_component<Camera>(Color::get_red());
}
-
{
Color color(0, 0, 0, 0);
- auto img = mgr.cache<Texture>("../asset/texture/second.png");
- obj1.add_component<Sprite>(img, color, FlipSettings{true, true});
+ obj1.add_component<Sprite>(make_shared<Texture>("../asset/texture/second.png"), color, FlipSettings{true, true});
}
+ /*
{
Color color(0, 0, 0, 0);
auto img = mgr.cache<Texture>("../asset/texture/second.png");
obj2.add_component<Sprite>(img, color, FlipSettings{true, true});
}
+ */
auto & sys = crepe::RenderSystem::get_instance();
auto start = std::chrono::steady_clock::now();