我正在使用Mac OS x。我的IDE是Intellij。出於某種原因,我在嘗試運行此代碼時遇到錯誤。該代碼是一個lwjgl遊戲引擎。GLFW引擎和主線程問題IntelliJ
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.glfw.Callbacks.*;
import org.lwjgl.*;
import java.awt.*;
import java.util.*;
import java.text.DateFormat;
import java.applet.Applet;
public class Main implements Runnable{
private int width = 1280;
private int height =720;
private String title = "Flappy";
private boolean running = false;
private Thread thread;
private long window;
public void start(){
running = true;
thread = new Thread(this, "Display");
//this will call the run method that we created below by using our implemented Runnable
thread.start();
}
public void run(){
init();
running = true;
while(running){
update();
render();
if(glfwWindowShouldClose(window)){
running = false;
}
}
}
//init initializes all of our stuff
private void init(){
if(!glfwInit()){
throw new IllegalStateException("Unable to initialize GLFW YOOO");
}
// Configure our window
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
window = glfwCreateWindow(width, height, title, NULL, NULL);
if(window == NULL){
throw new RuntimeException("Failed to create the GLFW window");
}
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - width)/2, (vidmode.height() - height)/2);
// Make the OpenGL context current
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);
// Make the window visible
glfwShowWindow(window);
}
public void update(){
glfwPollEvents();
}
public void render(){
glfwSwapBuffers(window);
}
public static void main(String[] args){
new Main().start();
}
}
閱讀一些文章我嘗試添加-XstartOnFirstThread編輯配置和程序參數,但沒有幫助。我的錯誤如果下面。如果有人可以幫忙,謝謝...再次使用Intellij的Mac。下面添加錯誤:
Caused by: java.lang.IllegalStateException: GLFW windows may only be created on the main thread and that thread must be the first thread in the process. Please run the JVM with -XstartOnFirstThread. For offscreen rendering, make sure another window toolkit (e.g. AWT or JavaFX) is initialized before GLFW.
可能重複的[GLFW窗口崩潰甚至與「-XstartOnFirstThread」在虛擬機參數](http://stackoverflow.com/questions/37333723/glfw-window-crash-even-with-xstartonfirstthread-in-vm-arguments) – CConard96
如何我可以更具體嗎?這個問題在另一篇文章中得到了解決,我在那裏提供的答案很自我解釋。 – CConard96
我改變它在主線程上運行它。我完全像他們在這裏顯示的那樣:https://www.lwjgl.org/guide仍然是相同的錯誤 – Boo