added asset loading class, zoom factor, vbo switch, shader version
This commit is contained in:
@@ -1,26 +1,23 @@
|
||||
#include "pch.h"
|
||||
#include "font.h"
|
||||
#include "shader.h"
|
||||
#include "asset.h"
|
||||
|
||||
std::map<kFont, Font> FontManager::m_fonts;
|
||||
Sampler FontManager::m_sampler;
|
||||
|
||||
bool Font::load(const char* ttf, int font_size)
|
||||
{
|
||||
FILE* font_file = fopen(ttf, "rb");
|
||||
if (font_file)
|
||||
Asset file;
|
||||
LOG("Font::load %s", ttf);
|
||||
if (file.open(ttf) && file.read_all())
|
||||
{
|
||||
fseek(font_file, 0, SEEK_END);
|
||||
long sz = ftell(font_file);
|
||||
auto data = std::make_unique<uint8_t[]>(sz);
|
||||
fseek(font_file, 0, SEEK_SET);
|
||||
auto bytes = fread(data.get(), 1, sz, font_file);
|
||||
assert(bytes==sz);
|
||||
LOG("Font::load loaded");
|
||||
auto bitmap = std::make_unique<uint8_t[]>(w*h);
|
||||
chars.resize(num_chars);
|
||||
int ret = stbtt_BakeFontBitmap(data.get(), 0, (float)font_size, bitmap.get(), w, h, start_char, num_chars, chars.data());
|
||||
font_tex.create(w, h, GL_RED, bitmap.get());
|
||||
fclose(font_file);
|
||||
int ret = stbtt_BakeFontBitmap(file.m_data, 0, (float)font_size, bitmap.get(), w, h, start_char, num_chars, chars.data());
|
||||
font_tex.create(w, h, GL_LUMINANCE, bitmap.get());
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -44,8 +41,8 @@ const Font& FontManager::get(kFont id)
|
||||
bool TextMesh::create()
|
||||
{
|
||||
glGenBuffers(2, font_buffers);
|
||||
#if USE_VBO
|
||||
glGenVertexArrays(1, &font_array);
|
||||
|
||||
glBindVertexArray(font_array);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
@@ -54,6 +51,7 @@ bool TextMesh::create()
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (GLvoid*)(sizeof(float)*2));
|
||||
glBindVertexArray(0);
|
||||
#endif // USE_VBO
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -113,9 +111,21 @@ void TextMesh::draw()
|
||||
f.font_tex.bind();
|
||||
FontManager::m_sampler.bind(0);
|
||||
|
||||
#if USE_VBO
|
||||
glBindVertexArray(font_array);
|
||||
glDrawElements(GL_TRIANGLES, font_array_count, GL_UNSIGNED_SHORT, 0);
|
||||
glBindVertexArray(0);
|
||||
#else
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, font_buffers[1]);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, font_buffers[0]);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (GLvoid*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (GLvoid*)(sizeof(float) * 2));
|
||||
glDrawElements(GL_TRIANGLES, font_array_count, GL_UNSIGNED_SHORT, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
#endif // USE_VBO
|
||||
|
||||
f.font_tex.unbind();
|
||||
FontManager::m_sampler.unbind();
|
||||
|
||||
Reference in New Issue
Block a user