有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java lwjgl在创建字体对象时冻结

我正在做一个简单的蛇游戏,因为我能,我偶然发现了一个有趣的虫子。我试图使用Slick Util在屏幕上绘制一些文本,但每当我尝试创建字体对象时,游戏就会冻结Here是我的代码。突出显示的部分只打印一次“a”

package tlf.snake.main;


import tlf.snake.main.game.helper.Pos;
import tlf.snake.main.game.input.Input;

import java.awt.Font;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFWWindowPosCallback;
import org.lwjgl.glfw.GLFWWindowSizeCallback;
import org.lwjgl.glfw.GLFWvidmode;
import org.lwjgl.opengl.GLContext;

import org.newdawn.slick.TrueTypeFont;

import static org.lwjgl.glfw.Callbacks.glfwSetCallback;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL;

/**
 * @author thislooksfun
 */
public class SnakeGame
{
    private static final Game game = new Game();

    private int WIDTH = game.gameWidth*15;
    private int HEIGHT = game.gameHeight*15;

    private static final int MINWIDTH = game.gameWidth*10;
    private static final int MINHEIGHT = game.gameHeight*10;

    private Pos lastPos;

    private int size = 10;
    private boolean resized = false;

    private boolean running = false;

    private long window;

    private static SnakeGame instance;

    public TrueTypeFont font;

    public SnakeGame()
    {
        instance = this;
    }

    private void init()
    {
        if (glfwInit() != GL_TRUE) {
            System.out.println("Error initializing");
            //TODO handle
            return;
        }

        glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

        window = glfwCreateWindow(WIDTH, HEIGHT, "NEAT Snake", NULL, NULL);

        if (window == NULL) {
            System.out.println("Error creating window");
            //TODO handle
            return;
        }

        ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(window, (GLFWvidmode.width(vidmode) - WIDTH) / 2, (GLFWvidmode.height(vidmode) - HEIGHT) / 2);
        initCallbacks();
        glfwMakeContextCurrent(window);
        glfwShowWindow(window);

        GLContext.createFromCurrent();

        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glDepthFunc(GL_NEVER);
        glDisable(GL_DEPTH_TEST);

        setOrtho();
        glDisable(GL_TEXTURE_2D);

        initFonts();

        running = true;
        resized = true;

        game.start();
    }

    private void initCallbacks()
    {
        glfwSetCallback(window, GLFWWindowSizeCallback(new GLFWWindowSizeCallback.SAM()
        {
            @Override
            public void invoke(long window, int width, int height)
            {
                resized = true;
                WIDTH = width;
                HEIGHT = height;

                if (width < MINWIDTH && height >= MINHEIGHT) {
                    glfwSetWindowSize(window, MINWIDTH, height);
                    glfwSetWindowPos(window, lastPos.x-10, lastPos.y);
                } else if (width >= MINWIDTH && height < MINHEIGHT) {
                    glfwSetWindowSize(window, width, MINHEIGHT);
                    glfwSetWindowPos(window, lastPos.x-10, lastPos.y);
                } else if (width < MINWIDTH && height < MINHEIGHT) {
                    glfwSetWindowSize(window, MINWIDTH, MINHEIGHT);
                    glfwSetWindowPos(window, lastPos.x-10, lastPos.y);
                } else
                    lastPos = getWindowPos();

                render();
                setOrtho();
            }
        }));

        glfwSetCallback(window, GLFWWindowPosCallback(new GLFWWindowPosCallback.SAM()
        {
            @Override
            public void invoke(long window, int xpos, int ypos)
            {
                lastPos = new Pos(xpos, ypos);
            }
        }));

        glfwSetKeyCallback(window, new Input());
    }

    private Pos getWindowPos()
    {
        IntBuffer xbuf = BufferUtils.createIntBuffer(4);
        IntBuffer ybuf = BufferUtils.createIntBuffer(4);
        glfwGetWindowPos(window, xbuf, ybuf);

        return new Pos(xbuf.get(0), ybuf.get(0));
    }

    private void setOrtho()
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);
    }

    private void initFonts()
    {
        System.out.println("a");
        Font awtFont = new Font("Default", Font.BOLD, 24);
        System.out.println("a");
        font = new TrueTypeFont(awtFont, true);
        System.out.println("a");
    }

    public void start()
    {
        init();
        while (running)
        {
            update();
            render();
            if (glfwWindowShouldClose(window) == GL_TRUE) {
                running = false;
            }
        }
    }

    private void update()
    {
        glfwPollEvents();
        game.tick();
    }

    private void render()
    {
        if (resized) {
            setOrtho();
            calcTileSize();
            resized = false;
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        game.render();

        glfwSwapBuffers(window);
    }

    public static SnakeGame instance()
    {
        return instance;
    }

    private void calcTileSize()
    {
        int sh, sw;
        sh = (int)Math.floor((1.0f*HEIGHT)/game.gameHeight);
        sw = (int)Math.floor((1.0f*WIDTH)/game.gameWidth);

        size = sh > sw ? sw : sh;
    }

    public int tileSize()
    {
        return size;
    }

    public void stop()
    {
        running = false;
    }
}

有什么想法吗
谢谢,
-tlf


共 (0) 个答案