aboutsummaryrefslogtreecommitdiffstats
path: root/src/text.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/text.cpp')
-rw-r--r--src/text.cpp92
1 files changed, 58 insertions, 34 deletions
diff --git a/src/text.cpp b/src/text.cpp
index fdb3245..e0eb158 100644
--- a/src/text.cpp
+++ b/src/text.cpp
@@ -69,14 +69,18 @@ void TextSystem::loadFont(const std::string& name,
//
auto& font = fontData[name];
- glGenTextures(1, &font.tex);
glGenBuffers(1, &font.vbo);
+
+ glGenTextures(1, &font.tex);
glBindTexture(GL_TEXTURE_2D, font.tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height,
0, GL_RED, GL_UNSIGNED_BYTE, 0);
+
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -89,9 +93,15 @@ void TextSystem::loadFont(const std::string& name,
// Load each character and add it to the texture
//
+ font.width = width;
+ font.height = height;
+
float offsetX = 0, offsetY = 0;
for (int c = 32; c < 128; c++) {
- FT_Load_Char(face, c, FT_LOAD_RENDER);
+ if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
+ std::cerr << "Unrecognized character: " << c << std::endl;
+ continue;
+ }
auto* g = face->glyph;
glTexSubImage2D(GL_TEXTURE_2D, 0, offsetX, offsetY,
@@ -99,13 +109,13 @@ void TextSystem::loadFont(const std::string& name,
GL_RED, GL_UNSIGNED_BYTE,
g->bitmap.buffer);
- auto& d = font.data[c - 32];
+ auto& d = font.data[c-32];
d.dim = { g->bitmap.width, g->bitmap.rows };
d.bitmap = { g->bitmap_left, g->bitmap_top };
d.advance = { g->advance.x >> 6, g->advance.y >> 6 };
d.offset = { offsetX / width, offsetY / height };
- offsetX += g->bitmap.width;
+ offsetX += g->bitmap.width;// + 1.0f;
// Keep offsetY at zero?
}
@@ -131,69 +141,83 @@ void TextSystem::updateVBOs(void)
auto& d = data.second;
d.buffer.clear();
for (auto& text : d.text) {
- float cOff = 0.0f;
+ float tx = text.x;
+ float ty = text.y;
for (char c : text.text) {
if (c < 32)
continue;
+ c -= 32;
+
+ float x = tx + d.data[c].bitmap.first;
+ float y = ty - (d.data[c].dim.second - d.data[c].bitmap.second);
+ float z = text.z;
+
+ float w = d.data[c].dim.first;
+ float h = d.data[c].dim.second;
+
+ tx += d.data[c].advance.first;
+ ty += d.data[c].advance.second;
+
+ if (w == 0.0f || h == 0.0f)
+ continue;
+
d.buffer += {
- text.x+cOff,
- text.y,
- text.z,
+ x,
+ y,
+ z,
d.data[c].offset.first,
- d.data[c].offset.second+d.data[c].dim.second,
+ d.data[c].offset.second+d.data[c].dim.second/d.height,
1.0f
};
d.buffer += {
- text.x+cOff+d.data[c].dim.first,
- text.y,
- text.z,
- d.data[c].offset.first+d.data[c].dim.first,
- d.data[c].offset.second+d.data[c].dim.second,
+ x+w,
+ y,
+ z,
+ d.data[c].offset.first+d.data[c].dim.first/d.width,
+ d.data[c].offset.second+d.data[c].dim.second/d.height,
1.0f
};
d.buffer += {
- text.x+cOff,
- text.y+d.data[c].dim.second,
- text.z,
+ x,
+ y+h,
+ z,
d.data[c].offset.first,
d.data[c].offset.second,
1.0f
};
d.buffer += {
- text.x+cOff+d.data[c].dim.first,
- text.y,
- text.z,
- d.data[c].offset.first+d.data[c].dim.first,
- d.data[c].offset.second+d.data[c].dim.second,
+ x+w,
+ y,
+ z,
+ d.data[c].offset.first+d.data[c].dim.first/d.width,
+ d.data[c].offset.second+d.data[c].dim.second/d.height,
1.0f
};
d.buffer += {
- text.x+cOff+d.data[c].dim.first,
- text.y+d.data[c].dim.second,
- text.z,
- d.data[c].offset.first+d.data[c].dim.first,
+ x+w,
+ y+h,
+ z,
+ d.data[c].offset.first+d.data[c].dim.first/d.width,
d.data[c].offset.second,
1.0f
};
d.buffer += {
- text.x+cOff,
- text.y+d.data[c].dim.second,
- text.z,
- d.data[c].offset.first+d.data[c].dim.first,
+ x,
+ y+h,
+ z,
+ d.data[c].offset.first,
d.data[c].offset.second,
1.0f
};
-
- cOff += d.data[c].dim.first + d.data[c].advance.first;
}
}
glBindBuffer(GL_ARRAY_BUFFER, d.vbo);
glBufferData(GL_ARRAY_BUFFER,
- d.text.size() * sizeof(TextMeshData), d.text.data(),
- GL_STREAM_DRAW);
+ d.buffer.size() * sizeof(TextMeshData), d.buffer.data(),
+ GL_DYNAMIC_DRAW);
}
}