Search code examples
javamacoslwjglglfw

lwjgl 3 macOS issue with creating windows


I created some code to work with lwjgl3, and ran into an issue with the macOS bindings since I am working on a mac.

The issue is that I cannot seem to get my program to create any windows. I know this for sure as the program just stops at that point. There are no errors.

The line the code gets stuck on is:

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

Since I am developing on IntelliJ, whenever I disconnect the process, it crashes with :

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

I feel that that is not the issue, as I tried running another code dependant on lwjgl3, and it also got stuck at the part creating the window.

I know that the code is stuck at the part to create the window as I have put print statements every line.

I really hope that this is not a compatibility issue with either macOS Big Sur or the mac book pro 2015 model.

while glfwInit() return true, when I run System.out.println(glGetString(GL_VERSION) );, I get this error 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.

Here is the complete code that I am using for testing:

    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);
        


    }

I have googled this multiple times: cannot seem to find a relevant answer. One person suggested that macOS has a hidden popup that had to be closed for the window to be assigned, but I did everything to disable it (as I could not find it) and still nothing happened. I think the GL_VERSION indicates an issue with how openGL is being bound to Java.

[Edit] Ensured that the app was running on the main thread by passing in the JVM option -XstartOnFirstThread.

[EDIT2]

After leaving the project for a few hours, I came back and reran it. I added one extra line on the top:

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

The createWindow(...) function started working, which is weird as I do not know why. It also started working if I removed the line, so if anyone knows what was happening please let me know.

I am leaving the question unanswered as I still do not know the reason for the issue, I just happened to be lucky that it fixed itself.

[EDIT3]

I force rebuilt the entire project on intellij and it stopped working again. Am really confused as to why it was working in between


Solution

  • I had the -XstartOnFirstThread on the gradle.properties. Try set it in the jvmargs of the run configuration you currently have. That fixed it for me