diff --git a/src/dawn/asset/assets/CMakeLists.txt b/src/dawn/asset/assets/CMakeLists.txt index e646d3b8..0019338d 100644 --- a/src/dawn/asset/assets/CMakeLists.txt +++ b/src/dawn/asset/assets/CMakeLists.txt @@ -7,4 +7,5 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE TextureAsset.cpp + TrueTypeAsset.cpp ) \ No newline at end of file diff --git a/src/dawn/asset/assets/TextureAsset.cpp b/src/dawn/asset/assets/TextureAsset.cpp index b6185ea3..67751402 100644 --- a/src/dawn/asset/assets/TextureAsset.cpp +++ b/src/dawn/asset/assets/TextureAsset.cpp @@ -15,20 +15,22 @@ TextureAsset::TextureAsset(AssetManager &assetManager, std::string name) : } void TextureAsset::updateSync() { - if(this->state == 0x00 || this->state == 0x01) return; + if( + this->state != 0x03 + ) return; - this->state = 0x03; + this->state = 0x04; this->texture->setSize(this->width, this->height); this->texture->buffer(this->colors); - this->state = 0x04; + this->state = 0x05; this->loaded = true; } void TextureAsset::updateAsync() { if(this->state != 0x00) return; - this->loader.loadRaw(&this->buffer); - this->state = 0x01; + this->loader.loadRaw(&this->buffer); + this->state = 0x02; // Parse header data. char integer[32]; @@ -52,7 +54,7 @@ void TextureAsset::updateAsync() { } this->colors = (struct Color *)((void *)(this->buffer + i)); - this->state = 0x02; + this->state = 0x03; } TextureAsset::~TextureAsset() { diff --git a/src/dawn/asset/assets/TrueTypeAsset.cpp b/src/dawn/asset/assets/TrueTypeAsset.cpp new file mode 100644 index 00000000..6d1ae587 --- /dev/null +++ b/src/dawn/asset/assets/TrueTypeAsset.cpp @@ -0,0 +1,76 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "TrueTypeAsset.hpp" + +using namespace Dawn; + +TrueTypeAsset::TrueTypeAsset(AssetManager &assMan, std::string name) : + Asset(assMan, name), + loader(name + ".truetype") +{ +} + +void TrueTypeAsset::updateSync() { + if(this->state != 0x04) return; + this->font.texture.setSize(this->width, this->height); + this->font.texture.buffer(this->pixels); + auto i = this->pixels; + memoryCopy( + this->characterData, + this->font.characterData, + sizeof(truetypechar_t) * TRUETYPE_NUM_CHARS + ); + this->state = 0x05; + this->loaded = true; +} + +void TrueTypeAsset::updateAsync() { + int32_t fontSize; + size_t i, j; + char intBuffer[32]; + char c; + + if(this->state != 0x00) return; + + this->state = 0x01; + this->loader.loadRaw(&this->buffer); + this->state = 0x02; + + // Parse header data. + i = j = 0; + width = -1, height = -1, fontSize = -1; + while(true) { + c = this->buffer[i++]; + if(c == '|') { + intBuffer[j] = '\0'; + if(width == -1) { + this->width = atoi(intBuffer); + j = 0; + continue; + } else if(height == -1) { + this->height = atoi(intBuffer); + j = 0; + continue; + } else { + fontSize = atoi(intBuffer); + break; + } + } + intBuffer[j++] = c; + } + + this->state = 0x03; + this->font.fontSize = fontSize; + this->pixels = (struct Color*)(this->buffer + i); + this->characterData = (truetypechar_t*)( + (uint8_t*)this->pixels + (this->width * this->height * sizeof(struct Color)) + ); + this->state = 0x04; +} + +TrueTypeAsset::~TrueTypeAsset() { + +} \ No newline at end of file diff --git a/src/dawn/asset/assets/TrueTypeAsset.hpp b/src/dawn/asset/assets/TrueTypeAsset.hpp new file mode 100644 index 00000000..3ba50c26 --- /dev/null +++ b/src/dawn/asset/assets/TrueTypeAsset.hpp @@ -0,0 +1,30 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "../Asset.hpp" +#include "../AssetLoader.hpp" +#include "display/font/TrueTypeFont.hpp" + +namespace Dawn { + class TrueTypeAsset : public Asset { + protected: + AssetLoader loader; + uint8_t *buffer = nullptr; + truetypechar_t *characterData = nullptr; + struct Color *pixels = nullptr; + int32_t width, height; + + public: + TrueTypeFont font; + + TrueTypeAsset(AssetManager &assMan, std::string name); + + void updateSync() override; + void updateAsync() override; + + ~TrueTypeAsset(); + }; +} \ No newline at end of file diff --git a/src/dawn/display/Transform.cpp b/src/dawn/display/Transform.cpp index b1a6fb0f..f2d90af4 100644 --- a/src/dawn/display/Transform.cpp +++ b/src/dawn/display/Transform.cpp @@ -152,8 +152,8 @@ Transform * Transform::getParent() { Transform::~Transform() { this->setParent(nullptr); - auto it = this->parent->children.begin(); - while(it != this->parent->children.end()) { + auto it = this->children.begin(); + while(it != this->children.end()) { (*it)->setParent(nullptr); ++it; } diff --git a/src/dawn/display/font/CMakeLists.txt b/src/dawn/display/font/CMakeLists.txt index 0a1bfd16..82430521 100644 --- a/src/dawn/display/font/CMakeLists.txt +++ b/src/dawn/display/font/CMakeLists.txt @@ -7,4 +7,15 @@ target_sources(${DAWN_TARGET_NAME} PRIVATE TrueTypeFont.cpp -) \ No newline at end of file + FontMeasure.cpp +) + +tool_truetype(truetype_ark + ark-pixel.ttf + truetype_ark + 96 + 96 + 10 +) + +add_dependencies(${DAWN_TARGET_NAME} truetype_ark) \ No newline at end of file diff --git a/src/dawn/display/font/Font.hpp b/src/dawn/display/font/Font.hpp index 2aa993e8..6dd291b1 100644 --- a/src/dawn/display/font/Font.hpp +++ b/src/dawn/display/font/Font.hpp @@ -10,40 +10,25 @@ #include "util/mathutils.hpp" #include "display/Texture.hpp" #include "display/mesh/QuadMesh.hpp" +#include "FontMeasure.hpp" #define FONT_NEWLINE '\n' #define FONT_SPACE ' ' namespace Dawn { - class FontMeasure { - public: - virtual float_t getWidth() = 0; - virtual float_t getHeight() = 0; - virtual int32_t getQuadsOnLine(int32_t line); - virtual int32_t getQuadIndexOnLine(int32_t line); - virtual int32_t getLineCount(); - virtual int32_t getQuadCount(); - }; - class Font { public: - Font(); - - virtual void init() = 0; - virtual void buffer( std::string text, float_t fontSize, float_t maxWidth, Mesh &mesh, - FontMeasure &measure + struct FontMeasure *info ) = 0; virtual Texture & getTexture() = 0; virtual void draw(Mesh &mesh, int32_t startCharacter, int32_t length) = 0; virtual float_t getLineHeight(float_t fontSize) = 0; virtual float_t getDefaultFontSize() = 0; - - virtual ~Font(); }; } \ No newline at end of file diff --git a/src/dawn/display/font/FontMeasure.cpp b/src/dawn/display/font/FontMeasure.cpp new file mode 100644 index 00000000..d1b69c99 --- /dev/null +++ b/src/dawn/display/font/FontMeasure.cpp @@ -0,0 +1,39 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "FontMeasure.hpp" + +using namespace Dawn; + +float_t FontMeasure::getWidth() { + return this->width; +} + +float_t FontMeasure::getHeight() { + return this->height; +} + +int32_t FontMeasure::getQuadCount() { + return this->realLength; +} + +size_t FontMeasure::getLineCount() { + return this->lines.size(); +} + +int32_t FontMeasure::getQuadsOnLine(int32_t line) { + return this->lines[line].length; +} + +int32_t FontMeasure::getQuadIndexOnLine(int32_t line) { + return this->lines[line].start; +} + +void FontMeasure::addLine(int32_t start, int32_t len) { + struct FontLineMeasure info; + info.start = start; + info.length = len; + this->lines.push_back(info); +} \ No newline at end of file diff --git a/src/dawn/display/font/FontMeasure.hpp b/src/dawn/display/font/FontMeasure.hpp new file mode 100644 index 00000000..7d2b67c6 --- /dev/null +++ b/src/dawn/display/font/FontMeasure.hpp @@ -0,0 +1,46 @@ +// Copyright (c) 2022 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" + +namespace Dawn { + struct FontLineMeasure { + /** What (real character) index the line starts at */ + int32_t start; + /** How many (real) characters the line is in length */ + int32_t length; + }; + + struct FontMeasure { + public: + /** How many raw chars are in the string */ + int32_t length; + + /** How many real characters (non whitespace) are in the string */ + int32_t realLength; + + /** The real character info for each line */ + std::vector lines; + + /** Dimensions of the string */ + float_t width, height; + + /** + * Internal method that adds a line to the text buffer process. + * + * @param start Start character index for the next line. + * @param len Length of the next line. + */ + void addLine(int32_t start, int32_t length); + + float_t getWidth(); + float_t getHeight(); + int32_t getQuadsOnLine(int32_t line); + int32_t getQuadIndexOnLine(int32_t line); + size_t getLineCount(); + int32_t getQuadCount(); + }; +} \ No newline at end of file diff --git a/src/dawn/display/font/TrueTypeFont.cpp b/src/dawn/display/font/TrueTypeFont.cpp index 11216b73..251f5525 100644 --- a/src/dawn/display/font/TrueTypeFont.cpp +++ b/src/dawn/display/font/TrueTypeFont.cpp @@ -5,6 +5,11 @@ #include "TrueTypeFont.hpp" +#ifndef STB_TRUETYPE_IMPLEMENTATION + #define STB_TRUETYPE_IMPLEMENTATION + #include +#endif + using namespace Dawn; void TrueTypeFont::bakeQuad(truetypequad_t *quad,float_t *x,float_t *y,char c){ @@ -17,10 +22,12 @@ void TrueTypeFont::bakeQuad(truetypequad_t *quad,float_t *x,float_t *y,char c){ ); } + float_t TrueTypeFont::getScale(float_t scale) { return scale / this->fontSize; } + float_t TrueTypeFont::getSpaceSize(float_t fontSize) { return mathRoundFloat(this->fontSize * 0.3f); } @@ -34,21 +41,14 @@ void TrueTypeFont::buffer( float_t fontSize, float_t maxWidth, Mesh &mesh, - FontMeasure &measure + struct FontMeasure *info ) { - // truetypequad_t *quads; - // int32_t stringLength, wordStart, i, j; - // float_t x, y, wordX; - // float_t scale; - // char c; - // truetypequad_t *quad; - auto stringLength = text.length(); if(stringLength == 0) { info->length = 0; info->realLength = 0; - info->lineCount = 0; + info->lines.clear(); info->lines[0].start = 0; info->lines[0].length = 0; info->height = 0; @@ -71,11 +71,10 @@ void TrueTypeFont::buffer( // Setup Scales info->length = 0; info->realLength = 0; - info->lineCount = 0; + info->lines.clear(); // Prepare the line counters - info->lines[0].start = 0; - info->lines[0].length = 0; + info->addLine(0, 0); // Reset Dimensions info->width = info->height = 0; @@ -95,7 +94,7 @@ void TrueTypeFont::buffer( // Did this space cause a newline? if(x > maxWidth) { - trueTypeTextBufferAddLine(info, info->realLength, 0); + info->addLine(info->realLength, 0); y += this->getLineHeight(fontSize); x = 0; } @@ -106,7 +105,7 @@ void TrueTypeFont::buffer( // New line. if(c == FONT_NEWLINE) { - trueTypeTextBufferAddLine(info, info->realLength, 0); + info->addLine(info->realLength, 0); wordStart = info->realLength; y += this->getLineHeight(fontSize); x = 0; @@ -130,19 +129,17 @@ void TrueTypeFont::buffer( } // Go back to the previous (still current) line and remove the chars - info->lines[info->lineCount].length -= info->realLength - wordStart; + info->lines[info->lines.size() - 1].length -= info->realLength - wordStart; // Next line begins with this word y += this->getLineHeight(fontSize); - trueTypeTextBufferAddLine(info, wordStart, info->realLength-wordStart); + info->addLine(wordStart, info->realLength-wordStart); wordX = 0; } - info->lines[info->lineCount].length++; + info->lines[info->lines.size() - 1].length++; info->realLength++; } - - info->lineCount++; // Initialize primitive mesh.createBuffers( @@ -192,5 +189,6 @@ float_t TrueTypeFont::getLineHeight(float_t fontSize) { } float_t TrueTypeFont::getDefaultFontSize() { - return this->fontSize; -} \ No newline at end of file + return (float_t)this->fontSize; +} + diff --git a/src/dawn/display/font/TrueTypeFont.hpp b/src/dawn/display/font/TrueTypeFont.hpp index 39914eb2..6adfd2a3 100644 --- a/src/dawn/display/font/TrueTypeFont.hpp +++ b/src/dawn/display/font/TrueTypeFont.hpp @@ -6,11 +6,6 @@ #pragma once #include "Font.hpp" -#ifndef STB_TRUETYPE_IMPLEMENTATION - #define STB_TRUETYPE_IMPLEMENTATION - #include -#endif - namespace Dawn { /** Which character (ASCII) to start the font from */ #define TRUETYPE_FIRST_CHAR 32 @@ -24,33 +19,6 @@ namespace Dawn { typedef stbtt_bakedchar truetypechar_t; typedef stbtt_aligned_quad truetypequad_t; - class TrueTypeFontMeasure : public FontMeasure { - public: - // typedef struct { - // /** What (real character) index the line starts at */ - // int32_t start; - // /** How many (real) characters the line is in length */ - // int32_t length; - // } truetypefontinfoline_t; - - // typedef struct { - // /** How many raw chars are in the string */ - // int32_t length; - - // /** How many real characters (non whitespace) are in the string */ - // int32_t realLength; - - // /** How many lines is the string? Trailing newlines will count */ - // int32_t lineCount; - - // /** The real character info for each line */ - // truetypefontinfoline_t lines[256]; - - // /** Dimensions of the string */ - // float_t width, height; - // } truetypefontinfo_t; - }; - class TrueTypeFont : public Font { protected: /** @@ -96,17 +64,15 @@ namespace Dawn { public: Texture texture; - float_t fontSize; + int32_t fontSize; truetypechar_t characterData[TRUETYPE_NUM_CHARS]; - void init() override; - void buffer( std::string text, float_t fontSize, float_t maxWidth, Mesh &mesh, - FontMeasure &measure + struct FontMeasure *info ) override; Texture & getTexture() override; diff --git a/src/dawnpokergame/game/DawnPokerGame.cpp b/src/dawnpokergame/game/DawnPokerGame.cpp index 2be99356..f34731c0 100644 --- a/src/dawnpokergame/game/DawnPokerGame.cpp +++ b/src/dawnpokergame/game/DawnPokerGame.cpp @@ -4,9 +4,13 @@ // https://opensource.org/licenses/MIT #include "DawnPokerGame.hpp" +#include "asset/assets/TextureAsset.hpp" using namespace Dawn; +std::shared_ptr asset; +std::shared_ptr assetTexture; + DawnGame::DawnGame(DawnHost &host) : host(host), renderManager(*this) @@ -23,14 +27,32 @@ int32_t DawnGame::init() { auto camera = cameraObject->addComponent(); camera->transform.lookAt(glm::vec3(5, 5, 5), glm::vec3(0, 0, 0)); - auto canvas = UICanvas::createCanvas(this->scene); - auto test = canvas->addElement(); - test->setTransform( - UI_COMPONENT_ALIGN_START, - UI_COMPONENT_ALIGN_START, - glm::vec4(0, 0, 32, 32), - 0 - ); + // auto canvas = UICanvas::createCanvas(this->scene); + // auto test = canvas->addElement(); + // test->setTransform( + // UI_COMPONENT_ALIGN_START, + // UI_COMPONENT_ALIGN_START, + // glm::vec4(0, 0, 32, 32), + // 0 + // ); + + asset = this->assetManager.load("truetype_ark"); + assetTexture = this->assetManager.load("texture_test"); + while(!asset->loaded || !assetTexture->loaded) { + this->assetManager.update(); + } + + + auto text = this->scene->createSceneItem(); + auto meshRenderer = text->addComponent(); + auto material = text->addComponent(); + meshRenderer->mesh = std::make_shared(); + // meshRenderer->mesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT); + // QuadMesh::bufferQuadMesh(*meshRenderer->mesh, glm::vec2(0, 0), glm::vec2(0, 0), glm::vec2(1, 1), glm::vec2(1, 1), 0, 0); + + struct FontMeasure measure; + asset->font.buffer("Test", 16.0f, -1, *meshRenderer->mesh, &measure); + material->textureValues[material->getShader()->getParameterByName("u_Text")] = std::shared_ptr(&asset->font.getTexture()); return DAWN_GAME_INIT_RESULT_SUCCESS; } diff --git a/src/dawnpokergame/game/DawnPokerGame.hpp b/src/dawnpokergame/game/DawnPokerGame.hpp index 3f8d52f4..1b308e0e 100644 --- a/src/dawnpokergame/game/DawnPokerGame.hpp +++ b/src/dawnpokergame/game/DawnPokerGame.hpp @@ -6,4 +6,5 @@ #pragma once #include "game/DawnGame.hpp" #include "scene/components/Components.hpp" -#include "ui/UISprite.hpp" \ No newline at end of file +#include "ui/UISprite.hpp" +#include "asset/assets/TrueTypeAsset.hpp" \ No newline at end of file diff --git a/src/dawntools/display/CMakeLists.txt b/src/dawntools/display/CMakeLists.txt index 50da7c0b..64d727b9 100644 --- a/src/dawntools/display/CMakeLists.txt +++ b/src/dawntools/display/CMakeLists.txt @@ -18,7 +18,7 @@ endfunction() # TrueType Tool function(tool_truetype target in out width height fontSize) add_custom_target(${target} - COMMAND truetypegen "${in}" "${DAWN_ASSETS_BUILD_DIR}/${out}" "${width}" "${height}" "${fontSize}" + COMMAND truetypegen "${DAWN_ASSETS_SOURCE_DIR}/${in}" "${DAWN_ASSETS_BUILD_DIR}/${out}" "${width}" "${height}" "${fontSize}" COMMENT "Generating truetype ${target} from ${in}" DEPENDS truetypegen )