Draw part of texture in AndEngine - android

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);

Related

Flickery Textures in andEngine

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

How to get bitmap of whole screen with Libgdx?

I would like to know how can I get bitmap of whole current screen. I would like to check RGBA values of selected pixel. My code that isn't working properly:
public class PixmapTest implements Screen
{
//(...)
private float getRedValue(int x, int y)
{
Pixmap pixmap = new Pixmap(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Pixmap.Format.RGBA8888);
Color color = new Color();
Color.rgba8888ToColor(color, pixmap.getPixel(x,y));
pixmap.dispose();
return color.r;
}
#Override
public void render()
{
Gdx.gl.glClearColor(1f, 0f, 0f, 1f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
System.out.println(getRedValue(50, 50));
}
}
It seems to me that the getRedValue method, should return 1.0, but it returns 0.0. I'm apologize for unproffesional question. I'm new to Libgdx and game development as well.
The easiest way is to use the ScreenUtils package, e.g.
Image screenShot = new Image(ScreenUtils.getFrameBufferTexture());
What you have done is you have created a blank pixmap that doesn't contain anything. Kumar Saurabh is right that you need to take the screen shot of that image and the function to get the pixel Value.
you can do it as
final Pixmap pixmap = new Pixmap(w, h, Format.RGBA8888);
ByteBuffer pixels = pixmap.getPixels();
Gdx.gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels);
Color color = new Color();
Color.rgba8888ToColor(color, pixmap.getPixel(x,y));
pixmap.dispose();
return color.r;
Hope this might work for you
https://code.google.com/p/libgdx-users/wiki/Screenshots
This is how stuff works
Do not forget to get PNG.java class also as main logic is in it only
Also if u are using LibGdx 0.9.8 these classes are implicitly included in the gdx.jar

Converting Android Bitmap to LibGdx's Texture

i am trying to convert bitmap into a libGDX Texture by converting:
Android Bitmap to byte[]
byte[] to libGDX Pixmap
libGDX Pixmap to libGDX Texture
The problem I am facing is that the bitmap which is converted to texture is drawing the sprite sheet from texture packer that is in assets folder
public void onByteArrayOfCroppedImageReciever(byte[] bytes) {
try {
pmap=new Pixmap(bytes, 0, bytes.length);
tex=new Texture(pmap);
face=new Sprite(tex);
// game.setScreen(new GameScreen(game, batcher, face));
} catch(Exception e) {
Gdx.app.log("KS", e.toString());
e.printStackTrace();
}
}
If the goal is to convert an Android Bitmap to a libgdx Texture, you don't need to use Pixmap at all. You can do it directly with the help of simple OpenGL and Android GLUtils. Try the followings; it is 100x faster than your solution. I assume that you are not in the rendering thread (you should not most likely). If you are, you don't need to call postRunnable().
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
Texture tex = new Texture(bitmap.getWidth(), bitmap.getHeight(), Format.RGBA8888);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex.getTextureObjectHandle());
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
bitmap.recycle();
// now you have the texture to do whatever you want
}
});
hmm another possibility is that you've got a threading issue. I've noticed this kind of problem when loading my own unmanaged textures on the UI thread while libgdx is doing its thing loading textures concurrently on the render thread.
If this is the problem the simple solution is to synchronize the creation of the texture with the render thread using Gdx.app.postRunnable. i.e.:
public void onByteArrayOfCroppedImageReciever(byte[] bytes) {
try {
pmap=new Pixmap(bytes, 0, bytes.length);
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
tex=new Texture(pmap);
face=new Sprite(tex);
}
});
} catch(Exception e) {
Gdx.app.log("KS", e.toString());
e.printStackTrace();
}
}
you have to code on a new thread because pixmap class takes time for byte conversion and sometimes returns a temporary pixmap in case the process hasnt finished so its better to run on a seperate thread and your problem will be solved.
Are you committed to that operational pipeline? Here's an alternate way to do the conversion you asked for:
Implement com.badlogic.gdx.graphics.TextureData to take an android.graphics.Bitmap in the constructor.
In prepare(), allocate and fill an IntBuffer using Bitmap.getPixels.
Use bitmath to swap the data in the buffer from ARGB to RGBA: (arr[i] << 8) | (arr[i]>>24)
In getType() return TextureData.TextureDataType.Compressed
In consumeCompressedData() call glTexImage2D to feed the data into the (prebound) texture.
Your pipeline above includes several copies and recompressions of pixel data; this pipeline allows you to feed your Bitmap data straight into the texture with only one copy (necessary for the byte format conversion anyway). Would that work for you?

Android OpenGL: Possibly Running Out Of Memory?

I have been trying to fix this issue for weeks but I'm at the point where I dont know what to do now.
I think that some Android devices dont have enough memory to load the amount of textures, although it could be something else causing the issue, as I said I really dont know what to do with this.
There are 28 PNG's being loaded all 1024x1024 which come to a total of 4.8megs. Below is the OpenGL method for loading textures
GL10 gl = glGraphics.getGL();
int[] textureIds = new int[1];
gl.glGenTextures(1, textureIds, 0);
textureId = textureIds[0];
InputStream in = null;
try {
in = fileIO.readAsset(fileName);
Bitmap bitmap = BitmapFactory.decodeStream(in);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
setFilters(GL10.GL_LINEAR , GL10.GL_LINEAR);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
width = bitmap.getWidth();
height = bitmap.getHeight();
bitmap.recycle();
} catch(IOException e) {
throw new RuntimeException("Couldn't load texture '" + fileName +"'", e);
} finally {
if(in != null)
try { in.close(); } catch (IOException e) { }
}
There are no issues on my Desire HD, but on a HTC Cha Cha a lot of textures dont appear at all and on a Galaxy S two textures just appear white. The Cha Cha throws this error while loading textures
02-04 15:46:28.907: E/Adreno200-ES20(1501): override1= 0xfffffffe,
override2= 0xfff *
Oddly if the Cha Cha is locked (OpenGL textures are destroyed) and then unlocked (reloaded textures) the particular textures that were not there initally are now, however different textures are now not visable.
Is this a memory issue? If so is there a way around this?
Thanks
The correct solution is texture compression, not PNG compression. PVR-TC would get you most of what you need. At 4-bpp, you'd go down to 12MB instead of 117MB. Even just using lower bitdepth images, like RGB-565 formats, (16-bits per pixel) would cut your needs in half.
Also, you don't have to use 1024x1024 textures for phones; that's kind of overkill. You could probably get away with 512x512 images. Coupled with PVR-TC, you'd only need about 3MB for all of that texture data.

Android OpenGL 1.0 different texture for each face of the cube with texture atlas

I have a cube, I can assign for it one texture, but I would like to assign different texture for each face. I have a 512x512 texture atlas, with four tiles, each 256x256. I use the NeHe ports, so for one texture the load is:
public void loadGLTexture(GL10 gl, Context context) {
InputStream is = context.getResources().openRawResource(R.drawable.test);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(is);
} finally {
try {
is.close();
is = null;
} catch (IOException e) {
}
}
I have tried to find solutions, but I was not able to find with texture atlas, only with loading as much texture as I want to use, then assign them to the faces, but because of the performance it's not really good for me. I hope someone can help me!
Thanks in advance!
PS: Which is faster, creating a cube with the coordinates writing in the code, or loading a cube model from an .obj file?

Categories

Resources