LWJGL을 사용하여 화면에서 FPS를 추적하려고 할 때 몇 가지 문제가 있습니다. 내 코드에서 무엇이 잘못 되었습니까?lwjgl fps 그리기 문제
나는이 모든 시간을 가지고 :
Exception in thread "main" java.lang.NullPointerException
at TheGame.renderFont(TheGame.java:66)
at TheGame.start(TheGame.java:48)
at TheGame.main(TheGame.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code 1
코드는, 여기에 꽤 간단합니다 - 그것은 기본적으로 창을 열고 FPS를 계산하고, 타이틀로 추적하면 FPS가 괜찮아하지만 어떻게 오류없이 문자열을 그립니다.
import java.awt.Font;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.Sys;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.util.ResourceLoader;
public class TheGame
{
int fps;
long lastFPS;
TrueTypeFont font;
boolean antiAlias = true;
public void start()
{
try
{
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
initFont();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// init OpenGL here
lastFPS = getTime(); // call before loop to initialise fps timer
while (!Display.isCloseRequested())
{
// render OpenGL here
updateFPS();
Display.update();
}
Display.destroy();
}
public void initFont()
{
Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
TrueTypeFont font = new TrueTypeFont(awtFont, antiAlias);
}
public void updateFPS()
{
if (getTime() - lastFPS > 1000)
{
font.drawString(100, 50, "THE LIGHTWEIGHT JAVA GAMES LIBRARY", Color.yellow);
fps = 0; //reset the FPS counter
lastFPS += 1000; //add one second
}
fps++;
}
public long getTime()
{
return (Sys.getTime() * 1000)/Sys.getTimerResolution();
}
public static void main(String[] argv)
{
TheGame displayExample = new TheGame();
displayExample.start();
}
}
:에 initFont 변경 http://slick.cokeandcode.com/javadoc/org/newdawn/slick/ TrueTypeFont.html 대신 UnicodeFont를 사용하십시오. – disperse