Intellij Idea GLFW引擎和IntelliJ主线程问题

bjp0bcyl  于 2023-08-03  发布在  其他
关注(0)|答案(4)|浏览(136)

我用的是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.

qyzbxkaa

qyzbxkaa1#

我在Intellij 2017.1.3中遇到了同样的错误; Mac OS 10.12.4; lwjgl-release-3.1
它解决了通过点击菜单-运行->编辑配置...然后将参数-XstartOnFirstThread添加到VM选项旁边的文本框中:
丝网印刷:x1c 0d1x的数据

wi3ka0sx

wi3ka0sx2#

对于缺少VM选项的用户,此字段将隐藏,如下所示:
运行->编辑配置... ->修改选项下拉菜单->添加VM选项-> -XstartOnFirstThread
x1c 0d1x的数据

ajsxfq5m

ajsxfq5m3#

你得到的异常非常清楚这个问题,你的init()方法调用glfwCreateWindow()来创建GLFW Window应该从main thread调用。因此,将init()方法设为公共,并删除run()方法中的init();调用。将你的main方法改为这样;

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

字符串

yacmzcpb

yacmzcpb4#

如果您使用的是初始化器构建的build.gradle文件,则可以添加此部分,以指定Java命令行选项:

application {
    applicationDefaultJvmArgs = ['-XstartOnFirstThread']
}

字符串

相关问题