package com.miviclin.droidengine2d.graphics.sprites;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.content.Context;

import com.miviclin.droidengine2d.graphics.cameras.Camera;
import com.miviclin.droidengine2d.graphics.shaders.PositionTextureOpacityBatchShaderProgram;

/**
 * PositionTextureSpriteBatch que tiene en cuenta la opacidad de los sprites
 * 
 * @see PositionTextureSpriteBatch
 * @author Miguel Vicente Linares
 * 
 */
public class PositionTextureOpacitySpriteBatch extends PositionTextureSpriteBatch {
	
	private FloatBuffer opacityBuffer;
	private float[] opacityData;
	private float lastOpacity;
	
	public PositionTextureOpacitySpriteBatch(Context context) {
		super(context, new PositionTextureOpacityBatchShaderProgram());
		
		opacityData = new float[BATCH_CAPACITY * 4];
		
		opacityBuffer = ByteBuffer.allocateDirect(opacityData.length * FLOAT_SIZE_BYTES)
				.order(ByteOrder.nativeOrder())
				.asFloatBuffer();
		opacityBuffer.put(opacityData).flip();
		
		lastOpacity = 1.0f;
	}
	
	protected PositionTextureOpacitySpriteBatch(Context context, PositionTextureOpacityBatchShaderProgram shaderProgram) {
		super(context, shaderProgram);
	}
	
	@Override
	protected void setupVertexShaderVariables() {
		super.setupVertexShaderVariables();
		PositionTextureOpacityBatchShaderProgram shaderProgram = (PositionTextureOpacityBatchShaderProgram) getShaderProgram();
		shaderProgram.specifyVerticesOpacity(opacityBuffer, 0, FLOAT_SIZE_BYTES);
	}
	
	@Override
	protected void drawSprite(Sprite sprite, Camera camera) {
		boolean textureChanged = checkTextureChanged(sprite);
		if ((getBatchSize() > 0) && ((getBatchSize() == BATCH_CAPACITY) || textureChanged || sprite.getColor().getA() != lastOpacity)) {
			drawBatch();
		}
		setupTexture(sprite, textureChanged);
		setSpriteVerticesData(sprite);
		updateSpriteMVPMatrix(sprite, camera);
		setupOpacity(sprite);
	}
	
	@Override
	protected void drawBatch() {
		opacityBuffer.clear();
		opacityBuffer.put(opacityData).limit(getBatchSize() * 4).position(0);
		super.drawBatch();
	}
	
	/**
	 * Define la opacidad del siguiente sprite en el batch
	 * 
	 * @param sprite Aprite que se esta agregando al batch
	 */
	protected void setupOpacity(Sprite sprite) {
		int spriteOffset = getBatchSize() * 6;
		int limit = spriteOffset + 6;
		for (int i = spriteOffset; i < limit; i++) {
			opacityData[i] = sprite.getColor().getA();
		}
		lastOpacity = sprite.getColor().getA();
	}
	
}

@@ -636,38 +636,10 @@
 ta;%0A
-%09private float lastOpacity;%0A
 %09%0A
+
 %09pub
@@ -1000,33 +1000,8 @@
 ();%0A
-%09%09%0A%09%09lastOpacity = 1.0f;%0A
 %09%7D%0A%09
@@ -1534,309 +1534,24 @@
 %7B%0A%09%09
-boolean textureChanged = checkTextureChanged(sprite);%0A%09%09if ((getBatchSize() %3E 0) && ((getBatchSize() == BATCH_CAPACITY) %7C%7C textureChanged %7C%7C sprite.getColor().getA() != lastOpacity)) %7B%0A%09%09%09drawBatch();%0A%09%09%7D%0A%09%09setupTexture(sprite, textureChanged);%0A%09%09setSpriteVerticesData(sprite);%0A%09%09updateSpriteMVPMatrix
+super.drawSprite
 (spr
@@ -1968,17 +1968,17 @@
 ize() * 
-6
+4
 ;%0A%09%09int 
@@ -2004,9 +2004,9 @@
 t + 
-6
+4
 ;%0A%09%09
@@ -2100,54 +2100,12 @@
 ();%0A
+
 %09%09%7D%0A
-%09%09lastOpacity = sprite.getColor().getA();%0A
 %09%7D%0A%09

Modify PositionTextureOpacitySpriteBatch to use Color