diff options
author | chai <chaifix@163.com> | 2021-11-18 19:14:35 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-11-18 19:14:35 +0800 |
commit | 2ab7fad7b308debba0aacbf76831569f360d19a0 (patch) | |
tree | a28f4b6ed64ef9782be0f009ca9e458a709e34ca /Runtime | |
parent | 6198d0c32b5416b328b55c4c4e5b760c745952c7 (diff) |
*misc
Diffstat (limited to 'Runtime')
-rw-r--r-- | Runtime/GUI/Font.cpp | 12 | ||||
-rw-r--r-- | Runtime/GUI/Font.h | 2 | ||||
-rw-r--r-- | Runtime/GUI/UISquare.cpp | 52 | ||||
-rw-r--r-- | Runtime/GUI/UISquare.h | 22 |
4 files changed, 83 insertions, 5 deletions
diff --git a/Runtime/GUI/Font.cpp b/Runtime/GUI/Font.cpp index 207e658..b611126 100644 --- a/Runtime/GUI/Font.cpp +++ b/Runtime/GUI/Font.cpp @@ -23,6 +23,7 @@ static const int s_SizePerPixel = sizeof(unsigned char); Font::Font(std::string path, TextGeneratingSettings settings) : LuaBind::NativeClass<Font>() + , m_Characters() { m_AtlasMargin = settings.margin; m_GlyphPadding = settings.padding; @@ -43,6 +44,7 @@ Font::Font(std::string path, TextGeneratingSettings settings) Font::Font(LuaBind::VM* vm, std::string path, TextGeneratingSettings settings) : LuaBind::NativeClass<Font>(vm) + , m_Characters() { m_AtlasMargin = settings.margin; m_GlyphPadding = settings.padding; @@ -63,6 +65,7 @@ Font::Font(LuaBind::VM* vm, std::string path, TextGeneratingSettings settings) Font::Font(DataBuffer* db, TextGeneratingSettings settings) : LuaBind::NativeClass<Font>() + , m_Characters() { m_AtlasMargin = settings.margin; m_GlyphPadding = settings.padding; @@ -83,6 +86,7 @@ Font::Font(DataBuffer* db, TextGeneratingSettings settings) Font::Font(LuaBind::VM* vm, DataBuffer* db, TextGeneratingSettings settings) : LuaBind::NativeClass<Font>(vm) + , m_Characters() { m_AtlasMargin = settings.margin; m_GlyphPadding = settings.padding; @@ -112,12 +116,12 @@ character::Hash Font::GetHash(Unicode codepoint, int pixelSize) const Character* Font::GetCharacter(character::Unicode codepoint, int pixelSize) { character::Hash hash = GetHash(codepoint, pixelSize); - auto iter = m_Characters.find(hash); + auto iter = m_Characters.find(hash.hashCode); if (iter == m_Characters.end()) { if (RenderCharacter(codepoint, pixelSize)) { - iter = m_Characters.find(hash); + iter = m_Characters.find(hash.hashCode); } else { @@ -148,7 +152,7 @@ void Font::RenderCharacters(std::vector<character::Unicode>& codepoint, int pixe bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize) { character::Hash hash = GetHash(codepoint, pixelSize); - if (m_Characters.count(hash) != 0) + if (m_Characters.size() > 0 && m_Characters.count(hash.hashCode) != 0) return true; FT_Set_Pixel_Sizes(m_FTFace, 0, pixelSize); @@ -220,7 +224,7 @@ bool Font::RenderCharacter(character::Unicode codepoint, int pixelSize) character.advance = m_FTFace->glyph->advance.x * 1/64.f; - m_Characters.insert(std::pair<character::Hash, Character>(hash, character)); + m_Characters.insert(std::pair<unsigned int, Character>(hash.hashCode, character)); return true; } diff --git a/Runtime/GUI/Font.h b/Runtime/GUI/Font.h index 4bcc70b..cf6e4ba 100644 --- a/Runtime/GUI/Font.h +++ b/Runtime/GUI/Font.h @@ -127,7 +127,7 @@ private: //------------------------------------------------------------------------- - std::unordered_map<character::Hash, Character> m_Characters; // 渲染完的文字 + std::unordered_map<unsigned int , Character> m_Characters; // 渲染完的文字 std::vector<GlyphAtals> m_Atlases; // 当前所有的atlas,由font完全拥有所有权,所以是lightuserdata std::unordered_map<int/*pixelSize*/, GlyphAtals*> m_AtlasCache; // 快速找到可用的atlas diff --git a/Runtime/GUI/UISquare.cpp b/Runtime/GUI/UISquare.cpp new file mode 100644 index 0000000..214435e --- /dev/null +++ b/Runtime/GUI/UISquare.cpp @@ -0,0 +1,52 @@ +#include "../Graphics/GfxDevice.h" +#include "UISquare.h" +#include "Runtime/Math/Math.h" + +struct UISquareLayout +{ + Vector2 position; +}; + +static CustomVertexLayout layout; + +InitializeStaticVariables([]() { + VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(UISquareLayout)); + + layout.attributes.push_back(POSITION); +}); + +void UISquare::Draw() +{ + const int nVerts = 4; + const int nIndices = 6; + + float pos[] = { + m_Left, m_Bottom, // left-bottom + m_Right, m_Bottom, // right-bottom + m_Right, m_Top, // right-top + m_Left, m_Top, // top-left + }; + + int indices[] = { + 0, 1, 3, // right-top + 1, 2, 3, // left-bottom + }; + + uint8* vb; + uint16* ib; + + g_SharedVBO.GetChunk(sizeof(UISquareLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib); + + UISquareLayout* dst = (UISquareLayout*)vb; + + for (int i = 0; i < nVerts; ++i) + { + dst[i].position.Set(pos[2 * i], pos[2 * i + 1]); + } + + for (int i = 0; i < nIndices; ++i) + ib[i] = indices[i]; + + g_SharedVBO.ReleaseChunk(4, 6); + g_SharedVBO.DrawChunk(layout); +}
\ No newline at end of file diff --git a/Runtime/GUI/UISquare.h b/Runtime/GUI/UISquare.h new file mode 100644 index 0000000..8de07f0 --- /dev/null +++ b/Runtime/GUI/UISquare.h @@ -0,0 +1,22 @@ +#pragma once +#include "../Utilities/StaticInitiator.h" +#include "UIMesh.h" + +// 纯色方形 +class UISquare : public UIMesh +{ +public: + UISquare(float l, float r, float t, float b) + { + m_Left = l; + m_Right = r; + m_Top = t; + m_Bottom = b; + } + + void Draw() override; + +private: + float m_Left, m_Right, m_Top, m_Bottom; + +}; |