LWJGL Forum

Programming => General Java Game Development => Topic started by: Greenleon on May 27, 2009, 16:24:07

Title: Slick's TrueTypeFont.drawString("") behaves strange
Post by: Greenleon on May 27, 2009, 16:24:07
Hello,

I shortly started to use slick-util. Now I am trying to draw a string into the opengl window. I tried it both of the ways that are explained here
http://slick.cokeandcode.com/wiki/doku.php?id=truetype_font_support

Unfortunately both are bringing the same results. A rectangle in the specified color where the text should have been.

The code:
Code: [Select]
package spiel;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
 
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
 
public class TTFTest {
 
private static final String GAME_TITLE = "test";
private static final boolean fullscreen = false;
    public static void main(String[] args)
        throws LWJGLException, FontFormatException, IOException {
 
        TrueTypeFont trueTypeFont;
 
        // not included for brevity
        initGL();
 
        Font startFont = Font.createFont(Font.TRUETYPE_FONT,
    new BufferedInputStream(new FileInputStream("verdana.ttf")));
        Font baseFont = startFont.deriveFont(Font.PLAIN, 50);
        trueTypeFont = new TrueTypeFont(baseFont, true);
       
        System.out.println(trueTypeFont.toString());
 
        while (true) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            trueTypeFont.drawString(120.0f, 120.0f, "this is a test", Color.green);
 
            Display.update();
            if (Display.isCloseRequested()) {
                System.exit(0);
            }
        }
       
       
    }
   
    private static void initGL()
    {
    try{
        // Create a fullscreen window with 1:1 orthographic 2D projection (default)
        Display.setTitle(GAME_TITLE);
        Display.setFullscreen(fullscreen);
     
        // Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
        Display.setVSyncEnabled(true);
     
        // Create default display of 640x480
        Display.create();
        } catch (Exception e)
        {
       
        }
       
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glViewport(0,0,640,480);
    GL11.glOrtho(0, 640, 0, 480, 0, 25);
    //GL11.glEnable(GL11.GL_LIGHTING);
    GL11.glEnable(GL11.GL_BLEND);

    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glDepthFunc(GL11.GL_ALWAYS);
    GL11.glEnable(GL11.GL_ALPHA_TEST);
    GL11.glAlphaFunc(GL11.GL_GREATER, 0);
    GL11.glEnable(GL11.GL_TEXTURE_2D);
   
    }

}

I have the file verdana.ttf (copied from /usr/share/fonts/truetype/msttcorefonts/) in the project folder which is on the classpath.

Any ideas?? It is frustrating!
Thanks!
Title: Re: Slick's TrueTypeFont.drawString("") behaves strange
Post by: Fool Running on May 28, 2009, 17:45:54
I've never used Slick, but your problem sounds like one of 2 things:
*Either texturing is somehow being turned off (seems unlikely based on your code). You might try doing this:
Quote
If you use your own gl methods to bind textures, TrueTypeFont may not display correctly. To ensure TrueTypeFont works, insert a call to TextureImpl.bindNone() before your drawstring calls.
*Or your blending mode is weird (seems most likely based on your code). You might try removing the setup for the blending mode (still enable blending)

Title: Re: Slick's TrueTypeFont.drawString("") behaves strange
Post by: Greenleon on May 28, 2009, 18:14:06
Thanks for the answer!
I tried bindNone()... did not change anything. And I do not really understand what you mean by "remove the setup for the blending mode". I do not have any setup for any blending mode. I just enabled GL_BLEND.

The full code of the testproject I am refering to is by the way with my first post.

Any other ideas??

Thanks!
Title: Re: Slick's TrueTypeFont.drawString("") behaves strange
Post by: Fool Running on May 31, 2009, 02:55:48
Sorry, I was referring to the:
Code: [Select]
    GL11.glEnable(GL11.GL_ALPHA_TEST);
     GL11.glAlphaFunc(GL11.GL_GREATER, 0);
Its actually the alpha testing, not blending. They might mess with the texture blending of the font, though, which is what I was referring to.
Have you tried removing this from the code:
Code: [Select]
    GL11.glEnable(GL11.GL_DEPTH_TEST);
     GL11.glDepthFunc(GL11.GL_ALWAYS);
     GL11.glEnable(GL11.GL_ALPHA_TEST);
     GL11.glAlphaFunc(GL11.GL_GREATER, 0);
Just for testing.

Again, I'm just guessing since I don't use Slick at all. :-\
Title: Re: Slick's TrueTypeFont.drawString("") behaves strange
Post by: Greenleon on May 31, 2009, 16:56:53
This did not change anything...

Ah I found it. I just looked at the testutils glinit.
Somehow, you need to have this:

Code: [Select]
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
Title: Re: Slick's TrueTypeFont.drawString("") behaves strange
Post by: princec on June 06, 2009, 08:11:50
Heh, not exactly "somehow" ;) That's pretty much the essential pair of lines to get any useful sprite work done.

Cas :)