- package com.langhart.rpg.graphics.sprites;
- import java.io.File;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.ShortBuffer;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import javax.microedition.khronos.opengles.GL10;
- import android.graphics.Rect;
- import android.opengl.GLU;
- import com.langhart.rpg.graphics.GraphicsManager;
- import com.langhart.rpg.maps.IMapDynamic;
- public class SpriteAnimation extends SpriteDrawable implements IMapDynamic {
- protected transient List<LoadedTexture> frames = new ArrayList<LoadedTexture>();
- protected List<Long> intervals = new ArrayList<Long>();
- protected List<String> framePaths;
- protected int numOfFrames = 0;
- protected int currentFrameIndex = 0;
- protected LoadedTexture currentFrame;
- protected long currentInterval = 0;
- protected long elapsed = 0;
- protected FloatBuffer texCoords;
- protected FloatBuffer vertexBuffer;
- protected boolean running = false;
- public SpriteAnimation(List<String> actualFiles) {
- framePaths = actualFiles;
- drawableType = DRAWABLETYPE.ANIMATION;
- }
- public void setFrame(int num){
- currentFrameIndex = num;
- currentFrame = frames.get(currentFrameIndex);
- currentInterval = intervals.get(currentFrameIndex);
- }
- public void decrementFrame(){
- currentFrameIndex--;
- if(currentFrameIndex < 0)
- currentFrameIndex = numOfFrames-1;
- setFrame(currentFrameIndex);
- }
- public void incrementFrame(){
- currentFrameIndex++;
- if(currentFrameIndex == numOfFrames)
- currentFrameIndex = 0;
- setFrame(currentFrameIndex);
- }
- @Override
- public void draw(GL10 gl) {
- gl.glFrontFace(GL10.GL_CCW);
- //gl.glColor4f(0f, 0f, 1f, 0.5f);
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
- gl.glBindTexture(GL10.GL_TEXTURE_2D, currentFrame.getIndexID());
- gl.glVertexPointer(3, GL10.GL_FLOAT, 0, this.vertexBuffer);
- gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, this.texCoords);
- gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
- gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
- }
- @Override
- public boolean isEnabled() {
- return true;
- }
- @Override
- public void load() {
- Collections.sort(framePaths);
- for(int i = 0; i < framePaths.size(); i++){
- frames.add(GraphicsManager.getInstance().loadTexture(framePaths.get(i)));
- intervals.add(200L);
- if(frames.size() == 1){
- currentFrame = frames.get(0);
- currentInterval = intervals.get(0);
- vertexBuffer = ByteBuffer.allocateDirect(4*4*3).order(ByteOrder.nativeOrder()).asFloatBuffer();
- texCoords = ByteBuffer.allocateDirect(4*4*2).order(ByteOrder.nativeOrder()).asFloatBuffer();
- Rect b = frames.get(0).getBounds();
- vertexBuffer.put(new float[]{0f, 0f, 0f, //Bottom Left
- (float)b.right, 0f, 0f, //Bottom Right
- 0f, (float)b.top, 0f, //Top Left
- (float)b.right, (float)b.top, 0f}); //Top Right
- //Tex coords in reverse order because bitmap is upside down
- texCoords.put(new float[]{0f, 1.0f,
- 1.0f, 1.0f,
- 0f, 0f,
- 1.0f, 0f});
- vertexBuffer.position(0);
- texCoords.position(0);
- }
- }
- numOfFrames = frames.size();
- }
- @Override
- public void release() {
- // TODO Auto-generated method stub
- }
- @Override
- public void tick(long amnt) {
- elapsed += amnt;
- if(elapsed >= currentInterval){
- incrementFrame();
- elapsed = 0;
- }
- }
- }