From 1f68fe78e84f25c8ddacdc37a293a5de31725bab Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 1 Jan 2022 13:01:08 +0000 Subject: First iteration with font/text support --- ui/text.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 ui/text.cpp (limited to 'ui/text.cpp') diff --git a/ui/text.cpp b/ui/text.cpp new file mode 100644 index 0000000..be696c3 --- /dev/null +++ b/ui/text.cpp @@ -0,0 +1,59 @@ +#include "text.h" +#include "font.h" +#include "gfx/gl/uiShader.h" +#include "uiComponent.h" +#include +#include +#include +#include +#include +#include + +const auto font {"/usr/share/fonts/hack/Hack-Regular.ttf"}; +Text::Text(std::string_view s, Position pos, glm::vec3 c) : UIComponent {pos}, colour {c} +{ + for (const auto & textureQuads : Font {font, static_cast(pos.size.y)}.render(s)) { + auto & rendering + = models.emplace_back(textureQuads.first, static_cast(6 * textureQuads.second.size())); + glBindVertexArray(rendering.vao); + + glBindBuffer(GL_ARRAY_BUFFER, rendering.vbo); + std::vector vertices; + vertices.reserve(6 * textureQuads.second.size()); + for (const auto & quad : textureQuads.second) { + for (auto offset = 0U; offset < 3; offset += 2) { + for (auto vertex = 0U; vertex < 3; vertex += 1) { + vertices.emplace_back(quad[(vertex + offset) % 4] + glm::vec4 {position.origin, 0, 0}); + } + } + }; + glBufferData(GL_ARRAY_BUFFER, static_cast(sizeof(glm::vec4)) * rendering.count, + glm::value_ptr(vertices.front()), GL_STATIC_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), nullptr); + + glBindVertexArray(0); + } +} + +void +Text::render(const UIShader & shader, const Position &) const +{ + shader.useText(colour); + for (const auto & m : models) { + glBindTexture(GL_TEXTURE_2D, m.texture); + glBindVertexArray(m.vao); + glDrawArrays(GL_TRIANGLES, 0, m.count); + } + glBindVertexArray(0); + glBindTexture(GL_TEXTURE_2D, 0); +} + +bool +Text::handleInput(const SDL_Event &, const Position &) +{ + return false; +} + +Text::Model::Model(GLuint t, GLsizei c) : texture {t}, count {c} { } -- cgit v1.2.3