jameshwart lopez
jameshwart lopez

Reputation: 3131

AndEngine Full Image Background (width and height)

I'm really new to Android game development and I am facing an issue about screen image background.

Here is my full class code that extends SimpleBaseGameActivity
UPDATED camera width and height that corresponds to image dimensions

private final int CAMERA_WIDTH = 480;//800;
private final int CAMERA_HEIGHT = 836; //1280;

private Camera mCamera;

//Background variables
private TextureRegion region;

private Scene mScene;

@Override
public EngineOptions onCreateEngineOptions() {
    this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);

    return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}

@Override
protected void onCreateResources() throws IOException {
    BitmapTextureAtlas atlas = new BitmapTextureAtlas(this.getTextureManager(),CAMERA_WIDTH+CAMERA_WIDTH,CAMERA_HEIGHT+CAMERA_HEIGHT);

    this.region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas,this,"snow_bg.png",0,0);
    atlas.load();

}

@Override
protected Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    this.mScene = new Scene();
    Sprite sprite = new Sprite(0,0,region,this.getVertexBufferObjectManager());
    this.mScene.setBackground(new SpriteBackground(sprite));

    return this.mScene;
}

The issue that I am facing is the background image, which is not in full screen. I've been searching how to stretch image background in AndEngine but I haven't found any good resources.

Here is the screen shot.

enter image description here

But the image full screen view is

enter image description here

Please give me some advise what should I do or sample codes to achieve my goal.

Thanks in advance.

Upvotes: 2

Views: 383

Answers (3)

jameshwart lopez
jameshwart lopez

Reputation: 3131

Thanks to Łukasz Motyczka i was able to make it by making my sprite

float centerX = CAMERA_WIDTH/2;
float centerY = CAMERA_HEIGHT/2;

Sprite sprite = new Sprite(centerX,centerY,this.region, this.getVertexBufferObjectManager());


Here's my full code

private final int CAMERA_WIDTH = 480;
private final int CAMERA_HEIGHT = 836;

private Camera mCamera;
private TextureRegion region;

private Scene mScene;

@Override
public EngineOptions onCreateEngineOptions() {
    this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);

    return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}

@Override
protected void onCreateResources() throws IOException {
    BitmapTextureAtlas atlas = new BitmapTextureAtlas(this.getTextureManager(),CAMERA_WIDTH,CAMERA_HEIGHT);
    this.region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas,this,"snow_bg.png",0,0);
    atlas.load();

}

@Override
protected Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    this.mScene = new Scene();
    float centerX = CAMERA_WIDTH/2;
    float centerY = CAMERA_HEIGHT/2;

    Sprite sprite = new Sprite(centerX,centerY,this.region, this.getVertexBufferObjectManager());
    this.mScene.setBackground(new SpriteBackground(sprite));

    return this.mScene;
}

Upvotes: 1

frogatto
frogatto

Reputation: 29285

Change this

Sprite sprite = new Sprite(0,0,region,this.getVertexBufferObjectManager());

to

Sprite sprite = new Sprite(0, 0, region,getWidth(), region.getHeight(), region, this.getVertexBufferObjectManager());

Upvotes: 0

Łukasz Motyczka
Łukasz Motyczka

Reputation: 1199

First of all: Cute background! Second: Your region dimensions should be in power of two (256x256, 1024x512, 2048x2048 and so on). Third: What are your image dimensions? Are they same as your camera width or height? if not you can do:

mScene.setBackground(new SpriteBackground(this.mCamera, new Sprite(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, this.region))); . 

Generally idea is to set dimensions of your background too, not only its position, because it is not stretched to the size of the screen automatically.

Upvotes: 2

Related Questions