Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 3.61 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.langhart.rpg.graphics.sprites;
  2.  
  3. import java.io.File;
  4. import java.nio.ByteBuffer;
  5. import java.nio.ByteOrder;
  6. import java.nio.FloatBuffer;
  7. import java.nio.ShortBuffer;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.List;
  11.  
  12. import javax.microedition.khronos.opengles.GL10;
  13.  
  14. import android.graphics.Rect;
  15. import android.opengl.GLU;
  16.  
  17. import com.langhart.rpg.graphics.GraphicsManager;
  18. import com.langhart.rpg.maps.IMapDynamic;
  19.  
  20. public class SpriteAnimation extends SpriteDrawable implements IMapDynamic {
  21.         protected transient List<LoadedTexture> frames = new ArrayList<LoadedTexture>();
  22.         protected List<Long> intervals = new ArrayList<Long>();
  23.         protected List<String> framePaths;
  24.         protected int numOfFrames = 0;
  25.         protected int currentFrameIndex = 0;
  26.         protected LoadedTexture currentFrame;
  27.         protected long currentInterval = 0;
  28.         protected long elapsed = 0;
  29.        
  30.         protected FloatBuffer texCoords;
  31.         protected FloatBuffer vertexBuffer;
  32.        
  33.         protected boolean running = false;
  34.        
  35.        
  36.         public SpriteAnimation(List<String> actualFiles) {
  37.                 framePaths = actualFiles;
  38.                 drawableType = DRAWABLETYPE.ANIMATION;
  39.         }
  40.        
  41.         public  void setFrame(int num){
  42.                 currentFrameIndex = num;
  43.                
  44.                 currentFrame = frames.get(currentFrameIndex);
  45.                 currentInterval = intervals.get(currentFrameIndex);
  46.         }
  47.          
  48.         public  void decrementFrame(){
  49.                 currentFrameIndex--;
  50.                 if(currentFrameIndex < 0)
  51.                         currentFrameIndex = numOfFrames-1;
  52.                 setFrame(currentFrameIndex);
  53.         }
  54.        
  55.         public  void incrementFrame(){
  56.                 currentFrameIndex++;
  57.                 if(currentFrameIndex == numOfFrames)
  58.                         currentFrameIndex = 0;
  59.                 setFrame(currentFrameIndex);
  60.                
  61.         }
  62.        
  63.         @Override
  64.         public void draw(GL10 gl) {
  65.                 gl.glFrontFace(GL10.GL_CCW);
  66.                 //gl.glColor4f(0f, 0f, 1f, 0.5f);
  67.                 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  68.                 gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  69.                
  70.                 gl.glBindTexture(GL10.GL_TEXTURE_2D, currentFrame.getIndexID());
  71.                
  72.                 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, this.vertexBuffer);
  73.                 gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, this.texCoords);
  74.                 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  75.                 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  76.                 gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  77.         }
  78.  
  79.         @Override
  80.         public boolean isEnabled() {
  81.                 return true;
  82.         }
  83.  
  84.         @Override
  85.         public void load() {
  86.                 Collections.sort(framePaths);
  87.                 for(int i = 0; i < framePaths.size(); i++){
  88.                         frames.add(GraphicsManager.getInstance().loadTexture(framePaths.get(i)));
  89.                         intervals.add(200L);
  90.                         if(frames.size() == 1){
  91.                                 currentFrame = frames.get(0);
  92.                                 currentInterval = intervals.get(0);
  93.                                 vertexBuffer = ByteBuffer.allocateDirect(4*4*3).order(ByteOrder.nativeOrder()).asFloatBuffer();
  94.                                 texCoords = ByteBuffer.allocateDirect(4*4*2).order(ByteOrder.nativeOrder()).asFloatBuffer();
  95.                                 Rect b = frames.get(0).getBounds();
  96.                                 vertexBuffer.put(new float[]{0f, 0f, 0f,           //Bottom Left
  97.                                                                      (float)b.right, 0f, 0f,      //Bottom Right
  98.                                                                      0f, (float)b.top, 0f,        //Top Left
  99.                                                                      (float)b.right, (float)b.top, 0f}); //Top Right
  100.                                
  101.                                 //Tex coords in reverse order because bitmap is upside down
  102.                                 texCoords.put(new float[]{0f, 1.0f,
  103.                                                                                 1.0f, 1.0f,
  104.                                                                                 0f, 0f,
  105.                                                                                 1.0f, 0f});
  106.                                 vertexBuffer.position(0);
  107.                                 texCoords.position(0);
  108.                         }
  109.                 }
  110.                 numOfFrames = frames.size();
  111.         }
  112.  
  113.         @Override
  114.         public void release() {
  115.                 // TODO Auto-generated method stub
  116.                
  117.         }
  118.  
  119.         @Override
  120.         public void tick(long amnt) {
  121.                 elapsed += amnt;
  122.                 if(elapsed >= currentInterval){
  123.                         incrementFrame();
  124.                         elapsed = 0;
  125.                 }
  126.         }
  127.        
  128. }