2013-02-25 109 views
0

我在使用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(); 
    } 
} 
+0

我不知道這是不是你的NPE的原因,但根據的JavaDoc TrueTypeFont已經過時:http://slick.cokeandcode.com/javadoc/org/newdawn/slick/ TrueTypeFont.html改爲使用UnicodeFont。 – disperse 2013-02-25 20:34:54

回答

1

問題是你在initFont方法中重新聲明字體。更改initFont到:

public void initFont() 
{ 
    Font awtFont = new Font("Times New Roman", Font.BOLD, 24); 
    font = new TrueTypeFont(awtFont, antiAlias); 
} 
+0

仍然沒有解決問題:| – omtcyfz 2013-02-26 12:38:04

+0

它基本上處理的錯誤,但沒有發生在屏幕上:( – omtcyfz 2013-02-26 12:47:41

+0

我會建議實現遊戲界面:http://slick.cokeandcode.com/javadoc/org/newdawn/slick/Game.html並使用GameContainer setShowFPS方法:http://slick.cokeandcode.com/javadoc/org/newdawn/slick/GameContainer.html#setShowFPS(boolean) – disperse 2013-02-26 14:21:10