TrueType Textures are now loading properly.

This commit is contained in:
2023-12-06 08:52:16 -06:00
parent cafa6e7442
commit 7459dc6173
4 changed files with 59 additions and 17 deletions

View File

@ -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<TrueTypeTexture> TrueTypeLoader::getTexture(
@ -53,16 +73,25 @@ std::shared_ptr<TrueTypeTexture> TrueTypeLoader::getTexture(
}
// Create the texture.
auto texture = std::make_shared<TrueTypeTexture>(face, fontSize);
auto texture = std::make_shared<TrueTypeTexture>(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);
}

View File

@ -12,7 +12,9 @@ namespace Dawn {
enum class TrueTypeLoaderState {
INITIAL,
ASYNC_LOADING,
ASYNC_DONE
ASYNC_DONE,
SYNC_LOADING,
SYNC_DONE
};
class TrueTypeLoader : public AssetLoader {

View File

@ -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<Texture>();
}
void TrueTypeTexture::setFace(const FT_Face face) {
this->face = face;
assertTrue(fontSize < 256, "Font size cannot be greater than 256");
texture = std::make_shared<Texture>();
// 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);
}

View File

@ -11,19 +11,27 @@
namespace Dawn {
class TrueTypeTexture final {
public:
private:
FT_Face face;
std::shared_ptr<Texture> texture;
uint32_t fontSize;
public:
std::shared_ptr<Texture> texture;
std::unordered_map<wchar_t, struct TrueTypeCharacter> 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.