1

I am working on a 2D-3D Game on Java, with the help of Libgdx for the 3D Part. By now, I have menus and the 2D game written purely on Java, and I want to do the 3D part with Libgdx.

So I have managed to start a libgdx application (for desktop) and paint a plane and a box, and the box can be moved around, like the camera.

The problem is that only that part is written with the help of Libgdx. The menus and other stuff are written with Java and I don't want to change that.

So I clic a Button on the Java menu and the 3D game starts like this:

public static LwjglApplication main() {
    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    config.forceExit = false;
    config.height = Game.HEIGHT;
    config.width = Game.WIDTH;
    config.title = "Game 3D";
    return new LwjglApplication(new SuperBomberman3D(), config);
}

So now I get the new window, and that works. With F1, I close the game like this:

game3D.exit();

being Game3D the application returned by main().

And the next step is clicking the 3D Game button again, and the window opens again. Though, this time, the window is not the size I have said in config. That's weird.

Anyway, I try to open and close the 3D window a couple of times, and at some point, and exception is thrown:

Exception in thread "LWJGL Application" java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL11.glViewport(GL11.java:3261)
at com.badlogic.gdx.backends.lwjgl.LwjglGL20.glViewport(LwjglGL20.java:839)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.setWindowedMode(LwjglGraphics.java:491)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.setupDisplay(LwjglGraphics.java:166)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:142)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

I have looked over the Internet, but nothing seems to fit in my problem. I don't call any method, besides the exit() method, outside the SuperBomberman3D class. I don't know where this error is being thrown or why. Anyway, here is the SuperBomberman3D class, in case you see something wrong there:

public class SuperBomberman3D extends ApplicationAdapter implements
    ApplicationListener {

public static LwjglApplication main() {
    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    config.forceExit = false;
    config.height = Game.HEIGHT;
    config.width = Game.WIDTH;
    config.title = "Super Bomberman 3D";
    return new LwjglApplication(new SuperBomberman3D(), config);
}

public PerspectiveCamera cam;
public CameraInputController camController;

public Model bombermanModel;
public Model planeModel;
public Environment env;
public ModelBatch modelBatch;

public ModelInstance bomberman;
public ModelInstance plane;

private int FIELD_OF_VIEW = 67;

private Vector3 initialPosition = new Vector3(10f, 10f, 10f);
private Vector3 origin = new Vector3(0, 0, 0);
private float near = 1f;
private float far = 300f;

@Override
public void create() {
    camera();
    models();
    environment();
}

private void camera() {
    cam = new PerspectiveCamera(FIELD_OF_VIEW, Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());
    cam.position.set(initialPosition);
    cam.lookAt(origin);
    cam.near = near;
    cam.far = far;
    cam.update();

    camController = new CameraInputController(cam);
    Gdx.input.setInputProcessor(camController);
}

private void models() {
    modelBatch = new ModelBatch();
    ModelBuilder mb = new ModelBuilder();
    bombermanModel = mb.createBox(5f, 5f, 5f,
            new Material(ColorAttribute.createDiffuse(Color.RED)),
            Usage.Position | Usage.Normal);
    bomberman = new ModelInstance(bombermanModel);

    planeModel = mb.createRect(-10, 0, 10,
            10, 0, 10,
            10, 0, -10,
            -10, 0, -10,
            0, 1, 0,
            GL20.GL_TRIANGLES,
            new Material(
                new ColorAttribute(
                    ColorAttribute.createDiffuse(Color.BLUE)),
                new BlendingAttribute(
                    GL20.GL_SRC_ALPHA,
                    GL20.GL_ONE_MINUS_SRC_ALPHA)),
            VertexAttributes.Usage.Position |
            VertexAttributes.Usage.TextureCoordinates);
    plane = new ModelInstance(planeModel);
}

private void environment() {
    env = new Environment();
    env.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.2f, 0.2f,
            0.2f, 1f));
    env.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
}

@Override
public void render() {
    step();

    camController.update();
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    modelBatch.begin(cam);
    modelBatch.render(bomberman, env);
    modelBatch.render(plane, env);
    modelBatch.end();
}

public void step() {
    float x = 0;
    float z = 0;

    if(Gdx.input.isKeyPressed(Input.Keys.UP)){
        x = -0.2f;
    }
    if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){
        x = 0.2f;
    }
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
        z = 0.2f;
    }
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
        z = -0.2f;
    }

    bomberman.transform.translate(new Vector3(x, 0, z));

    if(Gdx.input.isKeyPressed(Input.Keys.F1)){
        StatesMachine.goToRoom(STATE.MAIN_MENU, false);
    }
}

@Override
public void dispose() {
    bombermanModel.dispose();
    planeModel.dispose();
    modelBatch.dispose();
}

}

So, the question is why is that error being thrown, how do i put things right, and, on other note, why the configuration is only set well once, when I call game3D.exit() to exit the 3D application and I redo the application with the main() method.

Lady Peanut
  • 53
  • 1
  • 6
  • Is your application multi-threaded? if so read this....[LibGDX Multithreading](https://github.com/libgdx/libgdx/wiki/Threading) it explains about the rendering thread and interactions with any other thread. – Mr00Anderson May 24 '16 at 15:51
  • But the thing is I only start and close the game in other thread, and no more operations considering 3D are outside the SuperBomberman3D class. Anyway, I tried putting the game3D.exit() part in a post-runnable, but it didn't work ): – Lady Peanut May 24 '16 at 16:00
  • Some other things that could be happening.... That you are calling on OpenGL stuff before its ready, as in constructing items that require LibGDX has finished establishing that. I know when i first started using LibGDX i had similar issues that I could no reproduce in a specific way. This might be something similar where it comes to being a race condition, where usually OpenGL Context set up wins, but in the case of application open and close, the OpenGL context might be loosing to your apps method calls. – Mr00Anderson May 24 '16 at 16:11
  • I don't know, the first two calls are okay. The second call just messes up with the window, dunno why, and the third **always** fails with the same exception. I mean, it's weird that the configuration setup only works once, and the third time always fails. – Lady Peanut May 24 '16 at 16:16
  • Instead of ending the application, why do you not create a sub application that has all of the game related cams, keys, code, ect and instead of calling .exit(); you write in some swapping method. You could make it easy by having a "running-game" deal and then swap apps in and out of the running game design. – Mr00Anderson May 24 '16 at 16:28
  • I don't think I understand what you are saying. I have my 2D game written purely in Java, and it doesn't have cams, for example. How can I swap two apps, anyway? – Lady Peanut May 24 '16 at 17:24

0 Answers0