feat: added fonts

main
Sara 2025-01-27 00:42:47 +01:00
parent b294a76cc9
commit ed3e91aead
3 changed files with 40 additions and 0 deletions

View File

@ -42,6 +42,14 @@ void AssetDB::index_assets() {
} else {
this->assets.insert({name, std::make_shared<Texture>(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<Font>(itr.path())});
}
}
}
}

View File

@ -2,6 +2,7 @@
#include "core/canvas_engine.hpp"
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_ttf.h>
#include <filesystem>
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;
}
}

View File

@ -3,6 +3,7 @@
#include <filesystem>
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