import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import android.opengl.GLES20;
public class Sprite {
final public float[] vertices = new float[20];
final public short[] indices = new short[6];
public FloatBuffer vertBuffer;
public FloatBuffer texBuffer;
public ShortBuffer indxBuffer;
Texture texture;
public float width;
public float height;
public float sMin, sMax, tMin, tMax;
public Sprite(SpriteSheetRegion region){
this.texture = region.texture;
this.width = region.width;
this.height = region.height;
this.sMax = region.sMax;
this.sMin = region.sMin;
this.tMax = region.tMax;
this.tMin = region.tMin;
vertices[0] = -width*0.5f;
vertices[1] = -height*0.5f;
vertices[2] = 0.0f;
vertices[3] = sMin;
vertices[4] = tMax;
vertices[5] = width*0.5f;
vertices[6] = -height*0.5f;
vertices[7] = 0.0f;
vertices[8] = sMax;
vertices[9] = tMax;
vertices[10] = -width*0.5f;
vertices[11] = height*0.5f;
vertices[12] = 0.0f;
vertices[13] = sMin;
vertices[14] = tMin;
vertices[15] = width*0.5f;
vertices[16] = height*0.5f;
vertices[17] = 0.0f;
vertices[18] = sMax;
vertices[19] = tMin;
System.out.println((width*0.5f)+" "+(height*0.5f));
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 2;
indices[4] = 1;
indices[5] = 3;
vertBuffer = ByteBuffer.allocateDirect(vertices.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertBuffer.put(vertices).position(0);
indxBuffer = ByteBuffer.allocateDirect(indices.length*2).order(ByteOrder.nativeOrder()).asShortBuffer();
indxBuffer.put(indices).position(0);
}
public Sprite(SpriteSheetRegion region, float width, float height){
this.texture = region.texture;
this.width = width;
this.height = height;
this.sMax = region.sMax;
this.sMin = region.sMin;
this.tMax = region.tMax;
this.tMin = region.tMin;
vertices[0] = -width*0.5f;
vertices[1] = -height*0.5f;
vertices[2] = 0.0f;
vertices[3] = sMin;
vertices[4] = tMax;
vertices[5] = width*0.5f;
vertices[6] = -height*0.5f;
vertices[7] = 0.0f;
vertices[8] = sMax;
vertices[9] = tMax;
vertices[10] = -width*0.5f;
vertices[11] = height*0.5f;
vertices[12] = 0.0f;
vertices[13] = sMin;
vertices[14] = tMin;
vertices[15] = width*0.5f;
vertices[16] = height*0.5f;
vertices[17] = 0.0f;
vertices[18] = sMax;
vertices[19] = tMin;
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 2;
indices[4] = 1;
indices[5] = 3;
vertBuffer = ByteBuffer.allocateDirect(vertices.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertBuffer.put(vertices).position(0);
indxBuffer = ByteBuffer.allocateDirect(indices.length*2).order(ByteOrder.nativeOrder()).asShortBuffer();
indxBuffer.put(indices).position(0);
}
public void draw(int program){
GLES20.glUseProgram(program);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture.id);
vertBuffer.position(0);
GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(program, "aPos"), 3, GLES20.GL_FLOAT, false, 20, vertBuffer);
GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aPos"));
vertBuffer.position(3);
GLES20.glVertexAttribPointer(GLES20.glGetAttribLocation(program, "aTexPos"), 2, GLES20.GL_FLOAT, false, 20, vertBuffer);
GLES20.glEnableVertexAttribArray(GLES20.glGetAttribLocation(program, "aTexPos"));
GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "texture"), 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, indxBuffer);
}
@Override
public String toString(){
return texture.toString();
}
}