2016-05-19 5 views
1

Ich habe "-XstartOnFirstThread" in meiner VM Argumente, aber ich bin immer noch die Fehlermeldung bekommen:GLFW Fenster Crashing Sogar mit "-XstartOnFirstThread" In VM Argumente

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 

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

} 

Ich stelle diese Frage hier, weil ich mich umgesehen habe und nirgendwo anders eine Lösung gesehen habe. Danke für Ihre Hilfe!

Antwort

0

Jetzt starten Sie Ihr Programm im Haupt-Thread, aber sofort erstellen einen neuen Thread, der das Fenster erstellt. In LWJGL sollten Sie alle GLFW-Aufrufe und das OpenGL-Rendering im Hauptthread durchführen. Sie können andere Threads verwenden, um VBOs zu erstellen, Texturen zu laden, Physik zu berechnen usw.