Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am developing a game with 1024x600 resolution. I can't target smaller resolution devices at this resolution, so I intend to use two graphics for the game. How do I detect which graphics to use for which device?

share|improve this question

2 Answers 2

In your main activity, you can call:

DisplayMetrics displayMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

displayMetrics will then give you a bunch of data, for example:

displayMetrics.density;

displayMetrics.densityDpi;

displayMetrics.heightPixels;

And so forth. In the comments below, there is a request for sample code to handle variable screen resolutions (i.e., to eliminate letterboxing). Sample code to accomplish this is available here.

share|improve this answer
    
I want to ask that is it correct to use two graphics? Because game size increased atleast 50% of normal game size. –  Siddharth Feb 24 '13 at 4:34
    
@Siddharth It's not "incorrect" in any official sense, although it's more common with AndEngine to develop at a lower resolution and let the engine scale it up. –  Cameron Fredman Feb 24 '13 at 4:35
    
@Siddharth for what it's worth, I use the displayMetrics just to determine the aspect ratio of the device. In other words, I define a set height, and use the aspect ratio to figure out the width. AndEngine is really designed to take care of scaling for you. –  Cameron Fredman Feb 24 '13 at 4:40
    
Please you can provide some code snippet for what you said and what graphics as you perspective I need to prepare from graphics designer. Thank for your response. –  Siddharth Feb 24 '13 at 4:44
    
@Siddharth added some code per your request. –  Cameron Fredman Feb 24 '13 at 4:49

I have found other solution for this and test it my self.

Dealing with all the device resolutions scattered around the globe. The existing resolution policies offer either a stretch-to-fit scale, black bars or leave it to you to do the scaling.

The class I have here is another option that scales your screen up to fill the screen, whilst retaining the 1:1 assets ratio of your graphics.

public class CroppedResolutionPolicy extends BaseResolutionPolicy
{

    private final float mCameraWidth;
    private final float mCameraHeight;
    private float mMarginVertical;  // Pixels from top of canvas to visible area, and from bottom of canvas to visible area
    private float mMarginHorizontal;        // Pixels from left of canvas to visible area, and from right of canvas to visible area


    public CroppedResolutionPolicy(final float nCameraWidth, final float nCameraHeight)
    {
            this.mCameraWidth = nCameraWidth;
            this.mCameraHeight = nCameraHeight;

            this.mMarginVertical = 0;
            this.mMarginHorizontal = 0;
    }

    // ===========================================================
    // Getter & Setter
    // ===========================================================
    public float getMarginVertical()
    {
            return this.mMarginVertical;
    }

    public float getMarginHorizontal()
    {
            return this.mMarginHorizontal;
    }


    @Override
    public void onMeasure(RenderSurfaceView pRenderSurfaceView, int pWidthMeasureSpec, int pHeightMeasureSpec)
    {
            BaseResolutionPolicy.throwOnNotMeasureSpecEXACTLY(pWidthMeasureSpec, pHeightMeasureSpec);

            int measuredWidth = MeasureSpec.getSize(pWidthMeasureSpec);
            int measuredHeight = MeasureSpec.getSize(pHeightMeasureSpec);

            final float nCamRatio = (float)mCameraWidth /  (float)mCameraHeight;
            final float nCanvasRatio = (float)measuredWidth /  (float)measuredHeight;

            if(  (float)measuredWidth /  (float)measuredHeight < nCamRatio )
            {
                    // Scale to fit height, width will crop
                    measuredWidth = (int) ( measuredHeight * nCamRatio);
                    this.mMarginHorizontal = ( this.mCameraWidth - ( (float) this.mCameraHeight * nCanvasRatio ) ) / 2.0f;
            }
            else
            {
                    // Scale to fit width, height will crop
                    measuredHeight = (int) ( measuredWidth / nCamRatio );
                    this.mMarginVertical = ( this.mCameraHeight - ( (float) this.mCameraWidth / nCanvasRatio ) ) / 2.0f;
            }

            pRenderSurfaceView.setMeasuredDimensionProxy(measuredWidth, measuredHeight);
    }
}

Use this class in engine creation in the following manner.

final CroppedResolutionPolicy canvasSurface = new CroppedResolutionPolicy( CAMERA_WIDTH, CAMERA_HEIGHT );
final EngineOptions engineOptions = new EngineOptions( true, ScreenOrientation.LANDSCAPE_FIXED, canvasSurface, new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT) );
share|improve this answer
    
For those looking for the author/source of this code and some discussion about it: andengine.org/forums/gles2/… –  Cameron Fredman Feb 28 '13 at 17:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.