TrueType Textures are now loading properly.
This commit is contained in:
@ -18,6 +18,26 @@ TrueTypeLoader::TrueTypeLoader(const std::string name) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TrueTypeLoader::updateSync() {
|
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() {
|
void TrueTypeLoader::updateAsync() {
|
||||||
@ -25,6 +45,7 @@ void TrueTypeLoader::updateAsync() {
|
|||||||
state = TrueTypeLoaderState::ASYNC_LOADING;
|
state = TrueTypeLoaderState::ASYNC_LOADING;
|
||||||
|
|
||||||
// Load the data.
|
// Load the data.
|
||||||
|
this->loader.open();
|
||||||
size_t size = loader.getSize();
|
size_t size = loader.getSize();
|
||||||
buffer = new uint8_t[size];
|
buffer = new uint8_t[size];
|
||||||
|
|
||||||
@ -39,7 +60,6 @@ void TrueTypeLoader::updateAsync() {
|
|||||||
// Now close the asset loader
|
// Now close the asset loader
|
||||||
loader.close();
|
loader.close();
|
||||||
state = TrueTypeLoaderState::ASYNC_DONE;
|
state = TrueTypeLoaderState::ASYNC_DONE;
|
||||||
this->loaded = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<TrueTypeTexture> TrueTypeLoader::getTexture(
|
std::shared_ptr<TrueTypeTexture> TrueTypeLoader::getTexture(
|
||||||
@ -53,16 +73,25 @@ std::shared_ptr<TrueTypeTexture> TrueTypeLoader::getTexture(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create the texture.
|
// Create the texture.
|
||||||
auto texture = std::make_shared<TrueTypeTexture>(face, fontSize);
|
auto texture = std::make_shared<TrueTypeTexture>(fontSize);
|
||||||
textures[fontSize] = texture;
|
textures[fontSize] = texture;
|
||||||
|
if(this->loaded) texture->setFace(face);
|
||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
TrueTypeLoader::~TrueTypeLoader() {
|
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) {
|
if(buffer != nullptr) {
|
||||||
delete[] buffer;
|
delete[] buffer;
|
||||||
buffer = nullptr;
|
buffer = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
FT_Done_FreeType(fontLibrary);
|
|
||||||
}
|
}
|
@ -12,7 +12,9 @@ namespace Dawn {
|
|||||||
enum class TrueTypeLoaderState {
|
enum class TrueTypeLoaderState {
|
||||||
INITIAL,
|
INITIAL,
|
||||||
ASYNC_LOADING,
|
ASYNC_LOADING,
|
||||||
ASYNC_DONE
|
ASYNC_DONE,
|
||||||
|
SYNC_LOADING,
|
||||||
|
SYNC_DONE
|
||||||
};
|
};
|
||||||
|
|
||||||
class TrueTypeLoader : public AssetLoader {
|
class TrueTypeLoader : public AssetLoader {
|
||||||
|
@ -9,17 +9,21 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
TrueTypeTexture::TrueTypeTexture(FT_Face face, uint32_t fontSize) :
|
TrueTypeTexture::TrueTypeTexture(const uint32_t fontSize) :
|
||||||
face(face),
|
|
||||||
fontSize(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");
|
assertTrue(fontSize < 256, "Font size cannot be greater than 256");
|
||||||
|
|
||||||
texture = std::make_shared<Texture>();
|
|
||||||
|
|
||||||
// Set freetype font size prior to baking.
|
// Set freetype font size prior to baking.
|
||||||
if(FT_Set_Pixel_Sizes(face, 0, fontSize)) {
|
auto ret = FT_Set_Pixel_Sizes(face, 0, fontSize);
|
||||||
assertUnreachable("Failed to set font size");
|
if(ret != 0) {
|
||||||
|
assertUnreachable("Failed to set font size %i", ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the texture size
|
// Set the texture size
|
||||||
@ -126,5 +130,4 @@ struct TrueTypeCharacter TrueTypeTexture::getCharacterData(wchar_t c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TrueTypeTexture::~TrueTypeTexture() {
|
TrueTypeTexture::~TrueTypeTexture() {
|
||||||
FT_Done_Face(this->face);
|
|
||||||
}
|
}
|
@ -11,19 +11,27 @@
|
|||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class TrueTypeTexture final {
|
class TrueTypeTexture final {
|
||||||
public:
|
private:
|
||||||
FT_Face face;
|
FT_Face face;
|
||||||
std::shared_ptr<Texture> texture;
|
|
||||||
uint32_t fontSize;
|
uint32_t fontSize;
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::shared_ptr<Texture> texture;
|
||||||
std::unordered_map<wchar_t, struct TrueTypeCharacter> characterData;
|
std::unordered_map<wchar_t, struct TrueTypeCharacter> characterData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new New True Type Face Texture object
|
* Construct a new New True Type Face Texture object
|
||||||
*
|
*
|
||||||
* @param face The freetype face object.
|
* @param fontSize Size of the font.
|
||||||
* @param fontSize The font size to render at.
|
|
||||||
*/
|
*/
|
||||||
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.
|
* Returns the character data for the given character.
|
||||||
|
Reference in New Issue
Block a user