From a4eecbe1d4cba8218309060abae59fd155d3768d Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Mon, 12 Jun 2023 06:25:17 -0700 Subject: [PATCH] Prog ttf --- src/dawn/display/Color.hpp | 2 +- .../font/truetype/NewTrueTypeFaceTexture.cpp | 3 - src/dawn/scene/components/ui/UILabelNew.cpp | 298 ++++++++++-------- src/dawn/scene/components/ui/UILabelNew.hpp | 11 +- src/dawnliminal/CMakeLists.txt | 16 +- .../display/shader/shaders/FontShader.hpp | 4 +- 6 files changed, 181 insertions(+), 153 deletions(-) diff --git a/src/dawn/display/Color.hpp b/src/dawn/display/Color.hpp index 98caeff5..2ed11cdf 100644 --- a/src/dawn/display/Color.hpp +++ b/src/dawn/display/Color.hpp @@ -11,7 +11,7 @@ namespace Dawn { uint8_t r, g, b, a; }; - #pragma pack(push, 1) + #pragma pack(push, 4) struct Color { float_t r, g, b, a; diff --git a/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.cpp b/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.cpp index 6ca68894..3aacc33a 100644 --- a/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.cpp +++ b/src/dawn/display/font/truetype/NewTrueTypeFaceTexture.cpp @@ -66,9 +66,6 @@ NewTrueTypeFaceTexture::NewTrueTypeFaceTexture( info.bitmapPosition = glm::vec2(face->glyph->bitmap_left, -face->glyph->bitmap_top); info.textureY = y; char c2 = (char)c; - if(c2 == 'H') { - std::cout << "H" << std::endl; - } this->characterData[c] = info; // Buffer the pixels, oh dear GOD there has to be a more efficient way. diff --git a/src/dawn/scene/components/ui/UILabelNew.cpp b/src/dawn/scene/components/ui/UILabelNew.cpp index ee22b400..6ba93053 100644 --- a/src/dawn/scene/components/ui/UILabelNew.cpp +++ b/src/dawn/scene/components/ui/UILabelNew.cpp @@ -14,12 +14,162 @@ UILabelNew::UILabelNew(SceneItem *item) : } +void UILabelNew::onStart() { + this->shaderBuffer.init(); + + auto font = this->getGame()->assetManager.get("font_arial"); + + std::vector texts; + texts.push_back({ + .text = "Hello", + .style = { + .color = COLOR_RED, + .style = 0, + .size = 32, + .font = font + } + }); + texts.push_back({ + .text = "World", + .style = { + .color = COLOR_BLUE, + .style = 1, + .size = 64, + .font = font + } + }); + this->rebufferQuads(texts); + + // std::vector styleStack; + // struct UILabelStyle current; + // styleStack.push_back(current); + // std::vector texts; + + // std::function parseChildren = [&](Xml *node) { + // if(node->children.empty()) { + // struct UILabelText text; + // text.style = current; + // text.text = node->value; + // (node->value) + // } else { + // auto itNode = node->children.begin(); + // while(itNode != node->children.end()) { + // auto child = *itNode; + // std::cout << "Node: " << child->node << std::endl; + + // assertTrue(child->node == "font"); + + // struct UILabelStyle style; + // if(child->attributes.contains("font")) { + // style.font = this->getGame()->assetManager.get(child->attributes["font"]); + // } else { + // style.font = current.font; + // } + + // if(child->attributes.contains("size")) { + // style.size = std::stoi(child->attributes["size"]); + // } else { + // style.size = current.size; + // } + + // if(child->attributes.contains("style")) { + // style.style = std::stoi(child->attributes["style"]); + // } else { + // style.style = current.style; + // } + + // if(child->attributes.contains("color")) { + // style.color = std::stoi(child->attributes["color"]); + // } else { + // style.color = current.color; + // } + + // styleStack.push_back(style); + // current = style; + + // parseChildren(child); + + // styleStack.pop_back(); + // current = styleStack.back(); + // ++itNode; + // } + // } + // }; + + // auto root = Xml::load("" + this->test + ""); + // parseChildren(&root); + // this->rebufferQuads(texts); +} + +std::vector UILabelNew::getUIRenderPasses() { + // if(this->texts.size() == 0) return {}; + + auto canvas = this->getCanvas(); + auto shader = getGame()->renderManager.fontShader; + + struct ShaderPassItem item; + item.shader = shader; + item.mesh = &this->mesh; + item.matrixValues[shader->paramModel] = transform->getWorldTransform(); + item.parameterBuffers[shader->bufferUiCanvas] = &canvas->shaderBuffer; + item.parameterBuffers[shader->bufferFont] = &this->shaderBuffer; + item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND; + + // Map texture slots + auto it = textureMap.begin(); + while(it != textureMap.end()) { + shaderparameter_t param; + switch(it->second) { + case 0: + param = shader->paramTexture0; + break; + + case 1: + param = shader->paramTexture1; + break; + + case 2: + param = shader->paramTexture2; + break; + + case 3: + param = shader->paramTexture3; + break; + + default: + assertUnreachable(); + } + + item.textureSlots[it->second] = &it->first->texture; + item.textureValues[param] = it->second; + ++it; + } + + return { item }; +} + +float_t UILabelNew::getWidth() { + return 0; +} + +float_t UILabelNew::getHeight() { + return 0; +} + +float_t UILabelNew::getContentWidth() { + return 0; +} + +float_t UILabelNew::getContentHeight() { + return 0; +} + void UILabelNew::rebufferQuads(std::vector texts) { std::cout << "Rebuffering" << std::endl; auto oldTexts = this->texts; textureMap.clear(); - glm::vec2 position(0, 0); + glm::vec2 position(32, 32); struct FontShaderBufferData fontData; int32_t quadIndex = 0; int32_t partIndex = 0; @@ -32,13 +182,13 @@ void UILabelNew::rebufferQuads(std::vector texts) { quadCount += itText->text.length(); // Determine font and lock it. - assertNotNull(itText->font); - itText->lockId = itText->font->lock(NewTrueTypeFaceTextureStyle{ - itText->size, - itText->style + assertNotNull(itText->style.font); + itText->lockId = itText->style.font->lock(NewTrueTypeFaceTextureStyle{ + itText->style.size, + itText->style.style }); assertTrue(itText->lockId != -1); - itText->texture = itText->font->getTexture(itText->lockId); + itText->texture = itText->style.font->getTexture(itText->lockId); // Check for existing texture, if not, map it. if(textureMap.find(itText->texture) == textureMap.end()) { @@ -47,7 +197,7 @@ void UILabelNew::rebufferQuads(std::vector texts) { } // Set initial line height - position.y = mathMax(itText->size, position.y); + position.y = mathMax(itText->style.size, position.y); ++itText; } @@ -57,8 +207,8 @@ void UILabelNew::rebufferQuads(std::vector texts) { itText = oldTexts.begin(); while(itText != oldTexts.end()) { assertTrue(itText->lockId != -1); - assertNotNull(itText->font); - itText->font->unlock(itText->lockId); + assertNotNull(itText->style.font); + itText->style.font->unlock(itText->lockId); ++itText; } @@ -82,138 +232,13 @@ void UILabelNew::rebufferQuads(std::vector texts) { quadIndex, partIndex ); - partIndex++; + ++partIndex; ++itText; } shaderBuffer.buffer(&fontData); } -void UILabelNew::onStart() { - this->shaderBuffer.init(); - - auto font = this->getGame()->assetManager.get("font_arial"); - - std::vector texts; - texts.push_back({ - .text = "Hello", - .color = COLOR_RED, - .style = 0, - .size = 16, - .font = font - }); - texts.push_back({ - .text = " World", - .color = COLOR_BLUE, - .style = 1, - .size = 32, - .font = font - }); - this->rebufferQuads(texts); - - // this->texts.clear(); - - // useEffect([&]{ - // usagelockid_t newLock = -1; - - // if(font != nullptr) { - // newLock = font->lock(NewTrueTypeFaceTextureStyle{ - // fontSize, - // style - // }); - // } - - // if(fontLock != -1) { - // font.previous->unlock(fontLock); - // fontLock = -1; - // } - - // if(font == nullptr) return; - - // fontLock = newLock; - // this->rebufferQuads(); - // }, font)(); - - // useEffect([&]{ - // if(font == nullptr) return; - // auto newLock = font->lock(NewTrueTypeFaceTextureStyle{ - // fontSize, - // style - // }); - - // if(fontLock != -1) { - // font->unlock(fontLock); - // fontLock = -1; - // } - - // fontLock = newLock; - // this->rebufferQuads(); - // }, { &fontSize, &style })(); -} - -std::vector UILabelNew::getUIRenderPasses() { - if(this->texts.size() == 0) return {}; - - auto canvas = this->getCanvas(); - auto shader = getGame()->renderManager.fontShader; - - struct ShaderPassItem item; - item.shader = shader; - item.mesh = &this->mesh; - item.matrixValues[shader->paramModel] = transform->getWorldTransform(); - item.parameterBuffers[shader->bufferUiCanvas] = &canvas->shaderBuffer; - item.parameterBuffers[shader->bufferFont] = &this->shaderBuffer; - item.renderFlags = RENDER_MANAGER_RENDER_FLAG_BLEND; - - // Map texture slots - auto it = textureMap.begin(); - while(it != textureMap.end()) { - item.textureSlots[it->second] = &it->first->texture; - - shaderparameter_t param = FONT_SHADER_TEXTURE_MAX; - switch(it->second) { - case 0: - param = shader->paramTexture0; - break; - - case 1: - param = shader->paramTexture1; - break; - - case 2: - param = shader->paramTexture2; - break; - - case 3: - param = shader->paramTexture3; - break; - - default: - assertUnreachable(); - } - item.textureValues[param] = it->second; - ++it; - } - - return { item }; -} - -float_t UILabelNew::getWidth() { - return 0; -} - -float_t UILabelNew::getHeight() { - return 0; -} - -float_t UILabelNew::getContentWidth() { - return 0; -} - -float_t UILabelNew::getContentHeight() { - return 0; -} - int32_t UILabelNew::bufferQuads( struct UILabelText text, struct FontShaderBufferData &bufferData, @@ -258,11 +283,10 @@ int32_t UILabelNew::bufferQuads( } // Map texture level values - bufferData.colors[partIndex] = text.color; - auto textureId = textureMap.find(text.texture); assertTrue(textureId != textureMap.end()); bufferData.textures[partIndex] = textureId->second; + bufferData.colors[partIndex] = text.style.color; 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 1d5f8032..efa93883 100644 --- a/src/dawn/scene/components/ui/UILabelNew.hpp +++ b/src/dawn/scene/components/ui/UILabelNew.hpp @@ -7,14 +7,19 @@ #include "scene/components/ui/UIComponentRenderable.hpp" #include "display/mesh/QuadMesh.hpp" #include "asset/assets/NewTrueTypeAsset.hpp" +#include "util/Xml.hpp" namespace Dawn { - struct UILabelText { - std::string text; + struct UILabelStyle { struct Color color = COLOR_MAGENTA; flag_t style = 0; uint32_t size = 16; NewTrueTypeAsset *font = nullptr; + }; + + struct UILabelText { + std::string text; + struct UILabelStyle style; // Part index // Quad start @@ -56,6 +61,8 @@ namespace Dawn { public: + std::string test; + UILabelNew(SceneItem *item); void onStart() override; diff --git a/src/dawnliminal/CMakeLists.txt b/src/dawnliminal/CMakeLists.txt index cde87c07..90fba1c8 100644 --- a/src/dawnliminal/CMakeLists.txt +++ b/src/dawnliminal/CMakeLists.txt @@ -24,14 +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="/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" + # 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/dawnopengl/display/shader/shaders/FontShader.hpp b/src/dawnopengl/display/shader/shaders/FontShader.hpp index 7f7e439e..555f3a8d 100644 --- a/src/dawnopengl/display/shader/shaders/FontShader.hpp +++ b/src/dawnopengl/display/shader/shaders/FontShader.hpp @@ -12,13 +12,13 @@ #define FONT_SHADER_TEXTURE_MAX 4 namespace Dawn { - // #pragma pack(push, 4) + #pragma pack(push, 4) struct FontShaderBufferData { int32_t fontQuadMappings[FONT_SHADER_QUADS_MAX]; int32_t textures[FONT_SHADER_PARTS_MAX]; struct Color colors[FONT_SHADER_PARTS_MAX]; }; - // #pragma pack(pop) + #pragma pack(pop) class FontShaderBuffer : public ShaderParameterBuffer {