java - OpenGL (LWJGL+Slick-Util) - 文本无法正确显示

标签 java opengl lwjgl slick2d

我正在尝试通过 LWJGL 和 Slick-Util 使用 OpenGL 开发一款游戏(仍不完全确定它们如何相互关联)。我已经弄清楚如何让 TrueTypeFonts 工作。问题是,一旦我让字体工作,我的游戏的其他方面(加载栏和 map )现在根本不显示。知道问题是什么吗?这是我的游戏的类。

package manager;

import java.awt.Font;
import java.io.InputStream;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.util.ResourceLoader;

public class Game {

    //Scene States0

    public static final int STARTLOADING_SCENE = 0;
    public static final int MAINMENU_SCENE = 1;
    public static final int GAME_SCENE = 2;

    int gameScene;

    int loadingBarWidth;

    int width, height;

    int mouseXonMap,mouseYonMap;
    String tileTypeHighlighted;
    boolean mouseOnMap;

    boolean wKeyDown, aKeyDown, sKeyDown, dKeyDown;

    GameMap gameMap;
    int mapOffsetX, mapOffsetY;
    int tileWidth;

    TrueTypeFont commonGameFont;
    TrueTypeFont backupFont;

    /** 
     * Runs initialization components
     */
    public void start()
    {
        width = 640;
        height = 480;
        initGL(width,height);
        init();
        gameScene = 0;
        gameMap = new GameMap(10,10);
        mapOffsetX = 50;
        mapOffsetY = 50;
        tileWidth = 25;

        while(true) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

            checkForInput();
            updateObjects();
            render();

            Display.update();
            Display.sync(100);

            if(Display.isCloseRequested())
            {
                Display.destroy();
                System.exit(0);
            }
        }
    }

    /**
     * Initializes the display screen
     * @param width   - Width of the display
     * @param height  - Height of the display
     */
    public 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);
        }
        //Begin stuff from tutorial
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glShadeModel(GL11.GL_SMOOTH);        
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glDisable(GL11.GL_LIGHTING);                    

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

        GL11.glEnable(GL11.GL_BLEND); //This line is important, but I don't know why.
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        GL11.glViewport(0,0,width,height);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);

        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, width, height, 0, 1, -1);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        //End Stuff from tutorial
    }

    /**
     * Initializes resources
     */
    public void init()
    {
        //Initialize Resources
        try {

            InputStream inputStream = ResourceLoader.getResourceAsStream("res/8-Bit-Madness.ttf"); //The ResourceLoader.resourceExists() method says this .ttf exists.
            if(ResourceLoader.resourceExists("res/8-Bit-Madness.ttf"))
            {
                System.out.println("Text Exists");
            }
            else
            {
                System.out.println("Text load error");
            }
            Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
            awtFont = awtFont.deriveFont(36f); // set font size
            commonGameFont = new TrueTypeFont(awtFont,false);

        } catch (Exception e) {
            e.printStackTrace();
        }   
    }

    public void checkForInput()
    {
        int mouseXonScreen = Mouse.getX();
        int mouseYonScreen = Mouse.getY();
        while (Keyboard.next()) {
            if (Keyboard.getEventKeyState()) {
                if (Keyboard.getEventKey() == Keyboard.KEY_W) {
                    System.out.println("W Key Pressed");
                    wKeyDown = true;
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_A) {
                    System.out.println("A Key Pressed");
                    aKeyDown = true;
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_S) {
                    System.out.println("S Key Pressed");
                    sKeyDown = true;
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_D) {
                    System.out.println("D Key Pressed");
                    dKeyDown = true;
                }
            } 
            else 
            {
                if (Keyboard.getEventKey() == Keyboard.KEY_W) {
                    System.out.println("W Key Released");
                    wKeyDown = false;
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_A) {
                    System.out.println("A Key Released");
                    aKeyDown = false;

                }
                if (Keyboard.getEventKey() == Keyboard.KEY_S) {
                    System.out.println("S Key Released");
                    sKeyDown = false;
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_D) {
                    System.out.println("D Key Released");
                    dKeyDown = false;
                }
            }
        }

        if(gameScene == GAME_SCENE)
        {

            if(mouseXonScreen>mapOffsetX && mouseXonScreen<mapOffsetX+(tileWidth*gameMap.getWidth()))
            {
                if(mouseYonScreen>mapOffsetY && mouseYonScreen<mapOffsetY+(tileWidth*gameMap.getHeight()))
                {
                    mouseXonMap = (mouseXonScreen-mapOffsetX)/tileWidth;
                    mouseYonMap = (mouseYonScreen-mapOffsetY)/tileWidth;
                    tileTypeHighlighted = gameMap.getTileAt(mouseXonMap, mouseYonMap).getTileType();
                    mouseOnMap = true;
                }
                else
                {
                    mouseOnMap = false;
                }
            }
            else
            {
                mouseOnMap = false;
            }
        }
    }


    public void updateObjects()
    {
        if (gameScene == 0)
        {

            if (loadingBarWidth <= (width/2))
            {
                loadingBarWidth++;
            }
            else
            {
                gameScene = 2;
            }
        }
    }

    public void render()
    {
        if (gameScene == 0)
        {
            //These quads load properly, unless the blend line is in the program.
            GL11.glColor3f(0.5f,0.5f,0.5f);
            GL11.glBegin(GL11.GL_QUADS);
                GL11.glVertex2f((width/4),(height/4));
                GL11.glVertex2f((width/4),(height/4)+25);
                GL11.glVertex2f((width/4)*3,(height/4)+25);
                GL11.glVertex2f((width/4)*3,(height/4));
            GL11.glEnd();

            GL11.glColor3d(255d,255d,0d);
            GL11.glBegin(GL11.GL_QUADS);
                GL11.glVertex2f((width/4),(height/4));
                GL11.glVertex2f((width/4),(height/4)+25);
                GL11.glVertex2f((width/4)+loadingBarWidth,(height/4)+25);
                GL11.glVertex2f((width/4)+loadingBarWidth,(height/4));
            GL11.glEnd();
        }

        else if (gameScene == 2)
        {
            for(int x = 0; x<gameMap.getWidth(); x++)
            {
                for(int y = 0; y<gameMap.getHeight();y++)
                {
                    //These quads load correctly, unless that blend line above is active.
                    GL11.glColor3d(gameMap.getTileAt(x,y).getRColor(), gameMap.getTileAt(x,y).getGColor(), gameMap.getTileAt(x,y).getBColor());
                    GL11.glBegin(GL11.GL_QUADS);
                        GL11.glVertex2f(mapOffsetX+(x*tileWidth), mapOffsetY+(y*tileWidth)+tileWidth);
                        GL11.glVertex2f(mapOffsetX+(x*tileWidth), mapOffsetY+(y*tileWidth));
                        GL11.glVertex2f(mapOffsetX+(x*tileWidth)+tileWidth, mapOffsetY+(y*tileWidth));
                        GL11.glVertex2f(mapOffsetX+(x*tileWidth)+tileWidth, mapOffsetY+(y*tileWidth)+tileWidth);
                    GL11.glEnd();
                }
            }
            if(mouseOnMap)
            {
                commonGameFont.drawString(10,45, tileTypeHighlighted+ " Tile at X: "+ mouseXonMap +" Y: "+ mouseYonMap, Color.darkGray);
                commonGameFont.drawString(10,20, "This                             is a long space to test sizing ... ... ... ... ... ... ...",Color.white);
            }
        }   
    }


    public static void main(String[] args) {
        Game game = new Game();
        game.start();
    }
}

