diff options
Diffstat (limited to 'src/font.cpp')
-rw-r--r-- | src/font.cpp | 79 |
1 files changed, 33 insertions, 46 deletions
diff --git a/src/font.cpp b/src/font.cpp index adffa9c..3fdd928 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -1,12 +1,13 @@ #include <font.hpp> -FT_Library FontSystem::ftLibrary; -FT_Face FontSystem::ftFace; - -std::string FontSystem::fontFamily; -std::map<int, std::vector<FT_Info>> FontSystem::fontData; +#include <cstring> -int FontSystem::currentSize = 0; +FT_Library FontSystem::ftLibrary; +FT_Face FontSystem::ftFace; +std::string FontSystem::fontFamily; +std::map<int, std::vector<FT_Info>> FontSystem::fontData; +std::vector<std::unique_ptr<GLfloat>> FontSystem::drawData; +int FontSystem::currentSize = 0; Color FontSystem::currentColor; float FontSystem::currentZ = -8.0f; @@ -74,58 +75,44 @@ void FontSystem::setFontZ(float z) currentZ = z; } -vec2 FontSystem::putChar(float xx, float yy, char c) +vec2 FontSystem::putChar(float x, float y, char c) { const auto& ch = fontData.at(currentSize)[c - 33]; - int x = xx, y = yy; - // get dimensions of the rendered character - vec2 c1 = { - static_cast<float>(floor(x) + ch.bl.x), - static_cast<float>(floor(y) + ch.bl.y) + vec2 c1 (static_cast<float>(floor(x) + ch.bl.x), static_cast<float>(floor(y) + ch.bl.y)); + vec2 c2 (c1.x + ch.wh.x, c1.y - ch.wh.y); + + GLfloat verts[31] = { + static_cast<GLfloat>(ch.tex), + c1.x, c1.y, currentZ, 0, 0, + c2.x, c1.y, currentZ, 1, 0, + c2.x, c2.y, currentZ, 1, 1, + c2.x, c2.y, currentZ, 1, 1, + c1.x, c2.y, currentZ, 0, 1, + c1.x, c1.y, currentZ, 0, 0, }; - const auto& c2 = ch.wh; + drawData.emplace_back(reinterpret_cast<GLfloat*>(std::memcpy(new GLfloat[31], verts, 31 * sizeof(GLfloat)))); + return ch.ad; +} + +void FontSystem::render(void) +{ Render::textShader.use(); Render::textShader.enable(); - // draw the character - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, ch.tex); - - glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0f, 1.0f, 1.0f, 1.0f); - - GLfloat tex_coord[] = { - 0.0, 1.0, // bottom left - 1.0, 1.0, // bottom right - 1.0, 0.0, // top right - 1.0, 0.0, // top right - 0.0, 0.0, // top left - 0.0, 1.0, // bottom left - }; - - GLfloat text_vert[] = { - c1.x, c1.y - c2.y, currentZ, // bottom left - c1.x + c2.x, c1.y - c2.y, currentZ, // bottom right - c1.x + c2.x, c1.y + c2.y - c2.y, currentZ, // top right - c1.x + c2.x, c1.y + c2.y - c2.y, currentZ, // top right - c1.x, c1.y + c2.y - c2.y, currentZ, // top left - c1.x, c1.y - c2.y, currentZ // bottom left - }; - glUniform4f(Render::textShader.uniform[WU_tex_color], currentColor.red, currentColor.green, currentColor.blue, currentColor.alpha); - glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, text_vert); - glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex_coord); - glDrawArrays(GL_TRIANGLES, 0, 6); - - glUniform4f(Render::textShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0); + for (const auto& d : drawData) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(d.get()[0])); + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), d.get() + 1); + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), d.get() + 4); + glDrawArrays(GL_TRIANGLES, 0, 6); + } Render::textShader.disable(); Render::textShader.unuse(); - - // return the width. - return ch.ad; + drawData.clear(); } - |