5

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 ?

JohnRaja
  • 2,377
  • 6
  • 26
  • 47

4 Answers4

5

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.

vertti
  • 7,539
  • 4
  • 51
  • 81
4

Andengine games will scale onto the device natively - so...

If you set your CAMERA_WIDTH/HEIGHT to 480x320 and someone runs it on a phone which is 800x480 your game will be scaled-up to 720x480 (1.5x) and there will be an 80px margin (top, bottom, left, right or split between them depending on your View's gravity).

You have to decide what most of the people using your App will be using - I tend to target 800x480 and live with some shrinkage to smaller screens - rather than the other way around...

1

You can use this Source :

@Override
public Engine onLoadEngine() {
     final Display defaultDisplay = getWindow().getWindowManager().getDefaultDisplay();
     CAMERA_WIDTH = defaultDisplay.getWidth();
     CAMERA_HEIGHT = defaultDisplay.getHeight();
     this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
     return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
 }
SukiCZ
  • 300
  • 5
  • 10
1

AndEngine by default assumes that you want a fixed resolution policy. But you can also change it, as given in the following link,

http://android.kul.is/2013/10/andengine-tutorial-dealing-with-screen-sizes.html

Or, you can follow this code and modify your code accordingly. (My preference)

// Calculate the aspect ratio ofthe device.
float aspectRatio = (float) displayMetrics.widthPixels / (float) displayMetrics.heightPixels;

// Multiply the aspect ratio by the fixed height.
float cameraWidth = Math.round(aspectRatio * CAMERA_HEIGHT);

// Create the camera using those values.
this.mCamera = new Camera(0, 0, cameraWidth, cameraHeight);

// Pick some value for your height.
float cameraHeight = 320;

// Get the display metrics object.
final DisplayMetrics displayMetrics = new DisplayMetrics();

// Populate it with data about your display.
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);