Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java LWJGL Slick UnicodeFont acting up

Tags:

java

lwjgl

I'm having some problems displaying text on the screen, in the past I have just used sprite based text, however this time I want to use UnicodeFont. TrueTypeFonts draw perfectly however its deprecated.

When I try to draw the UnicodeFont it seems to be affected by the characters I use. for example If I draw the string "stackoverflow" the text and the box will draw, if I try "stackoverflowcom" the box will not draw.

A barebones version of my source code is below. On line ~74 I call uniFont.drawString(0, 0,"stackoverflow"); , if the com (or anything really) the box will not be drawn.

edit. > You can use the boolean tryUnicode to swap between true and unicode.

Ive tried sticking them in to seperate display lists but it made no difference.

Could anyone offer an insight in to why this is happening?

Thanks

import java.awt.Font;

import org.lwjgl.LWJGLException;


import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;



import static org.lwjgl.opengl.GL11.*;


public class Game {


    private UnicodeFont uniFont;
    private TrueTypeFont truFont;

    public static void main(String[] argv) {    

        Game game = new Game();
        game.start(); 
    }



    public void start()
    {




        initGL(600, 600);
        initFonts();
        while(!Display.isCloseRequested()) //display not closed
        {
            render();
            Display.update();
            Display.sync(60);
        }
        Display.destroy();
        System.exit(0);

    }


    private void render()
    {

        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
        GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);

        glPushMatrix(); 
            glTranslatef(-0.25f,0.7f,0);
                glScalef(0.001f,-0.001f,0.001f);
                glEnable(GL_BLEND);
                boolean tryUnicode = false;
                if(tryUnicode)
                {
                    uniFont.drawString(0, 0,"stackoverflow");
                                            //EDIT.. glDisable texture is required here.
                }else
                {
                    glScalef(1.1f,1.1f,1f);
                    truFont.drawString(0, 0, "stackoverflow truFont");
                }
                glDisable(GL_BLEND);                    
        glPopMatrix();

        glPushMatrix();
            glTranslatef(-0.25f,0,0);
                glColor3f(0.5f, 0f, 0f);
                    glBegin(GL_TRIANGLE_STRIP); 
                        glVertex3f(0, 0,0.0f);
                        glVertex3f(0.5f, 0,0f);
                        glVertex3f(0f,0.5f,0f);
                        glVertex3f(0.5f, 0.5f,0f);
                    glEnd();
        glPopMatrix();
    }


    private void initGL(int width, int height) {
        try {
            Display.setDisplayMode(new DisplayMode(width,height));
            Display.create();
            //Display.setVSyncEnabled(true);
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

        glEnable(GL11.GL_TEXTURE_2D);
        glShadeModel(GL11.GL_SMOOTH);        
        glEnable(GL11.GL_DEPTH_TEST);
        glDisable(GL11.GL_LIGHTING);                    

        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);                
        glClearDepth(1);                                       

        glEnable(GL_BLEND);
        glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        glDisable(GL_BLEND);
        glMatrixMode(GL11.GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1, 1, -1, 1, -1, 1);
        glMatrixMode(GL11.GL_MODELVIEW);

    }


    private void initFonts() {

        Font awtFont = new Font("", Font.PLAIN,55);
        truFont = new TrueTypeFont(awtFont, true); 

        uniFont = new UnicodeFont(awtFont, 128, false, false);
        uniFont.addAsciiGlyphs();
        uniFont.addGlyphs(400,600);           // Setting the unicode Range
        uniFont.getEffects().add(new ColorEffect(java.awt.Color.white));
        try {
            uniFont.loadGlyphs();
        } catch (SlickException e) {};


    }

}

like image 944
Andrew Avatar asked Dec 21 '22 23:12

Andrew


2 Answers

It seems I have this error now, it will only draw the outlined rectangle before using the font. This seems to be a Slick error, and is a major problem so they should fix it before adding new features. The only work-around is to render fonts last on your frame.

EDIT: Problem Solved!

add this line after you render your font:

GL11.glDisable(GL11.GL_TEXTURE_2D);

The Slick people should really add that line in as it causes so many bugs!

like image 197
adventurerOK Avatar answered Jan 03 '23 19:01

adventurerOK


This is how I create fonts:

Font font = ...; //Your loaded font
float size = 20.0F;
UnicodeFont f = new UnicodeFont(font.deriveFont(0 /*normal*/, size));
f.addAsciiGlyphs();
ColorEffect e = new ColorEffect();
e.setColor(java.awt.Color.white);
f.getEffects().add(e);
try {
    f.loadGlyphs();
} catch (SlickException e1) {
    e1.printStackTrace();
}
return f;

Hope this helped!

like image 40
adventurerok Avatar answered Jan 03 '23 19:01

adventurerok