Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle resize in Libgdx on html (GwtApplication)

Tags:

java

gwt

libgdx

I have a problem with libgdx on html. I can not solve this problem two days. When i resize a browser the Gdx.graphics in my app does not change. I looked at source code GwtApplication, it was very difficult for me, and I understood Gdx.graphics must be change automatic, but it is not. Sorry for my bad english.

like image 216
Alexey Nikitin Avatar asked Mar 15 '14 04:03

Alexey Nikitin


1 Answers

First look at how to resize on user input. In your shared project code you could do:

    if(Gdx.app.getType() == ApplicationType.WebGL) {
        InputAdapter webGlfullscreen = new InputAdapter() {
            @Override
            public boolean keyUp (int keycode) {
                if (keycode == Keys.ENTER) {
                    if (!Gdx.graphics.isFullscreen()) Gdx.graphics.setDisplayMode(Gdx.graphics.getDisplayModes()[0]);
                } else if(keycode == Keys.UP || keycode == Keys.W) {
                    Gdx.graphics.setDisplayMode(Gdx.graphics.getWidth()+100, Gdx.graphics.getHeight()+100, false);
                } else if(keycode == Keys.DOWN  || keycode == Keys.S) {
                    Gdx.graphics.setDisplayMode(Gdx.graphics.getWidth()-100, Gdx.graphics.getHeight()-100, false);
                }
                return true;
            }
        };
        Gdx.input.setInputProcessor(webGlfullscreen);
    }

Adding 100 to each dimension is only correct if you have a square viewport. Adjust for your desired ratio.

LibGDX does not handle GWT window resizing you have to add some code to the -html project because LibGDX doesn't listen for browser resizing so you'll have to add in some GWT code yourself. In your GwtApplication subclass do:

public class GwtLauncher extends GwtApplication {

    @Override
    public void onModuleLoad () {
        super.onModuleLoad();
        com.google.gwt.user.client.Window.addResizeHandler(new ResizeHandler() {
              public void onResize(ResizeEvent ev) {
                  Gdx.graphics.setDisplayMode(ev.getWidth(),ev.getHeight(), false);
              }
            });
    }

    @Override
    public GwtApplicationConfiguration getConfig () {
        int height = com.google.gwt.user.client.Window.getClientHeight();
        int width = com.google.gwt.user.client.Window.getClientWidth();
        GwtApplicationConfiguration cfg = new GwtApplicationConfiguration(width, height);
        return cfg;
    }

    @Override
    public ApplicationListener getApplicationListener () {
        return new GdxDemoGame();
    }
}

Of course now you'll have to deal with aspect ratio issues just like you do on the desktop side.

p.s. I had to look in the GWT backend to figure out if there was already any resizing behavior. That is how I figured out that calls to setDisplayMode would adjust the resolution but also how I saw there was not already a resize listener. You can look yourself at https://github.com/libgdx/libgdx/blob/master/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/GwtApplication.java

like image 108
Chase Avatar answered Sep 22 '22 01:09

Chase