2013-02-25 3 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

:에 initFont 변경 http://slick.cokeandcode.com/javadoc/org/newdawn/slick/ TrueTypeFont.html 대신 UnicodeFont를 사용하십시오. – disperse

답변

1

문제는 initFont 메소드에서 글꼴을 재 선언한다는 것입니다. JavaDoc을 따라이 당신의 NPE의 원인하지만 TrueTypeFont가 사용되지 않는 경우 나도 몰라

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

여전히 문제를 해결하지 못합니다. | – omtcyfz

+0

기본적으로 버그를 처리하지만 화면에서 아무 것도 일어나지 않습니다. ( – omtcyfz

+0

게임 인터페이스를 구현하는 것이 좋습니다. http://slick.cokeandcode.com/javadoc/org/newdawn/slick/Game.html 그리고 GameContainer 's setShowFPS 메서드 : http://slick.cokeandcode.com/javadoc/org/newdawn/slick/GameContainer.html#setShowFPS(boolean) – disperse