I'm having problems with my textures in andEngine
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws Exception {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
mTextureAtlas = new BuildableBitmapTextureAtlas(getTextureManager(),
480, 800);
fruitTextureRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(mTextureAtlas, this, "fruitsprites.png");
fruitBG = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
mTextureAtlas, this, "gamebg.png");
try {
mTextureAtlas
.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
0, 1, 1));
} catch (TextureAtlasBuilderException e) {
e.printStackTrace();
}
mTextureAtlas.load();
pOnCreateResourcesCallback.onCreateResourcesFinished();
// TODO Auto-generated method stub
}
This causes my whole texture atlas to appear on the screen; on my S4, the texture flickers continuously and the textures are upside down - it looks like the image was chopped up partially in a triangle.
On the emulator, the contents of the entire texture atlas is shown, the screen doesn't flicker, but the textures are upsidedown.
Also fruitBGSprite.setVisible(true); just makes the texture completely invisible (but the screen does not flicker)My goal right now is to set just 1 texture as my background.
create mTextureAtlas variable of ITexture class and fruitTextureRegion,fruitBG variables of ITextureRegion
Related
So i've been using ligbdx for some time, and it was really simple there to do such thing. So what i want to achieve is that when i have a large texture i would like to get a part of that texture by giving x,y (where to start cutting from) and width,height (size of cut part) and later use that part as a sprite or anything that is possible to be drawn on the andengine scene.
In libgdx it ws like that:
//loads file from assets into texture
Texture texture = new Texture(Gdx.files.internal("data/texture5.png"));
//cuts a part of it into drawable element
TextureRegion part = new TexureRegion(texture, x, y, width, height);
and part was just that section of the texture i needed to draw later on the screen. Is it really so hard to do in andengine that nowwhere on the internet i couldnt find any answer for 2h of searching? :)
I have just searched for hours and got nothing. But accidentally i tried this, and it worked. It's an old question but whatever. Maybe this can help.
ITexture texture = null;
try {
texture = new BitmapTexture(engine.getTextureManager(),
new IInputStreamOpener() {
public InputStream open() throws IOException {
return engine.getAssets().open(path);
}
});
texture.load();
} catch (IOException e) {
e.printStackTrace();
}
ITextureRegion texturePart = new TextureRegion(texture, x, y, width, height);
Does anyone know why this is happening?
Code to load image:
gameOverAtlas = new BuildableBitmapTextureAtlas(
activity.getTextureManager(), 1024, 1024,
TextureOptions.DEFAULT);
deathScreen1 = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(gameOverAtlas, activity,
"deathscreen1.png");
engine.getTextureManager().loadTexture(
this.gameOverAtlas);
engine.getTextureManager().loadTexture(
this.mAutoParallaxBackgroundTexture);
engine.getTextureManager().loadTexture(
this.gameOverAtlas);
try {
this.gameTextureAtlas
.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
0, 1, 0));
this.gameTextureAtlas.load();
this.gameOverAtlas
.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
0, 1, 0));
this.gameOverAtlas.load();
this.playerAtlas
.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(
0, 1, 0));
this.playerAtlas.load();
} catch (final TextureAtlasBuilderException e) {
Debug.e(e);
}
}
Then attaching the sprite:
gameOverScreen = new Sprite(0, 0, 650, 400, ResourceManager.getInstance().deathScreen1,vbom);
attachChild(gameOverScreen);
I get this error:
Looks like you are calling loadTexture before building the texture atlas.
move:
engine.getTextureManager().loadTexture(
this.gameOverAtlas);
engine.getTextureManager().loadTexture(
this.mAutoParallaxBackgroundTexture);
engine.getTextureManager().loadTexture(
this.gameOverAtlas);
after the try/Catch block.
Also if your texture is the same size as the dimensions of the TextureAltas it will fail unless you set the padding and spacing parameters to 0.
As an aside, if you are making a texture atlas that contains only a single texture region, there is no need to use a buildable texture atlas.
The special feature of the BuildableTexture atlas is that it will fit many regions for you into the atlas, meaning you do not need to use a program like TexturePacker and teh texturePacker extension for andengine.
Since its buildable texture, not normal one, you are loading it with bit different way
try
{
gameTexture.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 1));
gameTexture.load();
}
catch (TextureAtlasBuilderException e)
{
Debug.e(e);
}
Keep in mind those 3 last parameters, you can control padding etc.
I have got a background picture and want to draw text on it, but it isn't working.
There is only the picture on the screen.
private SpriteBatch spriteBatch;
private Texture splsh;
private BitmapFont font;
public void render(float arg0)
{
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
spriteBatch.draw(splsh, 0, 0);
font.draw(spriteBatch, "test", 10, 480);
spriteBatch.end();
}
public void show()
{
spriteBatch = new SpriteBatch();
splsh = new Texture(Gdx.files.internal("bg2.png"));
font = new BitmapFont();
font.setColor(Color.RED);
}
you should scale your font
font.setScale(0.2f);
change size according to your need...
and one more thing try drawing your font in center of screen, that way you can check that whether it is drawing or not
I'm creating a game with libgdx that I want to run at a higher resolution on the desktop, but I want it to scale everything down correctly when I run it on android at smaller resolutions. I've read that the best way to do this is to not use a pixel perfect camera, and instead to use world coordinates, but I'm not sure how to correctly do that.
This is the code I have right now:
#Override
public void create() {
characterTexture = new Texture(Gdx.files.internal("character.png"));
characterTextureRegion = new TextureRegion(characterTexture, 0, 0, 100, 150);
batch = new SpriteBatch();
Gdx.gl10.glClearColor(0.4f, 0.6f, 0.9f, 1);
float aspectRatio = (float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight();
camera= new OrthographicCamera(aspectRatio, 1.0f);
}
#Override
public void render() {
GL10 gl = Gdx.graphics.getGL10();
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
camera.apply(gl);
batch.setProjectionMatrix(camera.combined);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
draw();
}
private void draw() {
//batch.getProjectionMatrix().set(camera.combined);
batch.begin();
batch.draw(characterTextureRegion, 0, 0, // the bottom left corner of the box, unrotated
1f, 1f, // the rotation center relative to the bottom left corner of the box
0.390625f, 0.5859375f, // the width and height of the box
1, 1, // the scale on the x- and y-axis
0); // the rotation angle
batch.end();
}
The texture I'm use is 256x256 with the actual image in it being 100x150.
This is the result I get when I run the game: http://i.imgur.com/HV9Bi.png
The sprite that gets rendered is massive, considering this is the original image: http://i.imgur.com/q1cZT.png
What's the best way to go about making it so that the sprites get rendered at their original size while still keeping the ability to have the game scale correctly when played in different resolutions?
I've only found two solutions, both of which I don't like.
The image showed up how it was supposed to if I used pixel coordinates for the camera, but then that didn't scale at all when I put it on my phone with a different resolution.
I can scale the texture region down when I draw it, but it seems like there is a better way because it is extremely tedious trying to figure out the correct number to scale it by.
Have you ever used the Libgdx setup tool? When you create a project with it, it has a sample image that is displayed. It seems to keep it's ratio correct no matter what size you change the screen to.
public class RotationTest implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
Stage stage;
public boolean leonAiming = true;
#Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2); }....
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
First of all you need to fix boundaries to the world (I mean to your game ). In that world only you actors(game characters) should play. If you are crossing boundaries, manage it with camera like showing up, down, left and right.
I'm new to OpenGL ES and developing a simple 2D game. However, I'm confused as to how I can go about loading multiple animation frames as textures (for the player character). I've tried to load a different image every time the character is rendered, but that's too slow.
Here is my texture loading code thus far:
public void loadGLTexture(GL10 gl, Context context) {
InputStream[] is=new InputStream[3];
is[0]= context.getResources().openRawResource(R.drawable.r1);
is[1]= context.getResources().openRawResource(R.drawable.r2);
is[2]= context.getResources().openRawResource(R.drawable.r3);
try {
bitmap[0]= BitmapFactory.decodeStream(is[0]);
bitmap[1]= BitmapFactory.decodeStream(is[1]);
bitmap[2]= BitmapFactory.decodeStream(is[2]);
} finally {
try {
is[0].close();
is[1].close();
is[2].close();
is = null;
} catch (IOException e) {
}
}
gl.glGenTextures(3, textures,0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[0], 0);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[1], 0);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap[2], 0);
bitmap[0].recycle();
bitmap[1].recycle();
bitmap[2].recycle();
}
How can I make all three images accessible through an array?
You need to call glBindTexture before every texImage2D. Currently you are loading all three images into textures[0].
Don't try to load all textures at once. Change your function to load only one texture and just call it three times. You should be able to do:
textures[0]=loadGLTexture(GL10,context,R.drawable.r1);
textures[1]=loadGLTexture(GL10,context,R.drawable.r2);
textures[2]=loadGLTexture(GL10,context,R.drawable.r3);
You can place all the frames of animation on a single texture and use texture coordinates to select which one to use