Cleanup
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 2m23s

This commit is contained in:
2024-07-12 01:47:49 -04:00
parent eb3e037c96
commit ae327b96a5
5 changed files with 36 additions and 13 deletions

View File

@@ -36,7 +36,7 @@ public:
unsigned int getFontSize(); unsigned int getFontSize();
unsigned int getFontIndex(); unsigned int getFontIndex();
CachedGlyph* getGlyph(char c); CachedGlyph* getGlyph(char c);
std::vector<CachedGlyph*>* getGlyphs(); std::vector<CachedGlyph*> getGlyphs();
CachedFont(unsigned int font_size, unsigned int font_index); CachedFont(unsigned int font_size, unsigned int font_index);
}; };
@@ -44,7 +44,7 @@ class JGL::FontCache {
private: private:
std::vector<CachedFont*> cachedFonts = {}; std::vector<CachedFont*> cachedFonts = {};
public: public:
std::vector<CachedFont*>* getFonts(); std::vector<CachedFont*> getFonts();
CachedFont* getFont(unsigned int font_size, unsigned int font_index); CachedFont* getFont(unsigned int font_size, unsigned int font_index);
void appendFont(CachedFont* font); void appendFont(CachedFont* font);
void newFont(unsigned int font_size, unsigned int font_index); void newFont(unsigned int font_size, unsigned int font_index);

View File

@@ -54,6 +54,7 @@ namespace JGL {
bool Update(const Vector2& window_size); bool Update(const Vector2& window_size);
bool InitTextEngine(); bool InitTextEngine();
int LoadFont(const std::string& font_path); int LoadFont(const std::string& font_path);
void PurgeFontCache();
void UnloadFont(int font_index); void UnloadFont(int font_index);
// TODO: implement correct coloring // TODO: implement correct coloring
@@ -130,7 +131,8 @@ namespace JGL {
// TODO: Implement an overload that simply takes 3 Vector3's // TODO: Implement an overload that simply takes 3 Vector3's
/// Draws a text string on the screen with a given point-size and font. /// Draws a text string on the screen with a given point-size and font.
void DrawString(const Color3& color, std::string text, float x, float y, float scale, u32 size, unsigned int font_index); void DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index);
void DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index);
// TODO: Implement the following: // TODO: Implement the following:
void FillTexturedTriangle(); void FillTexturedTriangle();

View File

@@ -1,4 +1,5 @@
#include <JGL/FontCache.h> #include <JGL/FontCache.h>
#include <iostream>
using namespace JGL; using namespace JGL;
@@ -21,6 +22,9 @@ CachedGlyph::CachedGlyph(GLuint texture_id, char c, float x2offset, float y2offs
this->advanceY = advanceY; 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) { void JGL::CachedFont::appendGlyph(JGL::CachedGlyph* glyph) {
glyphs.push_back(glyph); glyphs.push_back(glyph);
} }
@@ -72,8 +76,8 @@ void CachedFont::eraseGlyph(GLuint texture_id) {
glyphs.erase(glyphs.begin() + i); glyphs.erase(glyphs.begin() + i);
} }
std::vector<CachedGlyph*>* CachedFont::getGlyphs() { std::vector<CachedGlyph*> CachedFont::getGlyphs() {
return &glyphs; return glyphs;
} }
void FontCache::appendFont(CachedFont* font) { 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) { void FontCache::eraseFont(CachedFont* font) {
for (int i = 0; i < cachedFonts.size(); i++) { for (int i = 0; i < cachedFonts.size(); i++) {
if (cachedFonts[i] == font) { if (cachedFonts[i] == font) {
for (auto& g: *cachedFonts[i]->getGlyphs()) for (auto& g: cachedFonts[i]->getGlyphs())
cachedFonts[i]->eraseGlyph(g); cachedFonts[i]->eraseGlyph(g);
delete cachedFonts[i]; delete cachedFonts[i];
@@ -101,11 +105,10 @@ void FontCache::purgeCache() {
//Remove every font from the cache. //Remove every font from the cache.
for (const auto& font : cachedFonts) for (const auto& font : cachedFonts)
eraseFont(font); eraseFont(font);
cachedFonts = {};
} }
std::vector<CachedFont*>* FontCache::getFonts() { std::vector<CachedFont*> FontCache::getFonts() {
return &cachedFonts; return cachedFonts;
} }
CachedFont* FontCache::getFont(unsigned int font_size, unsigned int font_index) { CachedFont* FontCache::getFont(unsigned int font_size, unsigned int font_index) {

View File

@@ -17,6 +17,7 @@ bool wasVertexArraysEnabled = false;
bool wasCullFaceEnabled = false; bool wasCullFaceEnabled = false;
bool wasBlendEnabled = false; bool wasBlendEnabled = false;
bool wasColorArrayEnabled = false; bool wasColorArrayEnabled = false;
GLint activeTextureUnit = 0;
namespace JGL { namespace JGL {
using namespace J3ML; using namespace J3ML;
@@ -36,6 +37,11 @@ namespace JGL {
glPushMatrix(); glPushMatrix();
glLoadIdentity(); glLoadIdentity();
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
activeTextureUnit = activeTextureUnit - GL_TEXTURE0;
if (activeTextureUnit != 0)
glActiveTexture(GL_TEXTURE0);
wasDepthTestEnabled = false; wasDepthTestEnabled = false;
if (glIsEnabled(GL_DEPTH_TEST)) if (glIsEnabled(GL_DEPTH_TEST))
wasDepthTestEnabled = true, wasDepthTestEnabled = true,
@@ -106,6 +112,9 @@ namespace JGL {
if (wasColorArrayEnabled) if (wasColorArrayEnabled)
glEnable(GL_COLOR_ARRAY); glEnable(GL_COLOR_ARRAY);
//Select whatever texture unit was selected before.
glActiveTexture(GL_TEXTURE0 + activeTextureUnit);
//Put the draw color back how it was before. //Put the draw color back how it was before.
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]); glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);

View File

@@ -52,7 +52,12 @@ namespace JGL {
} }
FontCache fontCache; 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. glUseProgram(0); // Fixed-function pipeline.
Font font{}; Font font{};
@@ -71,10 +76,10 @@ namespace JGL {
if (font.face == nullptr) if (font.face == nullptr)
return; return;
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, 1.0f);
FT_Set_Pixel_Sizes(font.face, 0, size); 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()); std::vector<GLuint> textures(text.length());
//For each character //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 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}; GLfloat textureCoordinates[12] = {0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0};
glTexCoordPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &textureCoordinates); glTexCoordPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &textureCoordinates);
glVertexPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &vertices); glVertexPointer(2, GL_FLOAT, sizeof(GL_FLOAT) * 2, &vertices);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
@@ -133,6 +138,10 @@ namespace JGL {
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture 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) { 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. //TODO figure out what the scale should actually be mathematically.
scale = scale * 0.002f; scale = scale * 0.002f;