Add configuration option for ghost reset distance. Improve documentation.
This commit is contained in:
22
text.cpp
22
text.cpp
@@ -5,13 +5,16 @@
|
||||
#include "text.h"
|
||||
#include "shader.h"
|
||||
|
||||
// Global freetype context shared across text rendering.
|
||||
FT_Library ft;
|
||||
FT_Face face;
|
||||
|
||||
// Uniform key for tinting glyphs.
|
||||
const char *textcolorC = "textColor";
|
||||
|
||||
unsigned int textVAO, textVBO;
|
||||
unsigned int textVAO, textVBO; // shared quad mesh for all HUD strings
|
||||
|
||||
// Glyph metadata + GL texture for a single character.
|
||||
struct character_t {
|
||||
unsigned int texture_id;
|
||||
glm::ivec2 size;
|
||||
@@ -19,8 +22,10 @@ struct character_t {
|
||||
unsigned int advance;
|
||||
};
|
||||
|
||||
// ASCII glyph cache keyed by character codepoint.
|
||||
std::map<char, character_t> glyph_to_character;
|
||||
|
||||
// Initialize freetype and bake ASCII glyphs into GL textures for reuse.
|
||||
int init_text(int *shader) {
|
||||
|
||||
if (FT_Init_FreeType(&ft)) {
|
||||
@@ -33,8 +38,9 @@ int init_text(int *shader) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Bake glyph bitmaps at a fixed pixel height; width is dynamic.
|
||||
FT_Set_Pixel_Sizes(face, 0, 48);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // allow byte-aligned glyph rows
|
||||
|
||||
for (unsigned char c = 0; c < 128; c++) {
|
||||
|
||||
@@ -44,6 +50,7 @@ int init_text(int *shader) {
|
||||
}
|
||||
|
||||
unsigned int texture;
|
||||
// Upload the glyph bitmap into a single-channel texture.
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(
|
||||
@@ -78,8 +85,7 @@ int init_text(int *shader) {
|
||||
FT_Done_Face(face);
|
||||
FT_Done_FreeType(ft);
|
||||
|
||||
// configure textVAO/textVBO for texture quads
|
||||
// -----------------------------------
|
||||
// Configure buffers for a dynamic quad (two triangles per glyph).
|
||||
glGenVertexArrays(1, &textVAO);
|
||||
glGenBuffers(1, &textVBO);
|
||||
glBindVertexArray(textVAO);
|
||||
@@ -100,9 +106,10 @@ int init_text(int *shader) {
|
||||
|
||||
}
|
||||
|
||||
// Draw a string using pre-baked glyph textures at the supplied screen position.
|
||||
void render_text(int shader, std::string text, float x, float y, float scale, glm::vec3 color) {
|
||||
|
||||
set_uniform(shader, textcolorC, color);
|
||||
set_uniform(shader, textcolorC, color); // apply tint per draw call
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindVertexArray(textVAO);
|
||||
|
||||
@@ -116,6 +123,7 @@ void render_text(int shader, std::string text, float x, float y, float scale, gl
|
||||
float w = ch.size.x * scale;
|
||||
float h = ch.size.y * scale;
|
||||
|
||||
// Each glyph is rendered as a textured quad.
|
||||
float vertices[24] = {
|
||||
xpos, ypos + h, 0.0f, 0.0f,
|
||||
xpos, ypos, 0.0f, 1.0f,
|
||||
@@ -126,12 +134,12 @@ void render_text(int shader, std::string text, float x, float y, float scale, gl
|
||||
};
|
||||
glBindTexture(GL_TEXTURE_2D, ch.texture_id);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, textVBO);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * (2 + 2) * 6, vertices);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float) * (2 + 2) * 6, vertices); // stream quad verts
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glDrawArrays(GL_TRIANGLES, 0, (2 + 2) * 6);
|
||||
|
||||
x += (ch.advance >> 6) * scale;
|
||||
x += (ch.advance >> 6) * scale; // advance is stored in 1/64th pixels
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user