GLFW Window Crashing Even With “-XstartOnFirstThread” In VM Arguments

隐身守侯 提交于 2019-12-08 08:28:38

问题


I have "-XstartOnFirstThread" in my VM arguments, however I am still getting the error message:

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

My Code:

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

}

I am asking this question here because I have looked around and have not seen a solution posted for this anywhere else. Thank you for your help!


回答1:


Right now you are starting your program in the main thread, but immediately creating a new thread that creates the window. In LWJGL, you should do all GLFW calls and OpenGL rendering in the main thread. You can use other threads to build VBOs, load textures, calculate physics, etc.



来源:https://stackoverflow.com/questions/37333723/glfw-window-crashing-even-with-xstartonfirstthread-in-vm-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!