diff --git a/src/dawn/asset/AssetManager.hpp b/src/dawn/asset/AssetManager.hpp index d4eccc36..c88e13d3 100644 --- a/src/dawn/asset/AssetManager.hpp +++ b/src/dawn/asset/AssetManager.hpp @@ -12,6 +12,7 @@ #include "assets/TextureAsset.hpp" #include "assets/TilesetAsset.hpp" #include "assets/TrueTypeAsset.hpp" +#include "assets/NewTrueTypeAsset.hpp" namespace Dawn { class AssetManager { diff --git a/src/dawn/asset/assets/CMakeLists.txt b/src/dawn/asset/assets/CMakeLists.txt index 6a1e322e..29e8e3b5 100644 --- a/src/dawn/asset/assets/CMakeLists.txt +++ b/src/dawn/asset/assets/CMakeLists.txt @@ -11,4 +11,5 @@ target_sources(${DAWN_TARGET_NAME} TextureAsset.cpp TilesetAsset.cpp TrueTypeAsset.cpp + NewTrueTypeAsset.cpp ) \ No newline at end of file diff --git a/src/dawn/asset/assets/NewTrueTypeAsset.cpp b/src/dawn/asset/assets/NewTrueTypeAsset.cpp index 0800c8e2..732d9abd 100644 --- a/src/dawn/asset/assets/NewTrueTypeAsset.cpp +++ b/src/dawn/asset/assets/NewTrueTypeAsset.cpp @@ -4,6 +4,7 @@ // https://opensource.org/licenses/MIT #include "NewTrueTypeAsset.hpp" +#include "asset/AssetManager.hpp" using namespace Dawn; @@ -21,20 +22,19 @@ void NewTrueTypeAsset::updateSync() { void NewTrueTypeAsset::updateAsync() { if(this->state != NEW_TRUE_TYPE_ASSET_STATE_INITIAL) return; - this->state = NEW_TRUE_TYPE_ASSET_STATE_OPEN; this->loader.open(); this->state = NEW_TRUE_TYPE_ASSET_STATE_VALIDATE_HEADER; - char buffer[1024]; + uint8_t buffer[64]; this->loader.rewind(); size_t read = this->loader.read(buffer, sizeof(char) * 6); assertTrue(read == (6 * sizeof(char))); buffer[6] = '\0'; // Confirm "DE_TTF" - assertTrue(std::string(buffer) == "DE_TTF"); + assertTrue(std::string((char *)buffer) == "DE_TTF"); // Vertical bar this->loader.read(buffer, 1); @@ -50,6 +50,7 @@ void NewTrueTypeAsset::updateAsync() { assertTrue(buffer[4] == '|'); // Read the count of font styles / variants. + size_t styleListBegin = this->loader.getPosition(); this->state = NEW_TRUE_TYPE_ASSET_STATE_READ_VARIANT_COUNT; read = this->loader.read(buffer, 64); assertTrue(read > 0); @@ -58,16 +59,121 @@ void NewTrueTypeAsset::updateAsync() { size_t i = 0; while(buffer[i] != '|' && i < 64) i++; assertTrue(buffer[i] == '|'); + styleListBegin += i + 1; buffer[i] = '\0'; - int32_t count = atoi(buffer); + int32_t count = atoi((char *)buffer); assertTrue(count > 0); - // Now begin parsing each font style - while(true) { + // Now begin parsing each font style. + this->state = NEW_TRUE_TYPE_ASSET_STATE_READ_VARIANT; + assetStyles.clear(); + while(assetStyles.size() != count) { + struct NewTrueTypeAssetStyle style; + + // Buffer + this->loader.rewind(); + this->loader.setPosition(styleListBegin); + read = this->loader.read(buffer, 32); + assertTrue(read == 32); + + // Read style + i = 0; + while(buffer[i] != ':' && i < 64) i++; + buffer[i] = '\0'; + style.style = atoi((char *)buffer); + styleListBegin += i + 1; + // Buffer + this->loader.rewind(); + this->loader.setPosition(styleListBegin); + read = this->loader.read(buffer, 32); + assertTrue(read == 32); + + // Read length + i = 0; + while(buffer[i] != '|' && i < 64) i++; + buffer[i] = '\0'; + styleListBegin += i + 1; + style.dataSize = atol((char *)buffer); + + // Push + assetStyles.push_back(style); } + + // Now we are at the first byte of the first style. + this->state = NEW_TRUE_TYPE_ASSET_STATE_ADJUSTING_OFFSETS; + auto itStyle = assetStyles.begin(); + while(itStyle != assetStyles.end()) { + (*itStyle).dataOffset = styleListBegin; + styleListBegin += (*itStyle).dataSize; + styleListBegin += 1; // Vertical bar + ++itStyle; + } + + // Init FreeType + this->state = NEW_TRUE_TYPE_ASSET_STATE_INIT_FREETYPE; + int32_t ret = FT_Init_FreeType(&fontLibrary); + assertTrue(ret == 0); + + // Convert to truetype faces + this->state = NEW_TRUE_TYPE_ASSET_STATE_CONVERTING_TO_FACES; + itStyle = assetStyles.begin(); + while(itStyle != assetStyles.end()) { + struct NewTrueTypeAssetStyle &style = (*itStyle); + + uint8_t *dataBuffer = (uint8_t*)memoryAllocate(sizeof(uint8_t) * style.dataSize); + this->loader.rewind(); + this->loader.setPosition(style.dataOffset); + read = this->loader.read(dataBuffer, style.dataSize); + assertTrue(read == style.dataSize); + + // Create the face + NewTrueTypeFace *face = new NewTrueTypeFace(); + ret = FT_New_Memory_Face(this->fontLibrary, (FT_Byte*)dataBuffer, style.dataSize, 0, &face->face); + memoryFree(dataBuffer); + assertTrue(ret == 0); + + // Push + this->faces[style.style] = face; + ++itStyle; + } + + // Done parsing! + this->state = NEW_TRUE_TYPE_ASSET_STATE_READY; + this->loaded = true; +} + +usagelockid_t NewTrueTypeAsset::lock(struct NewTrueTypeFaceTextureStyle style) { + // Try find style + auto it = this->faces.find(style.style); + assertTrue(it != this->faces.end()); + + auto lock = it->second->lock(style); + this->locks[lock] = style; + return lock; +} + +NewTrueTypeFaceTexture * NewTrueTypeAsset::getTexture(usagelockid_t id) { + auto existing = this->locks.find(id); + assertTrue(existing != this->locks.end()); + return this->faces[existing->second.style]->getTexture(id); +} + +void NewTrueTypeAsset::unlock(usagelockid_t id) { + auto existing = this->locks.find(id); + assertTrue(existing != this->locks.end()); + this->faces[existing->second.style]->unlock(id); + this->locks.erase(existing); } NewTrueTypeAsset::~NewTrueTypeAsset() { + auto it = this->faces.begin(); + while(it != this->faces.end()) { + FT_Done_Face(it->second->face); + delete it->second; + it++; + } + + FT_Done_FreeType(this->fontLibrary); } \ No newline at end of file diff --git a/src/dawn/asset/assets/NewTrueTypeAsset.hpp b/src/dawn/asset/assets/NewTrueTypeAsset.hpp index f710fc30..3908c327 100644 --- a/src/dawn/asset/assets/NewTrueTypeAsset.hpp +++ b/src/dawn/asset/assets/NewTrueTypeAsset.hpp @@ -6,26 +6,55 @@ #pragma once #include "../Asset.hpp" #include "../AssetLoader.hpp" +#include "util/flag.hpp" +#include "display/font/truetype/NewTrueTypeFace.hpp" namespace Dawn { enum NewTrueTypeAssetState { NEW_TRUE_TYPE_ASSET_STATE_INITIAL, NEW_TRUE_TYPE_ASSET_STATE_OPEN, - // NEW_TRUE_TYPE_ASSET_STATE_LOAD_RAW, NEW_TRUE_TYPE_ASSET_STATE_VALIDATE_HEADER, NEW_TRUE_TYPE_ASSET_STATE_VALIDATE_VERSION, NEW_TRUE_TYPE_ASSET_STATE_READ_VARIANT_COUNT, - } + NEW_TRUE_TYPE_ASSET_STATE_READ_VARIANT, + NEW_TRUE_TYPE_ASSET_STATE_ADJUSTING_OFFSETS, + NEW_TRUE_TYPE_ASSET_STATE_INIT_FREETYPE, + NEW_TRUE_TYPE_ASSET_STATE_CONVERTING_TO_FACES, + NEW_TRUE_TYPE_ASSET_STATE_READY + }; + + struct NewTrueTypeAssetStyle { + flag_t style; + size_t dataSize; + size_t dataOffset; + }; class NewTrueTypeAsset : public Asset { protected: AssetLoader loader; + FT_Library fontLibrary; enum NewTrueTypeAssetState state = NEW_TRUE_TYPE_ASSET_STATE_INITIAL; + std::vector assetStyles; + std::map faces; + std::map locks; public: NewTrueTypeAsset(AssetManager *assMan, std::string name); void updateSync() override; void updateAsync() override; + + /** + * Locks a specific style for use later. + * + * @param style Style you want to lock. + * @return A unique lock ID for this style. + */ + usagelockid_t lock(struct NewTrueTypeFaceTextureStyle style); + + NewTrueTypeFaceTexture * getTexture(usagelockid_t lock); + + void unlock(usagelockid_t lock); + ~NewTrueTypeAsset(); }; } \ No newline at end of file diff --git a/src/dawn/display/_RenderManager.hpp b/src/dawn/display/_RenderManager.hpp index 4a1f4575..e3a40b05 100644 --- a/src/dawn/display/_RenderManager.hpp +++ b/src/dawn/display/_RenderManager.hpp @@ -15,7 +15,6 @@ namespace Dawn { class DawnGame; class RenderPipeline; class ShaderManager; - class FontManager; class IRenderManager { protected: @@ -56,13 +55,6 @@ namespace Dawn { * @return Reference to the shader manager. */ virtual ShaderManager * getShaderManager() = 0; - - /** - * Returns the font manager that this render manager uses. - * - * @return Reference ot the font manager. - */ - virtual FontManager * getFontManager() = 0; /** * Sets the render flags for the render manager to use. diff --git a/src/dawn/display/font/CMakeLists.txt b/src/dawn/display/font/CMakeLists.txt index 67c04ee1..240a823a 100644 --- a/src/dawn/display/font/CMakeLists.txt +++ b/src/dawn/display/font/CMakeLists.txt @@ -6,7 +6,6 @@ # Sources target_sources(${DAWN_TARGET_NAME} PRIVATE - FontManager.cpp BitmapFont.cpp ExampleFont.cpp TrueTypeFont.cpp diff --git a/src/dawn/display/font/FontManager.cpp b/src/dawn/display/font/FontManager.cpp deleted file mode 100644 index 02b6b660..00000000 --- a/src/dawn/display/font/FontManager.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "FontManager.hpp" - -using namespace Dawn; - -FontManager::FontManager() { - -} - -void FontManager::init() { - // Init FreeType - if(FT_Init_FreeType(&fontLibrary)) { - std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl; - assertUnreachable(); - } -} - -FontManager::~FontManager() { - -} \ No newline at end of file diff --git a/src/dawn/display/font/FontManager.hpp b/src/dawn/display/font/FontManager.hpp deleted file mode 100644 index 0de6612f..00000000 --- a/src/dawn/display/font/FontManager.hpp +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "dawnlibs.hpp" -#include "assert/assert.hpp" - -namespace Dawn { - class FontManager { - protected: - - public: - FT_Library fontLibrary; - - FontManager(); - - void init(); - - ~FontManager(); - }; -} \ No newline at end of file diff --git a/src/dawn/display/font/truetype/CMakeLists.txt b/src/dawn/display/font/truetype/CMakeLists.txt index 1254bf78..bb9f9bfa 100644 --- a/src/dawn/display/font/truetype/CMakeLists.txt +++ b/src/dawn/display/font/truetype/CMakeLists.txt @@ -6,7 +6,6 @@ # Sources target_sources(${DAWN_TARGET_NAME} PRIVATE - NewTrueType.cpp NewTrueTypeFace.cpp - NewTrueTypeFaceTexture.hpp + NewTrueTypeFaceTexture.cpp ) \ No newline at end of file diff --git a/src/dawn/display/font/truetype/NewTrueType.cpp b/src/dawn/display/font/truetype/NewTrueType.cpp deleted file mode 100644 index 5fe7d97e..00000000 --- a/src/dawn/display/font/truetype/NewTrueType.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#include "NewTrueType.hpp" - -using namespace Dawn; - -NewTrueType::NewTrueType(FontManager *fontMan) { - assertNotNull(fontMan); - this->fontManager = fontMan; -} - -void NewTrueType::addStyle(flag_t style) { - assertTrue(this->faces.find(style) == this->faces.end()); - - //Create the face - this->faces[style] = new NewTrueTypeFace(); -} - -NewTrueType::~NewTrueType() { - auto it = this->faces.begin(); - while(it != this->faces.end()) { - delete it->second; - it++; - } -} \ No newline at end of file diff --git a/src/dawn/display/font/truetype/NewTrueType.hpp b/src/dawn/display/font/truetype/NewTrueType.hpp deleted file mode 100644 index 25866f06..00000000 --- a/src/dawn/display/font/truetype/NewTrueType.hpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2023 Dominic Masters -// -// This software is released under the MIT License. -// https://opensource.org/licenses/MIT - -#pragma once -#include "NewTrueTypeFace.hpp" -#include "display/font/FontManager.hpp" - -namespace Dawn { - class NewTrueType { - protected: - std::map faces; - FontManager *fontManager; - - public: - NewTrueType(FontManager *fontMan); - void addStyle(flag_t style); - NewTrueTypeFace * getFace(flag_t style); - ~NewTrueType(); - - friend class NewTrueTypeFace; - }; -} \ No newline at end of file diff --git a/src/dawn/display/font/truetype/NewTrueTypeFace.cpp b/src/dawn/display/font/truetype/NewTrueTypeFace.cpp index 6ed22143..f779214c 100644 --- a/src/dawn/display/font/truetype/NewTrueTypeFace.cpp +++ b/src/dawn/display/font/truetype/NewTrueTypeFace.cpp @@ -3,7 +3,7 @@ // This software is released under the MIT License. // https://opensource.org/licenses/MIT -#include "NewTrueType.hpp" +#include "asset/assets/NewTrueTypeAsset.hpp" using namespace Dawn; @@ -15,17 +15,10 @@ usagelockid_t NewTrueTypeFace::lock(struct NewTrueTypeFaceTextureStyle style) { NewTrueTypeFaceTexture *texture = nullptr; auto existing = this->texturesByStyle.find(style); if(existing == this->texturesByStyle.end()) { - // Load face - FT_Face face; - assertUnreachable(); - // Does not exist, create it. texture = new NewTrueTypeFaceTexture(this, face, style); this->textures.push_back(texture); this->texturesByStyle[style] = texture; - - // Cleanup face - FT_Done_Face(face); } else { texture = existing->second; } @@ -48,6 +41,7 @@ NewTrueTypeFaceTexture * NewTrueTypeFace::getTexture(usagelockid_t lock) { void NewTrueTypeFace::unlock(usagelockid_t lock) { auto texture = this->getTexture(lock); + assertNotNull(texture); texture->locks.removeLock(lock); this->stylesByLock.erase(lock); auto it = std::find(this->locksByStyle[texture->style].begin(), this->locksByStyle[texture->style].end(), lock); @@ -59,6 +53,5 @@ NewTrueTypeFace::~NewTrueTypeFace() { auto it = this->textures.begin(); while(it != this->textures.end()) { delete *it; - it = this->textures.erase(it); } } \ No newline at end of file diff --git a/src/dawn/display/font/truetype/NewTrueTypeFace.hpp b/src/dawn/display/font/truetype/NewTrueTypeFace.hpp index d505cc0b..e32f2488 100644 --- a/src/dawn/display/font/truetype/NewTrueTypeFace.hpp +++ b/src/dawn/display/font/truetype/NewTrueTypeFace.hpp @@ -8,13 +8,13 @@ namespace Dawn { class NewTrueTypeFace { - protected: + public: std::vector textures; std::map texturesByStyle; std::map> locksByStyle; std::map stylesByLock; + FT_Face face; - public: /** * Create a new TrueTypeFace object. TrueType face holds multiple textures * of various styles of this face. This face may be "Arial Bold" and have @@ -51,7 +51,5 @@ namespace Dawn { * Destroys this TrueTypeFace object, and all textures associated with it. */ ~NewTrueTypeFace(); - - friend class NewTrueType; }; } \ No newline at end of file diff --git a/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.cpp b/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.cpp index 7c964c9f..ca237a04 100644 --- a/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.cpp +++ b/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.cpp @@ -3,7 +3,7 @@ // This software is released under the MIT License. // https://opensource.org/licenses/MIT -#include "NewTrueTypeFaceTexture.hpp" +#include "NewTrueTypeFace.hpp" using namespace Dawn; @@ -13,11 +13,15 @@ NewTrueTypeFaceTexture::NewTrueTypeFaceTexture( struct NewTrueTypeFaceTextureStyle style ) { assertNotNull(trueTypeFace); - assertTrue(this->style.fontSize < 256); + assertTrue(style.fontSize < 256); this->trueTypeFace = trueTypeFace; this->face = &face; this->style = style; + + this->locks.onEmpty = [&]() { + delete this; + }; // Set freetype font size prior to baking. if(FT_Set_Pixel_Sizes(face, 0, this->style.fontSize)) { @@ -85,4 +89,14 @@ NewTrueTypeFaceTexture::NewTrueTypeFaceTexture( struct NewTrueTypeCharacter NewTrueTypeFaceTexture::getCharacterData(FT_ULong c) { return this->characterData[c]; +} + +NewTrueTypeFaceTexture::~NewTrueTypeFaceTexture() { + auto it = std::find( + this->trueTypeFace->textures.begin(), + this->trueTypeFace->textures.end(), + this + ); + assertTrue(it != this->trueTypeFace->textures.end()); + this->trueTypeFace->textures.erase(it); } \ No newline at end of file diff --git a/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.hpp b/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.hpp index f012c95b..6ecd485a 100644 --- a/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.hpp +++ b/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.hpp @@ -11,7 +11,6 @@ namespace Dawn { class NewTrueTypeFace; - class NewTrueType; typedef uint32_t newtruetypelock_t; @@ -26,17 +25,19 @@ namespace Dawn { struct NewTrueTypeFaceTextureStyle { uint32_t fontSize; flag_t style; + + bool operator < (const struct NewTrueTypeFaceTextureStyle& r) const { + return std::tie(fontSize, style) < std::tie(r.fontSize, r.style); + } }; class NewTrueTypeFaceTexture { - protected: + public: FT_Face *face; NewTrueTypeFace *trueTypeFace; std::map characterData; struct NewTrueTypeFaceTextureStyle style; UsageLock locks; - - public: Texture texture; /** @@ -66,6 +67,5 @@ namespace Dawn { ~NewTrueTypeFaceTexture(); friend class NewTrueTypeFace; - friend class NewTrueType; }; } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UILabelNew.cpp b/src/dawn/scene/components/ui/UILabelNew.cpp index e0a80e87..77e05691 100644 --- a/src/dawn/scene/components/ui/UILabelNew.cpp +++ b/src/dawn/scene/components/ui/UILabelNew.cpp @@ -8,87 +8,94 @@ using namespace Dawn; -UILabelNew::UILabelNew(SceneItem *item) : UIComponentRenderable(item) { +UILabelNew::UILabelNew(SceneItem *item) : + UIComponentRenderable(item), + font(nullptr), + fontSize(16), + style(0) +{ } +void UILabelNew::rebufferQuads() { + this->mesh.createBuffers(QUAD_VERTICE_COUNT * 128, QUAD_INDICE_COUNT * 128); + + auto texture = this->font->getTexture(this->fontLock); + texture. +} + void UILabelNew::onStart() { this->shaderBuffer.init(); - if(FT_New_Face( - getGame()->renderManager.getFontManager()->fontLibrary, - "/usr/share/fonts/TTF/arial.ttf", - // "C:\\Windows\\Fonts\\arial.ttf", - 0, - &face - )) { - assertUnreachable(); - } - UILabelNew::bakeTexture(defFace, face, 32); - + useEffect([&]{ + if(fontLock != -1) { + font.previous->unlock(fontLock); + fontLock = -1; + } - if(FT_New_Face( - getGame()->renderManager.getFontManager()->fontLibrary, - "/usr/share/fonts/TTF/arialbd.ttf", - // "C:\\Windows\\Fonts\\arialbd.ttf", - 0, - &faceBold - )) { - assertUnreachable(); - } - UILabelNew::bakeTexture(defFaceBold, faceBold, 32); + if(font == nullptr) return; + fontLock = font->lock(NewTrueTypeFaceTextureStyle{ + fontSize, + style + }); + }, font); - if(FT_New_Face( - getGame()->renderManager.getFontManager()->fontLibrary, - "/usr/share/fonts/TTF/ariali.ttf", - // "C:\\Windows\\Fonts\\ariali.ttf", - 0, - &faceItalics - )) { - assertUnreachable(); - } - UILabelNew::bakeTexture(defFaceItalics, faceItalics, 32); + useEffect([&]{ + if(font == nullptr) return; - struct FontShaderBufferData fontData; - glm::vec2 position = glm::vec2(32, 32); + if(fontLock != -1) { + font->unlock(fontLock); + fontLock = -1; + } - mesh.createBuffers(QUAD_VERTICE_COUNT * 128, QUAD_INDICE_COUNT * 128); - auto x = UILabelNew::bufferQuads( - "Hello ", - fontData, - defFace, - position, - 0, - 0 - ); - x += UILabelNew::bufferQuads( - " World", - fontData, - defFaceItalics, - position, - x, - 1 - ); - x += UILabelNew::bufferQuads( - " How are you?", - fontData, - defFaceBold, - position, - x, - 2 - ); + fontLock = font->lock(NewTrueTypeFaceTextureStyle{ + fontSize, + style + }); + }, { &fontSize, &style }); - fontData.colors[0] = COLOR_MAGENTA; - fontData.textures[0] = 0; - fontData.colors[1] = COLOR_RED; - fontData.textures[1] = 1; - fontData.colors[2] = COLOR_GREEN; - fontData.textures[2] = 2; - shaderBuffer.buffer(&fontData); + // struct FontShaderBufferData fontData; + // glm::vec2 position = glm::vec2(32, 32); + + // mesh.createBuffers(QUAD_VERTICE_COUNT * 128, QUAD_INDICE_COUNT * 128); + // auto x = UILabelNew::bufferQuads( + // "Hello ", + // fontData, + // defFace, + // position, + // 0, + // 0 + // ); + // x += UILabelNew::bufferQuads( + // " World", + // fontData, + // defFaceItalics, + // position, + // x, + // 1 + // ); + // x += UILabelNew::bufferQuads( + // " How are you?", + // fontData, + // defFaceBold, + // position, + // x, + // 2 + // ); + + // fontData.colors[0] = COLOR_MAGENTA; + // fontData.textures[0] = 0; + // fontData.colors[1] = COLOR_RED; + // fontData.textures[1] = 1; + // fontData.colors[2] = COLOR_GREEN; + // fontData.textures[2] = 2; + // shaderBuffer.buffer(&fontData); } std::vector UILabelNew::getUIRenderPasses() { + if(this->fontLock == -1) return {}; + auto canvas = this->getCanvas(); auto shader = getGame()->renderManager.fontShader; @@ -99,12 +106,12 @@ std::vector UILabelNew::getUIRenderPasses() { item.parameterBuffers[shader->bufferUiCanvas] = &canvas->shaderBuffer; item.parameterBuffers[shader->bufferFont] = &this->shaderBuffer; item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND; - item.textureSlots[0] = &defFace.texture; - item.textureSlots[1] = &defFaceItalics.texture; - item.textureSlots[2] = &defFaceBold.texture; - item.textureValues[shader->paramTexture0] = 0; - item.textureValues[shader->paramTexture1] = 1; - item.textureValues[shader->paramTexture2] = 2; + // item.textureSlots[0] = &defFace.texture; + // item.textureSlots[1] = &defFaceItalics.texture; + // item.textureSlots[2] = &defFaceBold.texture; + // item.textureValues[shader->paramTexture0] = 0; + // item.textureValues[shader->paramTexture1] = 1; + // item.textureValues[shader->paramTexture2] = 2; return { item }; } @@ -126,115 +133,115 @@ float_t UILabelNew::getContentHeight() { return 0; } -void UILabelNew::bakeTexture( - struct UILabelFontDef &fontDef, - FT_Face &face, - uint32_t fontSize -) { - fontDef.face = &face; - fontDef.fontSize = fontSize; +// void UILabelNew::bakeTexture( +// struct UILabelFontDef &fontDef, +// FT_Face &face, +// uint32_t fontSize +// ) { +// fontDef.face = &face; +// fontDef.fontSize = fontSize; - if(FT_Set_Pixel_Sizes(face, 0, fontSize)) { - assertUnreachable(); - } +// if(FT_Set_Pixel_Sizes(face, 0, fontSize)) { +// assertUnreachable(); +// } - size_t w = 0, h = 0; - FT_ULong c; +// size_t w = 0, h = 0; +// FT_ULong c; - for(c = NEW_LABEL_CHAR_BEGIN; c < NEW_LABEL_CHAR_END; c++) { - // Load the character - if(FT_Load_Char(face, c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME)) { - assertUnreachable(); - } +// for(c = NEW_LABEL_CHAR_BEGIN; c < NEW_LABEL_CHAR_END; c++) { +// // Load the character +// if(FT_Load_Char(face, c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME)) { +// assertUnreachable(); +// } - // Update the width and height - w = mathMax(w, face->glyph->bitmap.width); - h += face->glyph->bitmap.rows; - } +// // Update the width and height +// w = mathMax(w, face->glyph->bitmap.width); +// h += face->glyph->bitmap.rows; +// } - assertTrue(w > 0); - assertTrue(h > 0); +// assertTrue(w > 0); +// assertTrue(h > 0); - // Now buffer pixels to the texture - float_t y = 0; +// // Now buffer pixels to the texture +// float_t y = 0; - // I'd love to just buffer straight to the GPU, but it seems that is a bit - // unstable right now. - uint8_t *buffer = (uint8_t *)memoryFillWithZero(w * h * sizeof(uint8_t)); +// // I'd love to just buffer straight to the GPU, but it seems that is a bit +// // unstable right now. +// uint8_t *buffer = (uint8_t *)memoryFillWithZero(w * h * sizeof(uint8_t)); - size_t offset = 0; - for(c = NEW_LABEL_CHAR_BEGIN; c < NEW_LABEL_CHAR_END; c++) { - // Load the character - if(FT_Load_Char(face, c, FT_LOAD_RENDER)) { - assertUnreachable(); - } +// size_t offset = 0; +// for(c = NEW_LABEL_CHAR_BEGIN; c < NEW_LABEL_CHAR_END; c++) { +// // Load the character +// if(FT_Load_Char(face, c, FT_LOAD_RENDER)) { +// assertUnreachable(); +// } - // Store the character information - struct UILabelChar info; - info.advanceX = face->glyph->advance.x; - info.advanceY = face->glyph->advance.y; - info.bitmapSize = glm::vec2(face->glyph->bitmap.width, face->glyph->bitmap.rows); - info.bitmapPosition = glm::vec2(face->glyph->bitmap_left, -face->glyph->bitmap_top); - info.textureY = y; - fontDef.charStore[c] = info; +// // Store the character information +// struct UILabelChar info; +// info.advanceX = face->glyph->advance.x; +// info.advanceY = face->glyph->advance.y; +// info.bitmapSize = glm::vec2(face->glyph->bitmap.width, face->glyph->bitmap.rows); +// info.bitmapPosition = glm::vec2(face->glyph->bitmap_left, -face->glyph->bitmap_top); +// info.textureY = y; +// fontDef.charStore[c] = info; - // Buffer the pixels, oh dear GOD there has to be a more efficient way. - for(int32_t i = 0; i < face->glyph->bitmap.rows; i++) { - memoryCopy( - (void *)(face->glyph->bitmap.buffer + (i * face->glyph->bitmap.width)), - (void *)(buffer + offset), - face->glyph->bitmap.width * sizeof(uint8_t) - ); - offset += w * sizeof(uint8_t); - assertTrue(offset <= (w * h * sizeof(uint8_t))); - } - y += face->glyph->bitmap.rows; - } +// // Buffer the pixels, oh dear GOD there has to be a more efficient way. +// for(int32_t i = 0; i < face->glyph->bitmap.rows; i++) { +// memoryCopy( +// (void *)(face->glyph->bitmap.buffer + (i * face->glyph->bitmap.width)), +// (void *)(buffer + offset), +// face->glyph->bitmap.width * sizeof(uint8_t) +// ); +// offset += w * sizeof(uint8_t); +// assertTrue(offset <= (w * h * sizeof(uint8_t))); +// } +// y += face->glyph->bitmap.rows; +// } - fontDef.texture.setSize(w, h, TEXTURE_FORMAT_R); - fontDef.texture.buffer(buffer); - memoryFree(buffer); -} +// fontDef.texture.setSize(w, h, TEXTURE_FORMAT_R); +// fontDef.texture.buffer(buffer); +// memoryFree(buffer); +// } -int32_t UILabelNew::bufferQuads( - std::string text, - struct FontShaderBufferData &bufferData, - struct UILabelFontDef &fontDef, - glm::vec2 &position, - int32_t quadStart, - int32_t partIndex -) { - // Get string length - int32_t len = text.length(); +// int32_t UILabelNew::bufferQuads( +// std::string text, +// struct FontShaderBufferData &bufferData, +// struct UILabelFontDef &fontDef, +// glm::vec2 &position, +// int32_t quadStart, +// int32_t partIndex +// ) { +// // Get string length +// int32_t len = text.length(); - glm::vec2 wh = glm::vec2(fontDef.texture.getWidth(), fontDef.texture.getHeight()); +// glm::vec2 wh = glm::vec2(fontDef.texture.getWidth(), fontDef.texture.getHeight()); - // For each char - for(int32_t i = 0; i < len; i++) { - char ch = text[i]; - int32_t j = quadStart + i; - FT_ULong c = ch; - auto &charInfo = fontDef.charStore[c]; +// // For each char +// for(int32_t i = 0; i < len; i++) { +// char ch = text[i]; +// int32_t j = quadStart + i; +// FT_ULong c = ch; +// auto &charInfo = fontDef.charStore[c]; - // Determine texture coordinates. - glm::vec2 uv0 = glm::vec2(0.0f, charInfo.textureY) / wh; - glm::vec2 uv1 = uv0 + (charInfo.bitmapSize / wh); +// // Determine texture coordinates. +// glm::vec2 uv0 = glm::vec2(0.0f, charInfo.textureY) / wh; +// glm::vec2 uv1 = uv0 + (charInfo.bitmapSize / wh); - // Buffer the quad. - QuadMesh::bufferQuadMeshWithZ(&this->mesh, - position + charInfo.bitmapPosition, uv0, - position + charInfo.bitmapPosition + charInfo.bitmapSize, uv1, - 0.0f, - j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT - ); +// // Buffer the quad. +// QuadMesh::bufferQuadMeshWithZ(&this->mesh, +// position + charInfo.bitmapPosition, uv0, +// position + charInfo.bitmapPosition + charInfo.bitmapSize, uv1, +// 0.0f, +// j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT +// ); - // Move the current position along. - position.x += (float_t)(charInfo.advanceX >> 6); - position.y += (float_t)(charInfo.advanceY >> 6); +// // Move the current position along. +// position.x += (float_t)(charInfo.advanceX >> 6); +// position.y += (float_t)(charInfo.advanceY >> 6); - // Set the part index to the quad mappings - bufferData.fontQuadMappings[j] = partIndex; - } +// // Set the part index to the quad mappings +// bufferData.fontQuadMappings[j] = partIndex; +// } - return len; -} \ No newline at end of file +// return len; +// } \ No newline at end of file diff --git a/src/dawn/scene/components/ui/UILabelNew.hpp b/src/dawn/scene/components/ui/UILabelNew.hpp index d3bf3edd..47391ec7 100644 --- a/src/dawn/scene/components/ui/UILabelNew.hpp +++ b/src/dawn/scene/components/ui/UILabelNew.hpp @@ -6,11 +6,12 @@ #pragma once #include "scene/components/ui/UIComponentRenderable.hpp" #include "display/mesh/QuadMesh.hpp" +#include "asset/assets/NewTrueTypeAsset.hpp" namespace Dawn { struct UILabelStyle { struct Color; - UILabelFontDef *fontDef; + // UILabelFontDef *fontDef; }; class UILabelNew : public UIComponentRenderable { @@ -18,13 +19,15 @@ namespace Dawn { Mesh mesh; FontShaderBuffer shaderBuffer; - FT_Face face; - FT_Face faceBold; - FT_Face faceItalics; + usagelockid_t fontLock = -1; - struct UILabelFontDef defFace; - struct UILabelFontDef defFaceBold; - struct UILabelFontDef defFaceItalics; + // FT_Face face; + // FT_Face faceBold; + // FT_Face faceItalics; + + // struct UILabelFontDef defFace; + // struct UILabelFontDef defFaceBold; + // struct UILabelFontDef defFaceItalics; /** * Buffers the quads for the given text and updates the progressing values @@ -38,16 +41,22 @@ namespace Dawn { * @param partIndex The part index to store for each quad buffered. * @return The number of quads buffered, not the string length. */ - int32_t bufferQuads( - std::string text, - struct FontShaderBufferData &bufferData, - struct UILabelFontDef &fontDef, - glm::vec2 &position, - int32_t quadStart, - int32_t partIndex - ); + // int32_t bufferQuads( + // std::string text, + // struct FontShaderBufferData &bufferData, + // struct UILabelFontDef &fontDef, + // glm::vec2 &position, + // int32_t quadStart, + // int32_t partIndex + // ); + + void rebufferQuads(); public: + StateProperty font; + StateProperty fontSize; + StateProperty style; + UILabelNew(SceneItem *item); void onStart() override; diff --git a/src/dawnliminal/CMakeLists.txt b/src/dawnliminal/CMakeLists.txt index 0aa2bafe..cde87c07 100644 --- a/src/dawnliminal/CMakeLists.txt +++ b/src/dawnliminal/CMakeLists.txt @@ -24,10 +24,14 @@ tool_texture(texture_eth FILE=${LIMINAL_ASSETS_DIR}/textures/eth.png) tool_texture(texture_border FILE=${LIMINAL_ASSETS_DIR}/textures/texture_test.png) tool_newtruetype(font_arial - REGULAR="C:\\Windows\\Fonts\\arial.ttf" - BOLD="C:\\Windows\\Fonts\\arialbd.ttf" - ITALICS="C:\\Windows\\Fonts\\ariali.ttf" - BOLD_ITALICS="C:\\Windows\\Fonts\\arialbi.ttf" + REGULAR="/usr/share/fonts/TTF/arial.ttf" + BOLD="/usr/share/fonts/TTF/arialbd.ttf" + ITALICS="/usr/share/fonts/TTF/ariali.ttf" + BOLD_ITALICS="/usr/share/fonts/TTF/arialbi.ttf" + # REGULAR="C:\\Windows\\Fonts\\arial.ttf" + # BOLD="C:\\Windows\\Fonts\\arialbd.ttf" + # ITALICS="C:\\Windows\\Fonts\\ariali.ttf" + # BOLD_ITALICS="C:\\Windows\\Fonts\\arialbi.ttf" ) tool_scene(${LIMINAL_ASSETS_DIR}/scenes/SceneBase.xml) diff --git a/src/dawnliminal/scenes/HelloWorldScene.hpp b/src/dawnliminal/scenes/HelloWorldScene.hpp index 953b607c..9357d79f 100644 --- a/src/dawnliminal/scenes/HelloWorldScene.hpp +++ b/src/dawnliminal/scenes/HelloWorldScene.hpp @@ -28,6 +28,12 @@ namespace Dawn { newLabelItem->transform.setParent(canvas->transform); auto newLabel = newLabelItem->addComponent(); + auto font = this->game->assetManager.get("font_arial"); + + struct NewTrueTypeFaceTextureStyle style; + style.fontSize = 16; + style.style = 0; + // item = this->createSceneItem(); // auto meshRenderer = item->addComponent(); // auto quadMeshHost = item->addComponent(); @@ -59,6 +65,7 @@ namespace Dawn { auto assMan = &this->game->assetManager; std::vector assets; vectorAppend(&assets, SimpleSpinningCubePrefab::getRequiredAssets(assMan)); + assets.push_back(assMan->get("font_arial")); return assets; } diff --git a/src/dawnopengl/display/RenderManager.cpp b/src/dawnopengl/display/RenderManager.cpp index e2fa5cf0..7828ba2d 100644 --- a/src/dawnopengl/display/RenderManager.cpp +++ b/src/dawnopengl/display/RenderManager.cpp @@ -28,7 +28,6 @@ void RenderManager::init() { this->fontShader = this->shaderManager.getShader(this->lockFontShader); this->renderPipeline.init(); - this->fontManager.init(); // Prepare the initial values glEnable(GL_TEXTURE_2D); @@ -52,10 +51,6 @@ ShaderManager * RenderManager::getShaderManager() { return &this->shaderManager; } -FontManager * RenderManager::getFontManager() { - return &this->fontManager; -} - void RenderManager::setRenderFlags(renderflag_t flags) { this->renderFlags = flags; diff --git a/src/dawnopengl/display/RenderManager.hpp b/src/dawnopengl/display/RenderManager.hpp index 3f0758cc..f6a3b26b 100644 --- a/src/dawnopengl/display/RenderManager.hpp +++ b/src/dawnopengl/display/RenderManager.hpp @@ -10,7 +10,6 @@ #include "display/shader/shaders/FontShader.hpp" #include "display/shader/shaders/UIShader.hpp" #include "display/RenderPipeline.hpp" -#include "display/font/FontManager.hpp" #include "display/font/ExampleFont.hpp" namespace Dawn { @@ -18,7 +17,6 @@ namespace Dawn { private: RenderPipeline renderPipeline; ShaderManager shaderManager; - FontManager fontManager; shaderlock_t lockSimpleTextured = -1; shaderlock_t lockUIShaderProgram = -1; shaderlock_t lockFontShader = -1; @@ -38,7 +36,6 @@ namespace Dawn { RenderTarget * getBackBuffer() override; RenderPipeline * getRenderPipeline() override; ShaderManager * getShaderManager() override; - FontManager * getFontManager() override; void setRenderFlags(renderflag_t renderFlags) override; void init() override; void update() override; diff --git a/src/dawnshared/util/UsageLock.hpp b/src/dawnshared/util/UsageLock.hpp index 18ecbfa6..9ffe27f0 100644 --- a/src/dawnshared/util/UsageLock.hpp +++ b/src/dawnshared/util/UsageLock.hpp @@ -6,7 +6,7 @@ #include "assert/assert.hpp" namespace Dawn { - typedef uint32_t usagelockid_t; + typedef int32_t usagelockid_t; class UsageLock { protected: diff --git a/src/dawntools/newtruetypetool/NewTrueTypeTool.cpp b/src/dawntools/newtruetypetool/NewTrueTypeTool.cpp index bb578557..5d7d31f3 100644 --- a/src/dawntools/newtruetypetool/NewTrueTypeTool.cpp +++ b/src/dawntools/newtruetypetool/NewTrueTypeTool.cpp @@ -77,7 +77,6 @@ int32_t NewTrueTypeTool::start() { cleanupFiles(); return 1; } - std::cout << "Push back" << std::endl; ++itFlag; }