LWJGL和Slick2D出现文本错误

时间:2013-09-25 17:11:37

标签: java text lwjgl slick2d

我一直在尝试使用LWJGL构建一个小程序,只是为了自学一些图形,当我尝试将文本叠加到我正在绘制的2D场景上时(这是一个不断反弹的盒子)窗口的两侧)文本变得模糊,盒子反复闪烁 - http://imgur.com/UDelj1d陌生人仍然,如果我复制font.drawString行,但让它显示一个单词而不是它不闪烁的共同点完全但仍然模糊 - http://imgur.com/3VkTSXH

是否有人能够帮助我解决为什么会发生这种情况以及如何解决这个问题?我非常精通Java,但这是我第一次尝试以图形为导向。

import java.nio.FloatBuffer;
import java.text.DecimalFormat;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

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

import org.lwjgl.BufferUtils;

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


public class TimerDemo
{
//time in ms since the last frame was rendered
private static long lastFrame;

//current time in ms
private static long getTime()
{
    return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}

//gets the difference between the current time and lastFrame, then updates lastFrame
private static double getDelta()
{
    long currentTime = getTime();
    double delta = (double) (currentTime - lastFrame);
    lastFrame = getTime();
    return delta;
}


private static UnicodeFont font;
private static void setUpFonts()
{
    java.awt.Font awtFont = new java.awt.Font("Arial", java.awt.Font.PLAIN, 12);
    font = new UnicodeFont(awtFont);
    font.getEffects().add(new ColorEffect(java.awt.Color.white));
    font.addAsciiGlyphs();

    try
    {
      font.loadGlyphs();
    } 
    catch (SlickException e)
    {
        e.printStackTrace();
    }
}

public static void main(String[] args)
{
    try
    {
        Display.setDisplayMode(new DisplayMode(680, 480));
        Display.setTitle("Timer Demo");
        Display.create();
    }
    catch (LWJGLException e)
    {
        e.printStackTrace();
        Display.destroy();
        System.exit(1);
    }

    setUpFonts();

    //position
    int x = 100;
    int y = 100;

    //velocity
    int dx = 1;
    int dy = 1;

    //ensure lastframe is updated
    lastFrame = getTime();

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 680, 480, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);


    while (!Display.isCloseRequested())
    {
        // Render

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();

        //change in position
        double delta = getDelta();

        if(x + 30 >= 680)
            dx = -1;
        else if (x <= 0)
            dx = 1;

        if(y + 30 >= 480)
            dy = -1;
        else if (y <= 0)
            dy = 1;

        x += delta * dx * 0.1;
        y += delta * dy * 0.1;

        glRecti(x, y, x + 30, y + 30);

        font.drawString(100, 10, "x is " + x + " y is " + y);


        Display.update();
        Display.sync(60);
        }

    Display.destroy();
    System.exit(0);
}
}

1 个答案:

答案 0 :(得分:0)

致电glEnable(GL_TEXTURE_2D);font.drawString(100, 10, "x is " + x + " y is " + y); glDisable(GL_TEXTURE_2D);而非font.drawString(100, 10, "x is " + x + " y is " + y);