threerings / playn

Legacy 1.x version of PlayN library.
http://playn.io/
Apache License 2.0
195 stars 66 forks source link

Shutdown GLView completely #64

Closed tomfisher closed 10 years ago

tomfisher commented 10 years ago

I am trying port my application in which the game is integrated as a view controller. The game view will be shown as blank at the second time we run the game.

It is probably caused by GL drawing that is not shutdown completely. I did not find the equivalent function of gameView.Stop() in the Xamarin implementation.

The following is the code I used:

public class Demo extends UIApplicationDelegateAdapter {

private UIWindow window;

@Override
public boolean didFinishLaunching(UIApplication app, UIApplicationLaunchOptions launchOpts) {
    window = new UIWindow(UIScreen.getMainScreen().getBounds());
    window.setRootViewController(new MainController());
    window.makeKeyAndVisible();
    return true;
}

public static void main(String[] args) {
    NSAutoreleasePool pool = new NSAutoreleasePool();
    UIApplication.main(args, null, Demo.class);
    pool.close();
}

}

public class MainController extends UIViewController {

public MainController() {
    super();
    UIButton button = new UIButton();
    button.setTitle("Run", UIControlState.Normal);
    setView(button);
    button.addOnTouchUpInsideListener(new OnTouchUpInsideListener() {
        @Override
        public void onTouchUpInside(UIControl control, UIEvent event) {
            runGame();
        }
    });
}

private void runGame() {
    RoboPlatform.Config config = new RoboPlatform.Config();
    config.embedded = true;
    RoboPlatform platform = (RoboPlatform) PlayN.platform();
    platform = RoboPlatform.register(UIApplication.getSharedApplication(), UIApplication.getSharedApplication()
            .getWindows().get(0), config);

    // reference this to avoid GC collection.
    addStrongRef(platform);
    // Start the game
    Game game = new TheGame(this);
    PlayN.run(game);
    // We have to manually active the platform since it is embedded in our app.
    platform.activate();

}

private static class TheGame extends Game.Default {
    final private UIViewController from;

    public TheGame(UIViewController from) {
        super(30);
        this.from = from;
    }

    @Override
    public void init() {
        CanvasImage image = PlayN.graphics().createImage(200, 200);
        image.canvas().setFillColor(Colors.RED).fillRect(0, 0, 200, 200);
        ImageLayer layer = PlayN.graphics().createImageLayer(image);
        layer.addListener(new Pointer.Adapter() {
            @Override
            public void onPointerEnd(Event event) {
                UIApplication.getSharedApplication().getWindows().get(0).setRootViewController(from);
                RoboPlatform platform = (RoboPlatform) PlayN.platform();
                platform.terminate();
                PlayN.setPlatform(null);
                from.removeStrongRef(platform);
            }
        });
        PlayN.graphics().rootLayer().addAt(layer, 20, 20);
    }
}

}

tomfisher commented 10 years ago

I submitted a patch.