This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <JGL/FontCache.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace JGL;
|
||||
|
||||
@@ -21,6 +22,9 @@ CachedGlyph::CachedGlyph(GLuint texture_id, char c, float x2offset, float y2offs
|
||||
this->advanceY = advanceY;
|
||||
}
|
||||
|
||||
//TODO
|
||||
//Because most things shown would be english characters. We can cut down on the iteration time significantly
|
||||
//by putting each english character at the beginning of the list in order of how often they usually occur in text.
|
||||
void JGL::CachedFont::appendGlyph(JGL::CachedGlyph* glyph) {
|
||||
glyphs.push_back(glyph);
|
||||
}
|
||||
@@ -72,8 +76,8 @@ void CachedFont::eraseGlyph(GLuint texture_id) {
|
||||
glyphs.erase(glyphs.begin() + i);
|
||||
}
|
||||
|
||||
std::vector<CachedGlyph*>* CachedFont::getGlyphs() {
|
||||
return &glyphs;
|
||||
std::vector<CachedGlyph*> CachedFont::getGlyphs() {
|
||||
return glyphs;
|
||||
}
|
||||
|
||||
void FontCache::appendFont(CachedFont* font) {
|
||||
@@ -88,7 +92,7 @@ void FontCache::newFont(unsigned int font_size, unsigned int font_index) {
|
||||
void FontCache::eraseFont(CachedFont* font) {
|
||||
for (int i = 0; i < cachedFonts.size(); i++) {
|
||||
if (cachedFonts[i] == font) {
|
||||
for (auto& g: *cachedFonts[i]->getGlyphs())
|
||||
for (auto& g: cachedFonts[i]->getGlyphs())
|
||||
cachedFonts[i]->eraseGlyph(g);
|
||||
|
||||
delete cachedFonts[i];
|
||||
@@ -101,11 +105,10 @@ void FontCache::purgeCache() {
|
||||
//Remove every font from the cache.
|
||||
for (const auto& font : cachedFonts)
|
||||
eraseFont(font);
|
||||
cachedFonts = {};
|
||||
}
|
||||
|
||||
std::vector<CachedFont*>* FontCache::getFonts() {
|
||||
return &cachedFonts;
|
||||
std::vector<CachedFont*> FontCache::getFonts() {
|
||||
return cachedFonts;
|
||||
}
|
||||
|
||||
CachedFont* FontCache::getFont(unsigned int font_size, unsigned int font_index) {
|
||||
|
@@ -17,6 +17,7 @@ bool wasVertexArraysEnabled = false;
|
||||
bool wasCullFaceEnabled = false;
|
||||
bool wasBlendEnabled = false;
|
||||
bool wasColorArrayEnabled = false;
|
||||
GLint activeTextureUnit = 0;
|
||||
|
||||
namespace JGL {
|
||||
using namespace J3ML;
|
||||
@@ -36,6 +37,11 @@ namespace JGL {
|
||||
glPushMatrix();
|
||||
glLoadIdentity();
|
||||
|
||||
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
|
||||
activeTextureUnit = activeTextureUnit - GL_TEXTURE0;
|
||||
if (activeTextureUnit != 0)
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
wasDepthTestEnabled = false;
|
||||
if (glIsEnabled(GL_DEPTH_TEST))
|
||||
wasDepthTestEnabled = true,
|
||||
@@ -106,6 +112,9 @@ namespace JGL {
|
||||
if (wasColorArrayEnabled)
|
||||
glEnable(GL_COLOR_ARRAY);
|
||||
|
||||
//Select whatever texture unit was selected before.
|
||||
glActiveTexture(GL_TEXTURE0 + activeTextureUnit);
|
||||
|
||||
//Put the draw color back how it was before.
|
||||
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
|
||||
|
||||
|
@@ -52,7 +52,12 @@ namespace JGL {
|
||||
}
|
||||
FontCache fontCache;
|
||||
|
||||
void J2D::DrawString(const Color3& color, std::string text, float x, float y, float scale, u32 size, unsigned int font_index) {
|
||||
void PurgeFontCache() {
|
||||
fontCache.purgeCache();
|
||||
}
|
||||
|
||||
//TODO multi-texturing for 8x speedup. I tried. We'll come back to it later.
|
||||
void J2D::DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index) {
|
||||
glUseProgram(0); // Fixed-function pipeline.
|
||||
|
||||
Font font{};
|
||||
@@ -71,10 +76,10 @@ namespace JGL {
|
||||
if (font.face == nullptr)
|
||||
return;
|
||||
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, 1.0f);
|
||||
|
||||
FT_Set_Pixel_Sizes(font.face, 0, size);
|
||||
|
||||
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
|
||||
|
||||
std::vector<GLuint> textures(text.length());
|
||||
|
||||
//For each character
|
||||
@@ -124,8 +129,8 @@ namespace JGL {
|
||||
}
|
||||
|
||||
GLfloat vertices[12] = {x2, y2, x2, y2 + h, x2 + w, y2 + h,x2, y2, x2 + w, y2 + h, x2 + w, y2};
|
||||
|
||||
GLfloat textureCoordinates[12] = {0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0};
|
||||
|
||||
glTexCoordPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &textureCoordinates);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &vertices);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||
@@ -133,6 +138,10 @@ namespace JGL {
|
||||
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
|
||||
}
|
||||
|
||||
void J2D::DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index) {
|
||||
J2D::DrawString(Color4::FromColor3(color, 255), text, x, y, scale, size, font_index);
|
||||
}
|
||||
|
||||
void J3D::DrawString(const Color3& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, unsigned int font_index) {
|
||||
//TODO figure out what the scale should actually be mathematically.
|
||||
scale = scale * 0.002f;
|
||||
|
Reference in New Issue
Block a user