java - lwjgl fps绘图问题

标签 java opengl lwjgl slick2d

我在尝试使用 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 方法中重新声明字体。将 initFont 更改为:

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

关于java - lwjgl fps绘图问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15075419/

相关文章:

java - LWJGL 基础 : Drawing Quads At Depth

javarabbitmq确认无效

java - NoClassDefinitionFound : javax. xml.ws.Service

algorithm - 三角扇的纹理坐标

c++ - 使用立即模式绘制纹理的 OpenGL 替代方案?

java - 具有浮点和字节的交错 VBO

java - 控制台输出 Selenium Webdriver

java - 有没有办法改变 JComboBox 的项目焦点?

java - 从另一个线程中的对象运行方法

java - 如何使用 LWJGL 进行离屏渲染?