即使在VM参数中使用“-XstartOnFirstThread”,GLFW窗口也会崩溃

时间:2016-05-19 20:46:57

标签: java lwjgl

我的VM参数中有“-XstartOnFirstThread”,但是我仍然收到错误消息:

Exception in thread "Thread-0" java.lang.ExceptionInInitializerError
    at org.lwjgl.glfw.GLFW.glfwCreateWindow(GLFW.java:1248)
    at Main.init(Main.java:33)
    at Main.run(Main.java:56)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: GLFW windows may only be created on the main thread.
    at org.lwjgl.glfw.EventLoop$OffScreen.<clinit>(EventLoop.java:39)
    ... 4 more

我的代码:

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

import org.lwjgl.glfw.*;


public class Main implements Runnable {

    private Thread thread;
    private boolean running;

    public long window;

    public static void main(String[] args) {
        Main game = new Main();
        game.start();
    }

    public void start() {
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public void init() {
        if(glfwInit() != GL_TRUE) {
            System.err.println("GLFW Initialization Failed!");
        }

        glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

        window = glfwCreateWindow(800, 600, "test", NULL, NULL);

        if(window == NULL) {
            System.err.println("Could not create our window!");
        }

        GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(window, 100, 100);

        glfwMakeContextCurrent(window);

        glfwShowWindow(window);
    }

    public void update() {
        glfwPollEvents();
    }

    public void render() {
        glfwSwapBuffers(window);
    }

    public void run() {
        init();
        while(running) {
            update();
            render();

            if(glfwWindowShouldClose(window) == GL_TRUE) {
                running = false;
            }
        }
    }

}

我在这里问这个问题,因为我环顾四周,并没有看到在其他地方发布的解决方案。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

现在您正在主线程中启动程序,但立即创建一个创建窗口的新线程。在LWJGL中,您应该在主线程中执行所有GLFW调用和OpenGL渲染。您可以使用其他线程来构建VB​​O,加载纹理,计算物理等等。