added text widget parsing and rendering

This commit is contained in:
2017-02-06 23:32:01 +00:00
parent fd7f62693e
commit 3cc25e7592
7 changed files with 130 additions and 32 deletions

View File

@@ -1,7 +1,9 @@
#include "pch.h"
#include "font.h"
#include "shader.h"
std::map<kFont, Font> FontManager::m_fonts;
Sampler FontManager::m_sampler;
bool Font::load(const char* ttf)
{
@@ -24,6 +26,11 @@ bool Font::load(const char* ttf)
return false;
}
void FontManager::init()
{
m_sampler.create();
}
bool FontManager::load(kFont id, const char *ttf)
{
return m_fonts[id].load(ttf);
@@ -52,24 +59,23 @@ bool TextMesh::create()
void TextMesh::update(kFont id, const char* text)
{
font_id = id;
auto& f = FontManager::get(id);
if (f.chars.size())
{
static char str[64];
static int counter = 0;
counter++;
sprintf(str, "frame %04d", counter);
const auto len = strlen(str);
const auto len = strlen(text);
float x = 0;
float y = 0;
std::vector<glm::vec4> v;
std::vector<GLushort> idx;
glm::vec2 bbmin;
glm::vec2 bbmax;
for (int i = 0; i < len; i++)
{
int c = str[i] - f.start_char;
int c = text[i] - f.start_char;
stbtt_aligned_quad q;
stbtt_GetBakedQuad((stbtt_bakedchar*)f.chars.data(), f.w, f.h, c, &x, &y, &q, true);
auto n = v.size();
auto n = (int)v.size();
v.emplace_back(q.x0, q.y1, q.s0, q.t1);
v.emplace_back(q.x0, q.y0, q.s0, q.t0);
v.emplace_back(q.x1, q.y0, q.s1, q.t0);
@@ -80,7 +86,14 @@ void TextMesh::update(kFont id, const char* text)
idx.push_back(n+0);
idx.push_back(n+2);
idx.push_back(n+3);
bbmin = glm::min(bbmin, { q.x0, q.y0 });
bbmax = glm::max(bbmax, { q.x1, q.y1 });
}
for (int i = 0; i < len*4; i++)
{
v[i] -= glm::vec4(bbmin, 0, 0);
}
bb = bbmax - bbmin;
font_array_count = (int)idx.size();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, font_buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx.size() * sizeof(GLushort), idx.data(), GL_STATIC_DRAW);
@@ -90,3 +103,21 @@ void TextMesh::update(kFont id, const char* text)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
}
void TextMesh::draw()
{
auto& f = FontManager::get(font_id);
if (f.font_tex.ready())
{
glActiveTexture(GL_TEXTURE0);
f.font_tex.bind();
FontManager::m_sampler.bind(0);
glBindVertexArray(font_array);
glDrawElements(GL_TRIANGLES, font_array_count, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);
f.font_tex.unbind();
FontManager::m_sampler.unbind();
}
}