最佳答案

不在 initGL 中调用 glEnable(GL_BLEND),而是在绘制文本时调用 glEnable(GL_BLEND) 应该可以正常工作。使用您的代码的示例:

    if(mouseOnMap)
    {
        glEnable(GL_BLEND);
        commonGameFont.drawString(10,20, "This                             is a long space to test sizing ... ... ... ... ... ... ...",Color.white);
        commonGameFont.drawString(10,45, tileTypeHighlighted+ " Tile at X: "+ mouseXonMap +" Y: "+ mouseYonMap, Color.darkGray);
        glDisable(GL_BLEND);
    }

关于java - OpenGL (LWJGL+Slick-Util) - 文本无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26553916/

相关文章:

java - try-multicatch 与 ExceptionInInitializerError 和 ArithmeticException 混淆

c++ - 在可变大小的四边形上包装纹理坐标?

opengl - 为什么 OpenGL 的 glDrawArrays() 在核心配置文件 3.2 下会因 GL_INVALID_OPERATION 而失败,但在 3.3 或 4.2 下却不会?

c++ - 当仅需要 X-Y 旋转时,3D 对象沿所有三个轴旋转

java - Gdx exit 关闭整个应用程序 - 如何仅关闭 gdx 框架?

java - Java 的 OpenGL 数学 (GLM) 端口

java - 如何动态更改来自不同类的 View 背景颜色

java - JSP/Struts/Session 控制的 Web 应用程序中的爬虫

java - 带有 Unity 的 Google 云平台

java - 没有 glGet* 调用的 opengl 截锥体剔除