Search code examples
javaopengllwjgl

lwjgl: Have to recreate `True Type Font` every game loop or Slick-Util will not render text


I use this to initiate openGL:

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, -1, 1);
GL11.glViewport(0, 0, width, height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);

I create a True Type Font like this:

Font awtFont = new Font(font, i, size);
return new TrueTypeFont(awtFont, false);

However binding/ unbinding a texture messes up the font, forcing me to re-crate the font every time i want to draw some text. I can not afford to do this as it causes a major lag spike.

This is the method i use to draw textures:

public static void drawImage(image i, int x, int y) {
    i.bind();
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glBegin(7);
    {
        glTexCoord2f(0, 0);
        glVertex2f(x, y);

        glTexCoord2f(0, 1);
        glVertex2f(x, y + i.height);

        glTexCoord2f(1, 1);
        glVertex2f(x + i.width, y + i.height);

        glTexCoord2f(1, 0);
        glVertex2f(x + i.width, y);
    }
    glEnd();
    i.unBind();
}

When i removed i.bind() and i.unbind() the text drawing started working perfectly, but then i did not have my texture draw. So how can i have my texture and my text drawn?

PS:

I have enabled alpha blending:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

and do not disable it because the texture i want to render requires it.


Solution

  • Inserting TextureImpl.bindNone() before every call to render text seamed to fix the problem. I am still not sure where the problem came from because i have used the slick util text render before without using TextureImpl.bindNone() without problems.