aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api')
-rw-r--r--src/crepe/api/AssetManager.h62
-rw-r--r--src/crepe/api/Config.h11
-rw-r--r--src/crepe/api/Text.cpp0
-rw-r--r--src/crepe/api/Text.h35
4 files changed, 108 insertions, 0 deletions
diff --git a/src/crepe/api/AssetManager.h b/src/crepe/api/AssetManager.h
new file mode 100644
index 0000000..3b1cc4b
--- /dev/null
+++ b/src/crepe/api/AssetManager.h
@@ -0,0 +1,62 @@
+#pragma once
+
+#include <any>
+#include <memory>
+#include <string>
+#include <unordered_map>
+
+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:
+ AssetManager();
+ virtual ~AssetManager();
+
+public:
+ AssetManager(const AssetManager &) = delete;
+ AssetManager(AssetManager &&) = delete;
+ 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:
+ /**
+ * \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
+
+#include "AssetManager.hpp"
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index 6472270..def4c49 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -76,6 +76,17 @@ struct Config final {
*/
std::string root_pattern = ".crepe-root";
} asset;
+ //! Default font options
+ struct {
+ /**
+ * \brief Default font size
+ *
+ * using the SDL_ttf library the font size needs to be set when loading the font.
+ * This config option is the font size at which all fonts will be loaded initially.
+ *
+ */
+ int font_size = 16;
+ } font;
//! Audio system settings
struct {
diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/crepe/api/Text.cpp
diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h
new file mode 100644
index 0000000..9dd275b
--- /dev/null
+++ b/src/crepe/api/Text.h
@@ -0,0 +1,35 @@
+#pragma once
+
+#include <string>
+
+#include "../Component.h"
+
+#include "Asset.h"
+#include "Color.h"
+#include "UIObject.h"
+
+namespace crepe{
+class Text : public UIObject{
+public:
+ Text(game_object_id_t id,const vec2 & dimensions, const vec2 & offset, const Asset & font, int font_size);
+
+ //! Label text.
+ std::string text;
+ //! Label text color
+ Color text_color = Color::BLACK;
+ /**
+ * \brief fontsize for text rendering
+ *
+ * \note this is not the actual font size that is loaded in.
+ *
+ * Since SDL_TTF requires the font size when loading in the font it is not possible to switch the font size.
+ * The default font size that is loaded is set in the Config.
+ * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much.
+ */
+ int font_size = 16;
+
+ const Asset source;
+private:
+};
+
+} // namespace crepe