我的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;
            }
        }
    }

}


我在这里问这个问题是因为我环顾四周,还没有看到针对此问题的解决方案。谢谢您的帮助!

最佳答案

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

关于java - 即使VM参数中出现“-XstartOnFirstThread”,GLFW窗口也会崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37333723/

10-12 19:19