How to run my game for different screen resolution using andengine

JohnRaja picture JohnRaja · Sep 24, 2011 · Viewed 12.3k times · Source

I am developing a game using andeninge. I fixed camera width and height

private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

@Override
public Engine onLoadEngine(){    
    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new  FillResolutionPolicy(), this.mCamera).setNeedsSound(true));     
    return engine;
}

In game the image of building size is (1020x400). The building view correctly when camera_widhth and camera_height is 480 ,320. How to run my game for different screen resolution using andengine (Using same building image size).

Otherwise i need to change building images for all screen resolution ?

Answer

vertti picture vertti · Sep 24, 2011

You can use fixed camera, like you do now, if you want to. OpenGL ES will scale the view to fill the device screen. This is probably the easiest solution but it will either change the aspect ratio or leave black boxes below/above or left/right of your screen when the game is run on device that has different aspect ratio than 1.5 (480/320). I think currently most devices have 1.66 aspect ratio (800/480).

Other option is to use:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics)
CAMERA_WIDTH = metrics.widthPixels()
CAMERA_HEIGHT = metrics.heightPixels()

to set your camera size and then use combination of the pixel sizes and the screens pixel density (dpi) (see http://developer.android.com/reference/android/util/DisplayMetrics.html) and the use .setScale to your Sprites to scale them accordingly.