I've been attempting to build a small program with the LWJGL just to go teach myself a bit of graphics, when I try to overlay text onto a 2D scene i'm drawing (this being a box that continuously bounces off all the sides of the window) the text becomes blurred and the box flickers repeatedly - https://i.stack.imgur.com/OATmd.jpg Stranger still, if I copy the font.drawString line but have it display a single word instead of the co-ords it doesn't flicker at all but is still blurry - https://i.stack.imgur.com/vnIK6.jpg.
Would anyone be able to help me with why this is happening and how I would fix it? I'm quite proficient in Java but this is the first time i've attempted anything graphics orientated.
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);
}
}
Copyright Notice:Content Author:「Pastadin」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19011174/text-error-with-lwjgl-and-slick2d