Java LWJGL Slick UnicodeFont代理

时间:2012-04-08 17:28:06

标签: java lwjgl

我在屏幕上显示文本时遇到一些问题,过去我刚刚使用基于精灵的文本,但这次我想使用UnicodeFont。 TrueTypeFonts完全绘制,但不推荐使用。

当我尝试绘制UnicodeFont时,它似乎受到我使用的字符的影响。例如,如果我绘制字符串“stackoverflow”文本和框将绘制,如果我尝试“stackoverflowcom”框不会绘制。

我的源代码的准系统版本如下。在线~74我调用uniFont.drawString(0,0,“stackoverflow”); ,如果com(或任何真的)将不会绘制框。

修改。 >您可以使用boolean tryUnicode在true和unicode之间进行交换。

我曾尝试将它们粘贴到单独的显示列表中,但它没有任何区别。

任何人都可以提供有关为何发生这种情况的见解吗?

谢谢

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) {};


    }

}

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) {}; } }

2 个答案:

答案 0 :(得分:9)

现在看来我有这个错误,它只会在使用字体之前绘制轮廓矩形。这似乎是 Slick错误,并且是一个主要问题,所以他们应该在添加新功能之前修复它。 唯一的解决方法是在您的框架上渲染最后字体。

编辑:问题解决了!

渲染字体后添加此行:

GL11.glDisable(GL11.GL_TEXTURE_2D);

光滑的人应该真正添加该行,因为它会导致如此多的错误!

答案 1 :(得分:2)

这是我创建字体的方式:

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;

希望这有帮助!