use of org.andengine.opengl.texture.PixelFormat in project AndEngine by nicolasgramlich.
the class TexturePackParser method parseTexture.
private ITexture parseTexture(final Attributes pAttributes) throws TexturePackParseException {
final String file = SAXUtils.getAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_FILE);
if (this.mTextureManager.hasMappedTexture(file)) {
return this.mTextureManager.getMappedTexture(file);
}
final String type = SAXUtils.getAttributeOrThrow(pAttributes, TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE);
final PixelFormat pixelFormat = TexturePackParser.parsePixelFormat(pAttributes);
final TextureOptions textureOptions = this.parseTextureOptions(pAttributes);
final ITexture texture;
if (type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_BITMAP)) {
try {
texture = new BitmapTexture(this.mTextureManager, new IInputStreamOpener() {
@Override
public InputStream open() throws IOException {
return TexturePackParser.this.onGetInputStream(file);
}
}, BitmapTextureFormat.fromPixelFormat(pixelFormat), textureOptions);
} catch (final IOException e) {
throw new TexturePackParseException(e);
}
} else if (type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVR)) {
try {
texture = new PVRTexture(this.mTextureManager, PVRTextureFormat.fromPixelFormat(pixelFormat), new SmartPVRTexturePixelBufferStrategy(DataConstants.BYTES_PER_MEGABYTE / 8), textureOptions) {
@Override
protected InputStream onGetInputStream() throws IOException {
return TexturePackParser.this.onGetInputStream(file);
}
};
} catch (final IOException e) {
throw new TexturePackParseException(e);
}
} else if (type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVRGZ)) {
try {
texture = new PVRGZTexture(this.mTextureManager, PVRTextureFormat.fromPixelFormat(pixelFormat), new SmartPVRTexturePixelBufferStrategy(DataConstants.BYTES_PER_MEGABYTE / 8), textureOptions) {
@Override
protected InputStream onGetInputStream() throws IOException {
return TexturePackParser.this.onGetInputStream(file);
}
};
} catch (final IOException e) {
throw new TexturePackParseException(e);
}
} else if (type.equals(TexturePackParser.TAG_TEXTURE_ATTRIBUTE_TYPE_VALUE_PVRCCZ)) {
try {
texture = new PVRCCZTexture(this.mTextureManager, PVRTextureFormat.fromPixelFormat(pixelFormat), new SmartPVRTexturePixelBufferStrategy(DataConstants.BYTES_PER_MEGABYTE / 8), textureOptions) {
@Override
protected InputStream onGetInputStream() throws IOException {
return TexturePackParser.this.onGetInputStream(file);
}
};
} catch (final IOException e) {
throw new TexturePackParseException(e);
}
} else {
throw new TexturePackParseException(new IllegalArgumentException("Unsupported pTextureFormat: '" + type + "'."));
}
this.mTextureManager.addMappedTexture(file, texture);
return texture;
}
use of org.andengine.opengl.texture.PixelFormat in project AndEngine by nicolasgramlich.
the class BitmapTextureAtlas method writeTextureToHardware.
// ===========================================================
// Methods
// ===========================================================
@Override
protected void writeTextureToHardware(final GLState pGLState) {
final PixelFormat pixelFormat = this.mBitmapTextureFormat.getPixelFormat();
final int glInternalFormat = pixelFormat.getGLInternalFormat();
final int glFormat = pixelFormat.getGLFormat();
final int glType = pixelFormat.getGLType();
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, glInternalFormat, this.mWidth, this.mHeight, 0, glFormat, glType, null);
final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha;
/* Non alpha premultiplied bitmaps are loaded with ARGB_8888 and converted down manually. */
final Config bitmapConfig = (preMultipyAlpha) ? this.mBitmapTextureFormat.getBitmapConfig() : Config.ARGB_8888;
final ArrayList<IBitmapTextureAtlasSource> textureSources = this.mTextureAtlasSources;
final int textureSourceCount = textureSources.size();
final ITextureAtlasStateListener<IBitmapTextureAtlasSource> textureStateListener = this.getTextureAtlasStateListener();
for (int i = 0; i < textureSourceCount; i++) {
final IBitmapTextureAtlasSource bitmapTextureAtlasSource = textureSources.get(i);
try {
final Bitmap bitmap = bitmapTextureAtlasSource.onLoadBitmap(bitmapConfig);
if (bitmap == null) {
throw new NullBitmapException("Caused by: " + bitmapTextureAtlasSource.getClass().toString() + " --> " + bitmapTextureAtlasSource.toString() + " returned a null Bitmap.");
}
final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && pixelFormat == PixelFormat.RGBA_8888;
if (!useDefaultAlignment) {
/* Adjust unpack alignment. */
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
}
if (preMultipyAlpha) {
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, glFormat, glType);
} else {
pGLState.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, this.mPixelFormat);
}
if (!useDefaultAlignment) {
/* Restore default unpack alignment. */
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
}
bitmap.recycle();
if (textureStateListener != null) {
textureStateListener.onTextureAtlasSourceLoaded(this, bitmapTextureAtlasSource);
}
} catch (final NullBitmapException e) {
if (textureStateListener != null) {
textureStateListener.onTextureAtlasSourceLoadExeption(this, bitmapTextureAtlasSource, e);
} else {
throw e;
}
}
}
}
use of org.andengine.opengl.texture.PixelFormat in project AndEngine by nicolasgramlich.
the class Font method update.
public synchronized void update(final GLState pGLState) {
if (this.mTexture.isLoadedToHardware()) {
final ArrayList<Letter> lettersPendingToBeDrawnToTexture = this.mLettersPendingToBeDrawnToTexture;
if (lettersPendingToBeDrawnToTexture.size() > 0) {
this.mTexture.bind(pGLState);
final PixelFormat pixelFormat = this.mTexture.getPixelFormat();
final boolean preMultipyAlpha = this.mTexture.getTextureOptions().mPreMultiplyAlpha;
for (int i = lettersPendingToBeDrawnToTexture.size() - 1; i >= 0; i--) {
final Letter letter = lettersPendingToBeDrawnToTexture.get(i);
if (!letter.isWhitespace()) {
final Bitmap bitmap = this.getLetterBitmap(letter);
final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && (pixelFormat == PixelFormat.RGBA_8888);
if (!useDefaultAlignment) {
/* Adjust unpack alignment. */
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
}
if (preMultipyAlpha) {
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, letter.mTextureX, letter.mTextureY, bitmap);
} else {
pGLState.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, letter.mTextureX, letter.mTextureY, bitmap, pixelFormat);
}
if (!useDefaultAlignment) {
/* Restore default unpack alignment. */
GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
}
bitmap.recycle();
}
}
lettersPendingToBeDrawnToTexture.clear();
System.gc();
}
}
}
Aggregations