GLFW引擎和主线程问题IntelliJ

时间:2016-06-22 21:37:07

标签: java macos intellij-idea lwjgl

我使用的是Mac OS x。我的IDE是Intellij。出于某种原因,我在尝试运行此代码时遇到错误。代码是一个lwjgl游戏引擎。

    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.

2 个答案:

答案 0 :(得分:6)

我在Intellij 2017.1.3中遇到了同样的错误; mac os 10.12.4; LWJGL释放-3.1

通过点击菜单 - 运行 - >解决编辑配置...然后将参数-XstartOnFirstThread添加到VM选项旁边的文本框中:

丝网印刷: enter image description here

答案 1 :(得分:0)

您遇到的例外情况非常清楚,应该从init()调用glfwCreateWindow()调用GLFW Window来创建main thread的{​​{1}}方法。因此,请将您的init()方法公开,并移除init();方法中的run()调用。而是将主要方法更改为此;

public static void main(String[] args){
   Main main = new Main();
   main.init();
   main.start();
}
相关问题