11

I'm creating a level editor for my game, and I ave a problem using LwjglCanvas with a JFrame. I use a JFrame (not a LwjglFrame) to keep engine and level editor as independent as possible. I have two JARs: WorldEditor.jar, and GameEngine.jar. Inside WorldEditor, I have a button called "test", that is suppose to load GameEngine.jar (if not already loaded) and launch (resart it if already loaded) it into the application main frame. Actually, what I do is injecting the WorldEditor game container (a JPanel inside the JFrame for example) to the game app, and use Gdx.app.postRunnable to add the lwjglcanvas to the injected game container :

World editor side:

JPanel _gameContainer = new JPanel(); // is inside a JFrame
MyGame game = loadGame(_gameContainer); // load the GameEngine JAR, and retrive the game

GameEngine side:

// container is the _gamecontainer of above
public void createGame(final Container gameContainer)  
{
    LwjglCanvas canvas = new LwjglCanvas(myapp, myconfig);
    Gdx.app.postRunnable(new Runnable()
    {
       public void run()
       {
           gameContainer.add(canvas.getCanvas());
       }
    });
}

The fact is that the postRunnable is never called (due to the fact that the app doesn't before being visible, am I wrong ?) I have been trying for a long time but no result ...

Does someone have an idea of what I could do to fix this problem ? Or a least another (let's say easier) method to do that ?

dooxe
  • 1,481
  • 12
  • 17

2 Answers2

12

This is how I do it:

public class EditorApp extends JFrame {

    public EditorApp() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final Container container = getContentPane();
        container.setLayout(new BorderLayout());

        LwjglAWTCanvas canvas = new LwjglAWTCanvas(new MyGame(), true);
        container.add(canvas.getCanvas(), BorderLayout.CENTER);

        pack();
        setVisible(true);
        setSize(800, 600);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new EditorApp();
            }
        });
    }
}

I also have some API on my game class making it easy to work with from my editor.

5

You have to use SwingUtilites.invokelater because postRunnable posts to the game loop which is not running. I would try to get a Component from MyGame and add this. If you return a Component you don't have a dep. to the LwjglCanvas. It's not that nice because now the MyGame Interface has a dep. to swing but it's worth a shot to see if its solves your problem.

morpheus05
  • 4,772
  • 2
  • 32
  • 47
  • I think that I have already tried that, but had some Threading issues. Anyway, I will take a look to your solution this evening thanks. – dooxe Jan 21 '14 at 13:14
  • Yeah ! using `SwingUtilities.invokeLater` works ! The game appears inside the JFrame. Now I have to deal with some other issues (application window won't close, and the game does not appear at it should). Thanks ! – dooxe Jan 21 '14 at 19:49
  • 3
    From the docs: Note that you may need to call stop() or a Swing application may deadlock on System.exit due to how LWJGL and/or Swing deal with shutdown hooks. http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/backends/lwjgl/LwjglCanvas.html#exit() – morpheus05 Jan 21 '14 at 20:15
  • Thanks, I fixed that and the application closes properly now :) – dooxe Jan 21 '14 at 22:51