gpt4 book ai didi

java - LWJGL 和 Slick2D 的文本错误

转载 作者:行者123 更新时间:2023-12-01 04:20:11 26 4
gpt4 key购买 nike

我一直在尝试使用 LWJGL 构建一个小程序,只是为了自学一些图形,当我尝试将文本叠加到我正在绘制的 2D 场景上时(这是一个不断从所有物体上反弹的盒子)窗口的两侧)文本变得模糊并且框反复闪烁 - /image/OATmd.jpg更奇怪的是,如果我复制 font.drawString 行,但让它显示单个单词而不是坐标,它根本不会闪烁,但仍然模糊 - /image/vnIK6.jpg .

有人能帮我解释为什么会发生这种情况以及如何解决它吗?我非常精通 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);
}
}

最佳答案

调用 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);

关于java - LWJGL 和 Slick2D 的文本错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19011174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com