nicolasgramlich / AndEngine

Free Android 2D OpenGL Game Engine
http://www.andengine.org
Apache License 2.0
3.17k stars 1.35k forks source link

AndEngine does not allow detecting screen orientation changes. #266

Closed boscharun closed 9 years ago

boscharun commented 9 years ago

Am using the latest AndEngnie with camera and Engine initialized as

final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);

Also added the following in manifest.xml

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

And in the game class, I have onCnfigurationChanged defined, but it never gets triggered. Is there any way I can detect Screen orientation change while the game is going on?

taug commented 9 years ago

It seems only allow landscape.

boscharun commented 9 years ago

I quite don't understand +Taug... I can set the engine options to Portrait or Landscape and both works. What I want is to detect a screen orientation change while the user is playing the game. Looks like once the scene is visible, no orientation change triggers are available.

taug commented 9 years ago

Really?I will try later. Is the android API works for you?

boscharun commented 9 years ago

Hmmm I found it.. Seems this is intentional. BaseGameActivity does

this.setRequestedOrientation(orientation);

which basically sets the orientation permanently and no onConfiguationChanged() callbacks happen. I guess AndEngine was designed like this because a game is designed based on a fixed resolution and then scaled appropriately.

For my case, I was detecting whether portrait/landscape right at the start of the game and setting the engine appropriately.

So as a workaround, modified AndEngine code (BaseGameAcitivity.java) and introducing a new "NONE" orientation which would not call setRequestedOrientation at all. In my game class, I use onConfiguationChanged() to detect orientation change and to restart the current activity so it starts again with proper orientation.

ELY3M commented 9 years ago

I use this function in my wallpaper code. it works good for me.

@Override
public void onSurfaceChanged(final GLState pGLState, final int pWidth, final int pHeight) {
    super.onSurfaceChanged(pGLState, pWidth, pHeight);

    if ( (mEngine.getEngineOptions().getScreenOrientation() == ScreenOrientation.PORTRAIT_FIXED && pWidth > pHeight) ||
            (mEngine.getEngineOptions().getScreenOrientation() == ScreenOrientation.LANDSCAPE_FIXED && pHeight > pWidth)) {
        mEngine.getScene().setRotation(90f);
    } else {
        mEngine.getScene().setRotation(0f);
    }
}