diff --git a/src/dawn/asset/loaders/TrueTypeLoader.cpp b/src/dawn/asset/loaders/TrueTypeLoader.cpp index c94ec4cd..3d7a06b4 100644 --- a/src/dawn/asset/loaders/TrueTypeLoader.cpp +++ b/src/dawn/asset/loaders/TrueTypeLoader.cpp @@ -18,6 +18,26 @@ TrueTypeLoader::TrueTypeLoader(const std::string name) : } void TrueTypeLoader::updateSync() { + if(state != TrueTypeLoaderState::ASYNC_DONE) return; + state = TrueTypeLoaderState::SYNC_LOADING; + + // Init all the textures. + auto it = textures.begin(); + while(it != textures.end()) { + auto texture = it->second.lock(); + + if(texture) { + texture->setFace(face); + it++; + continue; + } + + it = textures.erase(it); + } + + // Done + state = TrueTypeLoaderState::SYNC_DONE; + this->loaded = true; } void TrueTypeLoader::updateAsync() { @@ -25,6 +45,7 @@ void TrueTypeLoader::updateAsync() { state = TrueTypeLoaderState::ASYNC_LOADING; // Load the data. + this->loader.open(); size_t size = loader.getSize(); buffer = new uint8_t[size]; @@ -39,7 +60,6 @@ void TrueTypeLoader::updateAsync() { // Now close the asset loader loader.close(); state = TrueTypeLoaderState::ASYNC_DONE; - this->loaded = true; } std::shared_ptr TrueTypeLoader::getTexture( @@ -53,16 +73,25 @@ std::shared_ptr TrueTypeLoader::getTexture( } // Create the texture. - auto texture = std::make_shared(face, fontSize); + auto texture = std::make_shared(fontSize); textures[fontSize] = texture; + if(this->loaded) texture->setFace(face); return texture; } TrueTypeLoader::~TrueTypeLoader() { + if( + this->state == TrueTypeLoaderState::SYNC_DONE || + this->state == TrueTypeLoaderState::SYNC_LOADING || + this->state == TrueTypeLoaderState::ASYNC_DONE + ) { + FT_Done_Face(face); + } + + FT_Done_FreeType(fontLibrary); + if(buffer != nullptr) { delete[] buffer; buffer = nullptr; } - - FT_Done_FreeType(fontLibrary); } \ No newline at end of file diff --git a/src/dawn/asset/loaders/TrueTypeLoader.hpp b/src/dawn/asset/loaders/TrueTypeLoader.hpp index 78198fce..8735dab4 100644 --- a/src/dawn/asset/loaders/TrueTypeLoader.hpp +++ b/src/dawn/asset/loaders/TrueTypeLoader.hpp @@ -12,7 +12,9 @@ namespace Dawn { enum class TrueTypeLoaderState { INITIAL, ASYNC_LOADING, - ASYNC_DONE + ASYNC_DONE, + SYNC_LOADING, + SYNC_DONE }; class TrueTypeLoader : public AssetLoader { diff --git a/src/dawn/display/font/TrueTypeTexture.cpp b/src/dawn/display/font/TrueTypeTexture.cpp index c9915634..7b85f745 100644 --- a/src/dawn/display/font/TrueTypeTexture.cpp +++ b/src/dawn/display/font/TrueTypeTexture.cpp @@ -9,17 +9,21 @@ using namespace Dawn; -TrueTypeTexture::TrueTypeTexture(FT_Face face, uint32_t fontSize) : - face(face), +TrueTypeTexture::TrueTypeTexture(const uint32_t fontSize) : fontSize(fontSize) { + assertTrue(fontSize > 0, "Font size cannot be zero"); + texture = std::make_shared(); +} + +void TrueTypeTexture::setFace(const FT_Face face) { + this->face = face; assertTrue(fontSize < 256, "Font size cannot be greater than 256"); - texture = std::make_shared(); - // Set freetype font size prior to baking. - if(FT_Set_Pixel_Sizes(face, 0, fontSize)) { - assertUnreachable("Failed to set font size"); + auto ret = FT_Set_Pixel_Sizes(face, 0, fontSize); + if(ret != 0) { + assertUnreachable("Failed to set font size %i", ret); } // Set the texture size @@ -126,5 +130,4 @@ struct TrueTypeCharacter TrueTypeTexture::getCharacterData(wchar_t c) { } TrueTypeTexture::~TrueTypeTexture() { - FT_Done_Face(this->face); } \ No newline at end of file diff --git a/src/dawn/display/font/TrueTypeTexture.hpp b/src/dawn/display/font/TrueTypeTexture.hpp index b95ba1be..169d8e61 100644 --- a/src/dawn/display/font/TrueTypeTexture.hpp +++ b/src/dawn/display/font/TrueTypeTexture.hpp @@ -11,19 +11,27 @@ namespace Dawn { class TrueTypeTexture final { - public: + private: FT_Face face; - std::shared_ptr texture; uint32_t fontSize; + + public: + std::shared_ptr texture; std::unordered_map characterData; /** * Construct a new New True Type Face Texture object * - * @param face The freetype face object. - * @param fontSize The font size to render at. + * @param fontSize Size of the font. */ - TrueTypeTexture(FT_Face face, uint32_t fontSize); + TrueTypeTexture(const uint32_t fontSize); + + /** + * Sets the face for this texture. + * + * @param face Face to set. + */ + void setFace(const FT_Face face); /** * Returns the character data for the given character.