diff --git a/include/JGL/JGL.h b/include/JGL/JGL.h index cdb14e7..764fc8c 100644 --- a/include/JGL/JGL.h +++ b/include/JGL/JGL.h @@ -35,7 +35,7 @@ namespace JGL { /// @param window_size void Update(const Vector2& window_size); - inline void PurgeFontCache() { fontCache.purgeCache(); } + inline void PurgeFontCache() { JGL::fontCache.purgeCache(); } std::array OpenGLPerspectiveProjectionRH(float fovY, float aspect, float z_near, float z_far); /// Returns true if the graphics driver meets the requirements (GL Version & Extensions). bool MeetsRequirements(); diff --git a/include/JGL/types/FontCache.h b/include/JGL/types/FontCache.h index 45d55f0..01a52c7 100644 --- a/include/JGL/types/FontCache.h +++ b/include/JGL/types/FontCache.h @@ -26,7 +26,7 @@ public: //CachedGlyph(GLuint texture_id, char c); CachedGlyph(char c, std::array texcoords, float x2o, float y2o, float w, float h, float advX, float advY); - char getCharacter(); + char getCharacter() const; [[nodiscard]] std::array getTexCoords() const; }; @@ -38,16 +38,19 @@ private: GLsizei texture_width = 0, texture_height = 0; unsigned int font_size = 0; unsigned int font_index = 0; + void Erase(); public: void appendGlyph(CachedGlyph* glyph); - unsigned int getFontSize(); - unsigned int getFontIndex(); + unsigned int getFontSize() const; + unsigned int getFontIndex() const; CachedGlyph* getGlyph(char c); std::unordered_map getGlyphs(); - const GLuint* getTexture(); + const GLuint* getTextureHandle(); [[nodiscard]] GLsizei getTextureWidth() const; [[nodiscard]] GLsizei getTextureHeight() const; +public: CachedFont(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, unsigned int font_size, unsigned int font_index); + ~CachedFont(); }; class JGL::FontCache { diff --git a/main.cpp b/main.cpp index 77617db..8f0670a 100644 --- a/main.cpp +++ b/main.cpp @@ -121,7 +121,7 @@ public: glDepthFunc(GL_LESS); glDepthMask(GL_TRUE); image = new Texture("assets/sprites/Re3D.png", TextureFilteringMode::BILINEAR); - j2d_render_target = new RenderTarget({540, 540}, {0,0,0,0}, false, MSAA_SAMPLE_RATE::MSAA_8X); + j2d_render_target = new RenderTarget({540, 540}, {0,0,0,0}, false, MSAA_SAMPLE_RATE::MSAA_NONE); image2 = image; image2_render_target = new RenderTarget(image2); @@ -208,10 +208,9 @@ public: //Draw the Render Target that we just drew all that stuff onto. - auto image3_render_target = image2_render_target; J2D::Begin(); J2D::DrawSprite(j2d_render_target, {0, 0}, 0, {0.5, 0.5}, {1,1}, Colors::White); - J2D::DrawSprite(image3_render_target, {300, 500}, 0, {0.5, 0.5}, {1,1}, Colors::White); + J2D::DrawSprite(image2_render_target, {300, 500}, 0, {0.5, 0.5}, {1,1}, Colors::White); J2D::End(); } diff --git a/src/JGL.cpp b/src/JGL.cpp index 042420e..7d01c27 100644 --- a/src/JGL.cpp +++ b/src/JGL.cpp @@ -726,7 +726,7 @@ namespace JGL { DrawLine(color, last, first, thickness); } - void J2D::OutlinePolygon(const Color4 &color, const Vector2* points, int points_size, float thickness) { + void J2D::OutlinePolygon(const Color4& color, const Vector2* points, int points_size, float thickness) { if (!inJ2D) Logger::Error("Drawing J2D element before J2D begin."); diff --git a/src/TextRendering.cpp b/src/TextRendering.cpp index 7995af9..90f234d 100644 --- a/src/TextRendering.cpp +++ b/src/TextRendering.cpp @@ -15,7 +15,7 @@ #include #include -#include "JGL/logger/logger.h" +#include namespace JGL { CachedFont* CacheFont(const Font& font, u32 size) { @@ -121,7 +121,7 @@ namespace JGL { glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_TEXTURE_2D); //Texture parameters are restored when the texture_handle is bound - glBindTexture(GL_TEXTURE_2D, *cachedFont->getTexture()); + glBindTexture(GL_TEXTURE_2D, *cachedFont->getTextureHandle()); std::vector> vertices(text.size()); std::vector> texcoords(text.size()); @@ -194,7 +194,7 @@ namespace JGL { glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_TEXTURE_2D); glColor4ubv(color.ptr()); - glBindTexture(GL_TEXTURE_2D, *cachedFont->getTexture()); + glBindTexture(GL_TEXTURE_2D, *cachedFont->getTextureHandle()); std::vector> vertices(text.size()); std::vector> texcoords(text.size()); diff --git a/src/types/Font.cpp b/src/types/Font.cpp index c960232..83dcede 100644 --- a/src/types/Font.cpp +++ b/src/types/Font.cpp @@ -82,8 +82,7 @@ namespace JGL //return newIndex; } - Font::~Font() - { + Font::~Font() { //Detail::UnloadFont(this->index); } diff --git a/src/types/FontCache.cpp b/src/types/FontCache.cpp index 3ed4f57..4ccbbde 100644 --- a/src/types/FontCache.cpp +++ b/src/types/FontCache.cpp @@ -2,7 +2,7 @@ using namespace JGL; -char CachedGlyph::getCharacter() { +char CachedGlyph::getCharacter() const { return character; } @@ -25,11 +25,11 @@ void JGL::CachedFont::appendGlyph(JGL::CachedGlyph* glyph) { glyphs.emplace(glyph->getCharacter(), glyph); } -unsigned int JGL::CachedFont::getFontSize() { +unsigned int JGL::CachedFont::getFontSize() const { return font_size; } -unsigned int JGL::CachedFont::getFontIndex() { +unsigned int JGL::CachedFont::getFontIndex() const { return font_index; } @@ -52,7 +52,7 @@ std::unordered_map CachedFont::getGlyphs() { return glyphs; } -const GLuint* CachedFont::getTexture() { +const GLuint* CachedFont::getTextureHandle() { return &texture; } @@ -64,6 +64,15 @@ GLsizei CachedFont::getTextureHeight() const { return texture_height; } +void CachedFont::Erase() { + if (texture != 0) + glDeleteTextures(1, &texture); +} + +CachedFont::~CachedFont() { + Erase(); +} + void FontCache::appendFont(CachedFont* font) { cachedFonts.push_back(font); }