user5049277
user5049277

Reputation:

Adding multiple windows in LibGDX?

I want to create multiple game windows from my DekstopLauncher.java . The second one should be able to interact with the first one, for example asking for a string.

Is it possible to do so? And how?

This is what I tried in the DeskopLauncher class:

This is creating the first window:

LwjglApplicationConfiguration configForTiles = new LwjglApplicationConfiguration();
TilePresets tilesWindow = new TilePresets();
LwjglApplication tiles = new LwjglApplication(tilesWindow, configForTiles);

And this the second

LwjglApplicationConfiguration configForMap = new LwjglApplicationConfiguration();
MapMaker mapWindow = new MapMaker();
LwjglApplication map = new LwjglApplication(mapWindow, configForMap);

Creating only one works. But if add both it gives me this error:

LwjglApplication: Couldn't initialize audio, disabling audio
java.lang.IllegalStateException: Only one OpenAL context may be instantiated at any one time.
at org.lwjgl.openal.AL.create(AL.java:113)
at org.lwjgl.openal.AL.create(AL.java:102)
at org.lwjgl.openal.AL.create(AL.java:206)
at com.badlogic.gdx.backends.lwjgl.audio.OpenALAudio.(OpenALAudio.java:72)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.(LwjglApplication.java:83)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.(LwjglApplication.java:64)
at com.whoplays.mapmaker.desktop.DesktopLauncher.main(DesktopLauncher.java:16)
Exception in thread "LWJGL Application" java.lang.IllegalStateException: From thread Thread[LWJGL Application,5,main]: Thread[LWJGL Application,5,main] already has the context current at org.lwjgl.opengl.ContextGL.checkAccess(ContextGL.java:184)
at org.lwjgl.opengl.ContextGL.forceDestroy(ContextGL.java:241)
at org.lwjgl.opengl.DrawableGL.destroy(DrawableGL.java:130)
at org.lwjgl.opengl.Display$5.destroy(Display.java:834)
at org.lwjgl.opengl.Display.destroy(Display.java:1095)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.createDisplayPixelFormat(LwjglGraphics.java:197)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.setupDisplay(LwjglGraphics.java:174)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:138)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)

Upvotes: 4

Views: 3325

Answers (1)

asherbret
asherbret

Reputation: 6018

Basically, you can run each window in a separate process (use the answer here to see how to implement JavaProcess which is used below):

public class Tiles {
   public static void main(String[] args) {
      LwjglApplicationConfiguration configForTiles = new LwjglApplicationConfiguration();
      TilePresets tilesWindow = new TilePresets();
      LwjglApplication tiles = new LwjglApplication(tilesWindow, configForTiles);
   }
}

Wrapper.java is the main entry point. It's where launching both windows occurs:

public class Wrapper {
   public static void main(String[] args) {
      // Launch mapWindow regularly 
      LwjglApplicationConfiguration configForMap = new LwjglApplicationConfiguration();
      MapMaker mapWindow = new MapMaker();
      LwjglApplication map = new LwjglApplication(mapWindow, configForMap);

      try {
         int res = JavaProcess.exec(Tiles.class); // Where the second window is shown
      } catch (IOException e) {
         e.printStackTrace();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }
}

Credit alert! I had a similar question and I found this solution somewhere, but I can't remember where. I'll post the source here when I find it.

Edit: Credit goes to the person from which I got the idea for this solution.

Upvotes: 4

Related Questions