From ed3e91aead430b1a0975a920f6e99513717cf236 Mon Sep 17 00:00:00 2001 From: Sara Date: Mon, 27 Jan 2025 00:42:47 +0100 Subject: [PATCH] feat: added fonts --- src/core/assets/asset_db.cpp | 8 ++++++++ src/core/assets/asset_wrapper.cpp | 20 ++++++++++++++++++++ src/core/assets/asset_wrapper.h | 12 ++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/core/assets/asset_db.cpp b/src/core/assets/asset_db.cpp index 615faa6..b77828c 100644 --- a/src/core/assets/asset_db.cpp +++ b/src/core/assets/asset_db.cpp @@ -42,6 +42,14 @@ void AssetDB::index_assets() { } else { this->assets.insert({name, std::make_shared(itr.path())}); } + } else if(itr.path().extension() == ".ttf") { + std::filesystem::path name{itr.path().filename()}; + name.replace_extension(); + if(this->assets.contains(name)) { + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to add asset %s, asset by that name is already indexed", name.c_str()); + } else { + this->assets.insert({name, std::make_shared(itr.path())}); + } } } } diff --git a/src/core/assets/asset_wrapper.cpp b/src/core/assets/asset_wrapper.cpp index 97c745e..2b9b972 100644 --- a/src/core/assets/asset_wrapper.cpp +++ b/src/core/assets/asset_wrapper.cpp @@ -2,6 +2,7 @@ #include "core/canvas_engine.hpp" #include #include +#include #include namespace ce { @@ -30,4 +31,23 @@ bool Texture::is_loaded() const { SDL_Texture *Texture::get() { return this->texture; } + +Font::Font(std::filesystem::path const &path) : Asset(path) {} + +Font::~Font() { + this->unload(); +} + +void Font::load() { + this->font = TTF_OpenFont(this->path.c_str(), 32); +} + +void Font::unload() { + TTF_CloseFont(this->font); + this->font = nullptr; +} + +bool Font::is_loaded() const { + return this->font != nullptr; +} } diff --git a/src/core/assets/asset_wrapper.h b/src/core/assets/asset_wrapper.h index d086b31..ac55cd3 100644 --- a/src/core/assets/asset_wrapper.h +++ b/src/core/assets/asset_wrapper.h @@ -3,6 +3,7 @@ #include typedef struct SDL_Texture SDL_Texture; +typedef struct _TTF_Font TTF_Font; namespace ce { class Asset { @@ -28,6 +29,17 @@ public: virtual bool is_loaded() const override; SDL_Texture *get(); }; + +class Font : public Asset { + TTF_Font *font{nullptr}; +public: + Font(std::filesystem::path const &path); + ~Font(); + virtual void load() override; + virtual void unload() override; + virtual bool is_loaded() const override; + TTF_Font *get(); +}; } #endif // !CANVAS_ASSET_WRAPPER_HPP