SLDT's GameEngine fails to start using LWJGL 3 (IllegalStateException)

Started by Yuri6037, July 11, 2015, 10:16:32

Previous topic - Next topic

Yuri6037

Hello all,

So after about 3 hours of updating, I finaly corrected all IDE errors with LWJGL 3.0.0a. However no game are running with the GameEngine. All games throws IllegalStateException : No OpenGL context is current in the current thread.

I don't know why it's happening I called GLContext.createFromCurrent after glfwMakeContextCurrent but it still throws the same error, and no windows are opening just the proload works fine (version/credits/logger creation).

Here is the code of the main GameEngine thread which manages all the load stuff :
    public void run() {
        try {
            glfwDefaultWindowHints();
            glfwWindowHint(GLFW_RESIZABLE, GL11.GL_FALSE);
            glfwWindowHint(GLFW_VISIBLE, GL11.GL_TRUE);
            if (isWindowed) {
                gameWindow = glfwCreateWindow(sizeX, sizeY, gameName, NULL, NULL);
                //Display.setDisplayMode(findDisplayMode(sizeX, sizeY, Display.getDisplayMode().getBitsPerPixel()));
            } else {
                gameWindow = glfwCreateWindow(sizeX, sizeY, gameName, glfwGetPrimaryMonitor(), NULL);
                //Display.setDisplayModeAndFullscreen(findDisplayMode(sizeX, sizeY, Display.getDisplayMode().getBitsPerPixel()));
            }
            //Display.setTitle(gameName);
            //Display.create();
            engineLogger.info("Starting LWJGL - OpenAL...");
            //AL.create();
            soundSystemContext = ALContext.create();
            engineLogger.info("LWJGL - OpenAL successfully started !");
            //Mouse.create();
            //Keyboard.create();
            glfwMakeContextCurrent(gameWindow);
            GLContext.createFromCurrent();
            initOpenGL();
            init();
            ByteBuffer[] shit = new ByteBuffer[2];
            if (getIconPackage() == null) {
                shit[0] = renderEngine.mountTexture("PNG", GameApplication.class.getResourceAsStream("icon16.png"));
                shit[1] = renderEngine.mountTexture("PNG", GameApplication.class.getResourceAsStream("icon32.png"));
            } else {
                shit[0] = renderEngine.mountTexture("PNG", ClassLoader.getSystemResourceAsStream("./" + getIconPackage() + "/gameIcon16.png"));
                shit[1] = renderEngine.mountTexture("PNG", ClassLoader.getSystemResourceAsStream("./" + getIconPackage() + "/gameIcon32.png"));
            }
            //Display.setIcon(shit); Will wait for next GLFW version
            while (glfwWindowShouldClose(gameWindow) == GL11.GL_FALSE) {
                curTime = getTime();
                //Display.update();
                gameTimer.updateTimer();
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
                for (int j = 0; j < gameTimer.elapsedTicks; j++) {
                    update();
                }
                glfwSwapBuffers(gameWindow); //Swap buffers
                render();

                updateFPS();
                prevTime = curTime;
                glfwPollEvents(); //Equivalent to Display.update()
            }
            closeGame();
        } catch (Exception e) {
            onGameCrash(e);
        }
    }


EDIT : I forget to say it's crashing at initOpenGL which means to run loading function and intializing function for OpenGL, this also preloads the AssetsManager which by definition will inject textures as raw in memory.

spasi

Are you spawning any threads in initOpenGL()? If anything was wrong before that, GLContext.createFromCurrent() would also throw an exception.

Yuri6037

The only thread that is created is the one created by setupMainThread function. The function creates a thread that takes as parameter the GameApplication class which implements the Runnable method I gave you.

Rectification, the error handler of the game I'm using to test the engine has a bug. I just fixed it and now it says IllegalStateException at org.lwjgl.opengl.GLContextWindows.createFromCurrent(GLContextWindows.java:61)
at org.lwgjl.opengl.GLContext.createFromCurrent(GLContext.java:36)
at net.sldt_team.gameEngine.GameApplication.run(GameApplication.java:719)
at java.lang.Thread.run(Thread.java:662)

EDIT : I just added a check if gameWindow == NULL right after glfwCreateWindow, and well you know what, it's NULL !! glfwCreateWindow is not working returning 0L !

Yuri6037

Well after adding glfwInit() the GameEngine started successfully however it's not the case of OpenGL. Every single function of OpenGL GL11 and event GL12 does NOTHING ! All I can get is black screen, nothing rendered, GL11.glVertex seams to be broken same as every OpenGL functions. All do nothing just black screen...

And second cursor problem. The cursor isn't grabbed like on the old LWJGL 2. I can go over the window with my cursor ; I can't block it inside the window...

EDIT : First problem fixed made glfwSwapBuffers after main game rendering function, now I have the video, a little bug with mouse position it seams that the screenHeight - mousePosY is no longer needed, and still the problem of the mouse not correctly grabbed.

EDIT 2 : A bug of GLFW is that it cannot run in non VSync which is praticaly unplayable on my computer because when I play a game at 60 FPS it's too slow for me : mouse does not answer enough quickly that's why I always play without VSync, but GLFW seams to not handle this case...

EDIT 3 : Fixed the mouse pos bug, fixed the vsync, now facing 2 problems :
- Sometimes the game freezes and restores that's strange.
- Mouse not being grabbed correctly.

EDIT 4 : LWJGL 3 is more lagging than LWJGL 2. I also prefer to rest on the LWJGL 2. I'm sorry to say that but that's the case, the games freezes randomly on LWJGL 3 but does never freeze on LWJGL 2. And apparently the mouse not being grabbed correctly is a bug of GLFW so it can't be solved...

EDIT 5 : Maybe it's not finished when removing 2 functions calls from GLFW game stops to freeze randomly :
glfwSetInputMode(gameWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
and
glfwSetInputMode(gameWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
This seams to be a problem in GLFW itself.

spasi

My guess is that you're misusing the API somehow. There haven't been any reports related to mouse grabbing or lagging/freezing, but to make sure:

- It'd be very helpful to see some code. Preferably a simple render loop that reproduces the issue.
- Post some info about your system (OS, GPU, etc).
- Make sure to register a GLFW error callback and an OpenGL debug callback (use GLContext.createFromCurrent().setupDebugMessageCallback(), in a debug context).
- The good old "-Dorg.lwjgl.util.Debug=true" is supported in LWJGL 3 too.

With regard to multi-threading, GLFW has strict requirements for many of its functions; most of them must only be called from the main thread. There are no restrictions to how many rendering threads you can have, but e.g. event handling must be done on the main thread. The javadoc on each function explains where and when it can be called.

Yuri6037

Please use my new topic the main problem exposed here is solved.