I am facing 2 problems.
Questions are listed in picture above.
And main code snippets like this:
My device has a 1024x600 resolution.
Engine:
this.mCamera = new Camera(0, 0,*device'width*,*device'height*);
final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), this.mCamera);
engineOptions.getTouchOptions().setNeedsMultiTouch(true);
Background:
BitmapTextureAtlas bta = new BitmapTextureAtlas(mGame.getTextureManager(),WIDTH_SCENE_PNG,HEIGHT_SCENE_PNG, TextureOptions.BILINEAR);
ITextureRegion it = BitmapTextureAtlasTextureRegionFactory.createFromResource(bta, mGame, R.drawable.bg_main, 0, 0);
bta.load();
final Sprite sprite = new Sprite(0, 0,*device'width*,*device'height*,it, mGame.getVertexBufferObjectManager());
SpriteBackground bg = new SpriteBackground(sprite);
bg.setColor(Color.PINK);
setBackground(bg);
Sprites:
BitmapTextureAtlas btaTools = new BitmapTextureAtlas(mGame.getTextureManager(), 30, 40);
ITextureRegion itDelete = BitmapTextureAtlasTextureRegionFactory.createFromResource(btaTools, c, R.drawable.sprite_delete,0,0);
btaTools.load();
The distortions could come from the TextureOptions. For the TextureAtlas of your background sprite you use the TextureOptions.BILINEAR. The TextureAtlas of your tools however don't use any options. I guess they have transparent areas? When you use transparent sprites try TextureOptions.NEAREST_PREMULTIPLYALPHA or TextureOptions.BLINEAR_PREMULTIPLYALPHA. I for one use TextureOptions.NEAREST and it works fine for me (even with transparency). Try out some of the options maybe the distortions go away.
The size of your background: for the TextureAtlas bta you already use those constants WIDTH_SCENE_PNG and HEIGHT_SCENE_PNG, why don't you use them for the size of you sprite as well? Anyway, my guess is, that your camera has a different size. For a fullscreen image try:
final Sprite sprite = new Sprite(0, 0,mEngine.getCamera().getWidth(),mEngine.getCamera().getHeight(),it, mGame.getVertexBufferObjectManager());
Christoph
I fixed my problem.
The point is that I moved my resources(png images) from project's drawable-hdpi folder to drawable foler, sprites look nice then.
raw or asset folders are also OK.
Your answer is also helpful for me.Thank you! #Christoph
Related
I have three TextureRegions that are created like this:
mBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, pActivity, "menu_background.png", 0, 0);
mPlayBtnTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, pActivity, "play_btn.png", 50, 100);
mExitBtnTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mMenuTextureAtlas, pActivity, "exit_btn.png", 50, 200);
I also have corresponding sprites for each TextureRegion which are created like this:
mBackgroundSprite = new Sprite(0, 0, mBackgroundTextureRegion, mBufferObjectManager);
mPlayBtnSprite = new Sprite(0, 0, mPlayBtnTextureRegion, mBufferObjectManager);
mExitBtnSprite = new Sprite(0, 0, mExitBtnTextureRegion, mBufferObjectManager);
When I attach mBackgroundSprite to a scene like this:
mScene.attachChild(mBackgroundSprite);
I would expect only mBackgroundTextureRegion to appear on the scene (i.e. "menu_background.png") but in reality all three TextureRegions appear (i.e. mPlayBtnTextureRegion and mExitBtnTextureRegion appear too in their location as well, (50,100) and (50,200) respectively).
My questions are:
A. Why does this happen? (does it have something to do with using the same Texture for all TextureRegions?)
B. What is the correct way to make only one TextureRegion appear on a scene?
No this has nothing to do with the texture atlas. This is the correct way of doing it more or less. Nothing should be shown on the screen unless they are attached as a child to something (scene or engine). If you need more straightforward help please upload the part of your code that you are loading the sprite.
I noticed that I can keep one atlas for one textureRegion although I can draw same region multiple times as a sprite. Is it possible to keep all textureRegions in one textureAtlas in a scene?
My special case is, I am generating images instead of using any image file. I do this with BaseBitmapTextureAtlasSourceDecorator and generate the region from IBitmapTextureAtlasSource.
Yes. Generally, though, you should only create atlases with a a max width/height of 1024 (these sizes must be powers of 2, by the way), to be efficient.
On another note, I've found it easier to use BuildableBitmapTextureAtlas. With this kind of atlas, you don't have to specify where in the atlas you are placing your textures. I think it also might take care of sprite-bleeding to some degree (not sure, though). It's the same idea really... Here is an example from my project:
BuildableBitmapTextureAtlas buttonAtlas = new BuildableBitmapTextureAtlas(getTextureManager(), 512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
model.moveLeftButtonTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buttonAtlas, this, "moveleft_button.png");
model.moveRightButtonTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buttonAtlas, this, "moveright_button.png");
model.handleBlockButtonTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buttonAtlas, this, "handleblock_button.png");
model.restartButtonTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buttonAtlas, this, "restart_button.png");
try{ buttonAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 1)); }
catch(Exception e){ e.printStackTrace(); }
buttonAtlas.load();
In your case, use the following method:
BitmapTextureAtlasTextureRegionFactory.createFromSource(BuildableBitmapTextureAtlas atlas, IBitmapTextureAtlasSource source)
In summary, the atlas just holds all the textures you add to it. Then you load this atlas into memory so that these textures can be retrieved quickly. You can then use a single instance of a texture to build as many independent sprites as you wish.
I have to set a new sprite on background sprite. Background sprite has dimensions set according to dimension of device get through pixel format ..
I am using andengine and ratio Resolution as well but ..
Problem is little bit complex.. I tried to use pixel let say
Glaxy S3 ha *dimensions 720*1280 And required sprite location is (584,608)
so i set in manner ( CAMERA_WIDTH/1.233f,CAMERA_HEIGHT/2.112f)
BUT HTC experia has dimensions 320*480 So the required positions according to
( CAMERA_WIDTH/1.233f,CAMERA_HEIGHT/2.112f) is (2599.5,227.27)
but this wrong according to display... when i set it on (244,172) for experia its working perfect.... please help.
in the method :
public Engine onCreateEngine() {
this.camara = new Camera(0, 0, camara_width, camera_height);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE,
new FillResolutionPolicy(), this.camara).setNeedsSound(true).setNeedsMusic(true);
}
the parameter "new FillResolutionPolicy()" auto resize the width and height you only need make for a one resolution and the app run in any smartphone
I have to work with big textures in my project so I can't put all the textures into the one BitmapTextureAtlas. I tried to put them into two atlases:
textureAtlas = new BitmapTextureAtlas(2048, 2048, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
textureAtlas2 = new BitmapTextureAtlas(1024, 2048, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
foo = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas ,this,"foo.png",0,0);
bar = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas2,this,"bar.png",0,0);
But when I try to use bar
Sprite sBar = new Sprite(0,0,bar);
scene.attachChild(sBar);
the only thing I can see is the white rectangle instead of my image. And I have no idea what is wrong here.
OK, I got it. I just forgot about this:
getEngine().getTextureManager().loadTexture(textureAtlas2);
im new to android gaming and started andengine and facing problem while using createTiledFromAsset
the code where im getting problem is
#Override
public void onLoadResources() {
mBitmapTextureAtlas = new BitmapTextureAtlas(128, 128,
TextureOptions.BILINEAR);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createTiledFromAsset(this.mBitmapTextureAtlas, this,
"move.png", 0, 0, 10, 1);
mEngine.getTextureManager().loadTexture(mBitmapTextureAtlas);
}
#Override
public Scene onLoadScene() {
mEngine.registerUpdateHandler(new FPSLogger());
mMainScene = new Scene();
mMainScene
.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
player = new AnimatedSprite(0, 0, mPlayerTextureRegion);
mMainScene.attachChild(player);
return mMainScene;
}
im not getting the error as my BitmapTextureAtlas is of 128*128 and each tiled part coming from createTiledFromAsset should be of 78*85 as the passed arguments to it are 1 row and 10 columns and my source image is of 779*85 which means when the width is tiled to 10 parts then 779/10=78 approx which will be width of each tiled part and as row is 1 so 85/1=85 hence the width*height of each tiled part which is to be placed on the BitmapTextureAtlas is 78*85 and the BitmapTextureAtlas itself has size 128*128 then why the error saying Supplied pTextureAtlasSource must not exceed bounds of Texture
what is happening here ...? or im not understanding the actual functions ...? if im wrong then how the process of createTiledFromAsset is working........?
I found the same problem too. I am using a large sprite sheet, and try to use it. after searching, I found some clue in this tutorial: getting-started-working-with-images and i realize that the value of width and height used in:
BitmapTextureAtlas(WIDTH, HEIGHT ,TextureOptions.BILINEAR);
must be higher than the image size. for example if I use 1200*100 sprite sheet, I must use the width and height higher than 1200*100.
BitmapTextureAtlas(2047, 128, TextureOptions.BILINEAR);
If I understand BitmapTextureAtlas correctly, you are trying to put 779*85 image into the small space of 128*128. TextureAtlas is a large canvas on which you are supposed to place many images. These images are later accessed using object called TextureRegion, which basically specifies the size and coordinates of the smaller picture on the canvas. The method createTiledFromAsset probably copies the original image 1:1 onto the TextureAtlas and saves the coordinates of the tiles.
Please note that TextureRegion has nothing to do with the image itself, it is merely a "pointer" to the place on TextureAtlas where the image is stored.
To get the idea of what a TextureAtlas actually is, look at the awesome pictures at the bottom of this page:
http://www.blackpawn.com/texts/lightmaps/default.html