【问题标题】:lwjgl 3 macOS issue with creating windowslwjgl 3 macOS 创建窗口的问题
【发布时间】:2020-11-23 21:46:42
【问题描述】:

我创建了一些代码来使用 lwjgl3,但由于我使用的是 Mac,因此遇到了 macOS 绑定问题。

问题是我似乎无法让我的程序创建任何窗口。我肯定知道这一点,因为程序就在那时停止。没有错误。

代码卡在的行是:

long window = glfwCreateWindow(300, 300, "Hello World!", MemoryUtil.NULL, MemoryUtil.NULL);

由于我在 IntelliJ 上进行开发,所以每当我断开进程时,它都会崩溃:

Exception Type: EXC_BAD_ACCESS (SIGABRT) 
Exception Codes: EXC_I386_GPFLT 
Exception Note: EXC_CORPSE_NOTIFY

我觉得这不是问题,因为我尝试运行另一个依赖于 lwjgl3 的代码,但它也卡在了创建窗口的部分。

我知道代码卡在创建窗口的部分,因为我在每一行都放置了打印语句。

我真的希望这不是与 macOS Big Sur 或 mac book pro 2015 型号的兼容性问题。

虽然glfwInit() 返回true,但当我运行System.out.println(glGetString(GL_VERSION) ); 时,我收到此错误Fatal error: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.

这是我用于测试的完整代码:

    public static void main(String[] args) throws Exception{
        GLFWErrorCallback.createPrint(System.err).set();


        // Initialize GLFW. Most GLFW functions will not work before doing this.
        if ( !glfwInit() )
            throw new IllegalStateException("Unable to initialize GLFW");

        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);


        // Configure GLFW
        glfwDefaultWindowHints(); // optional, the current window hints are already the default
        glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
        glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Should be true for macOS, according to GLFW docs, to get core profile.
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// According to Apple docs, non-core profiles are limited to version 2.1.
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        // Create the window
        long window = glfwCreateWindow(300, 300, "Hello World!", MemoryUtil.NULL, MemoryUtil.NULL);


        //glClearColor(1.0f, 0.0f, 0.0f, 0.0f);


        System.out.println('0');
        
        glfwDestroyWindow(window);
        


    }

我用谷歌搜索了很多次: 似乎找不到相关的答案。有人建议 macOS 有一个隐藏的弹出窗口,必须关闭它才能分配窗口,但我尽一切努力禁用它(因为我找不到它)但仍然没有发生任何事情。我认为GL_VERSION 表明 openGL 如何绑定到 Java 存在问题。

[编辑] 通过传入 JVM 选项 -XstartOnFirstThread 确保应用在主线程上运行。

[编辑2]

离开项目几个小时后,我回来重新运行它。 我在顶部多加了一行:

System.out.println("Hello LWJGL " + Version.getVersion() + "!");

createWindow(...) 函数开始工作,这很奇怪,我不知道为什么。如果我删除了线路,它也会开始工作,所以如果有人知道发生了什么,请告诉我。

我没有回答这个问题,因为我仍然不知道问题的原因,我很幸运它自己解决了。

[编辑3]

我在 intellij 上强制重建了整个项目,但它再次停止工作。我真的很困惑为什么它在两者之间工作

【问题讨论】:

  • 忘了提到我添加了标志。错误表示 Thread main 异常
  • 您能详细说明如何做到这一点吗?也许是一个链接。我确实看到在某个地方,但找不到更多信息
  • 据我所知,所有GL. createCapabilities() 等都发生在GLFW.createWindow(...) 调用之后。但这并不能解决createWindow 没有返回值的问题。我不认为这是解决办法:(
  • 如前所述,我已经搜索了使用建议的隐藏弹出窗口。我什至禁用了弹出窗口,清除了缓存以便未注册应用程序状态等。但似乎无法启动应用程序或让该弹出窗口出现

标签: java macos lwjgl glfw


【解决方案1】:

我在 gradle.properties 上有 -XstartOnFirstThread。尝试在您当前拥有的运行配置的 jvmargs 中设置它。这为我解决了问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多