Android Open Source - Flumpgdx Flump Display






From Project

Back to project page Flumpgdx.

License

The source code is released under:

Copyright (c) 2014 Daniyal Khan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Softw...

If you think the Android project Flumpgdx listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.flumpgdx.display;
/*from  ww  w. j  a v a  2  s.c o  m*/
import static com.badlogic.gdx.graphics.g2d.Batch.C1;
import static com.badlogic.gdx.graphics.g2d.Batch.C2;
import static com.badlogic.gdx.graphics.g2d.Batch.C3;
import static com.badlogic.gdx.graphics.g2d.Batch.C4;
import static com.badlogic.gdx.graphics.g2d.Batch.U1;
import static com.badlogic.gdx.graphics.g2d.Batch.U2;
import static com.badlogic.gdx.graphics.g2d.Batch.U3;
import static com.badlogic.gdx.graphics.g2d.Batch.U4;
import static com.badlogic.gdx.graphics.g2d.Batch.V1;
import static com.badlogic.gdx.graphics.g2d.Batch.V2;
import static com.badlogic.gdx.graphics.g2d.Batch.V3;
import static com.badlogic.gdx.graphics.g2d.Batch.V4;
import static com.badlogic.gdx.graphics.g2d.Batch.X1;
import static com.badlogic.gdx.graphics.g2d.Batch.X2;
import static com.badlogic.gdx.graphics.g2d.Batch.X3;
import static com.badlogic.gdx.graphics.g2d.Batch.X4;
import static com.badlogic.gdx.graphics.g2d.Batch.Y1;
import static com.badlogic.gdx.graphics.g2d.Batch.Y2;
import static com.badlogic.gdx.graphics.g2d.Batch.Y3;
import static com.badlogic.gdx.graphics.g2d.Batch.Y4;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Matrix3;
import com.badlogic.gdx.math.Vector2;
import com.flumpgdx.library.FlumpKeyFrame;
import com.flumpgdx.library.FlumpLayer;

public class FlumpDisplay {

  private static final float[] DISPLAY_VERTICES = new float[20];
  private static final float DEFAULT_VERTEX_COLOR = Color.WHITE.toFloatBits(); 
  
  protected static float xPivot, yPivot, xSkew, ySkew, xScale, yScale, xLoc, yLoc; 
  
  protected Vector2[] pos = new Vector2[4];
  
  private int keyframe; //current keyframe
  protected int numFrames;
  
  boolean overElapsed = false;
  
  protected final FlumpLayer reference;
    
  public FlumpDisplay(FlumpLayer layer) {
    this.reference = layer;
    this.numFrames = maxFrameCount();
    for (int i = 0; i < pos.length; i++) {
      pos[i] = new Vector2();
    }
  }
  
  protected FlumpDisplayTexture getDisplayTexture() {
    return TextureCache.obtain().get(reference.keyframes[keyframe].ref);
  }
  
  public int maxFrameCount() {
    int numFrames = 0;
    for (FlumpKeyFrame keyframe: reference.keyframes) {
      numFrames += keyframe.duration;
    }
    return numFrames;
  }
  
  protected void applyTransformation(Matrix3 transformation) {
    for (int i = 0; i < pos.length; i++) {
      pos[i].mul(transformation);
    }
  }
  
  protected void applyTranslation(Vector2 translation) {
    for (int i = 0; i < pos.length; i++) {
      pos[i].add(translation);
    }
  }
  
  protected static void getAttributes(FlumpDisplay source, float interpolation) {
    FlumpKeyFrame[] keyframes = source.reference.keyframes;
    int frame = source.keyframe;
    int nextFrame = (frame + 1) % keyframes.length;
     FlumpKeyFrame current = keyframes[frame];
    if (interpolation == 0) {
      //in the case where no interpolation occurs, save some computations
      xScale = current.scale[0];
      yScale = current.scale[1];
      xSkew = current.skew[0];
      ySkew = current.skew[1];
      xLoc = current.loc[0];
      yLoc = -current.loc[1];
      xPivot = -current.pivot[0];
      yPivot = -current.pivot[1];
    } else {
      FlumpKeyFrame next = keyframes[nextFrame];
      xScale = current.scale[0] + (next.scale[0] - current.scale[0]) * interpolation;
      yScale = current.scale[1] + (next.scale[1] - current.scale[1]) * interpolation;
      xSkew = current.skew[0] + (next.skew[0] - current.skew[0]) * interpolation;
      ySkew = current.skew[1] + (next.skew[1] - current.skew[1]) * interpolation;
      xLoc = current.loc[0] + (next.loc[0] - current.loc[0]) * interpolation;
      yLoc = -(current.loc[1] + (next.loc[1] - current.loc[1]) * interpolation);
      xPivot = -(current.pivot[0] + (next.pivot[0] - current.pivot[0]) * interpolation);
      yPivot = -(current.pivot[1] + (next.pivot[1] - current.pivot[1]) * interpolation);

    }
  }
  
  void update(int frame) {
    if (reference == null) throw new IllegalStateException("FlumpDisplayLayer is empty on update!");
    keyframe = 0;
    overElapsed = frame > maxFrameCount();
    if (overElapsed) return;
    while(frame >= reference.keyframes[keyframe].duration && keyframe < reference.keyframes.length - 1) {
      frame -= reference.keyframes[keyframe].duration;
      keyframe++;
    }
    float interpolation = ((float) frame) / reference.keyframes[keyframe].duration;
    //last frame in the animation, no interpolation
    if (keyframe == reference.keyframes.length - 1) {
      interpolation = 0;
    }
    float ease = reference.keyframes[keyframe].ease;
    if (ease != 0) {
      float t;
      if (ease < 0) {
        // Ease in
        float inv = 1 - interpolation;
        t = 1 - inv * inv;
        ease = -ease;
      } else {
        // Ease out
        t = interpolation * interpolation;
      }
      interpolation = ease * t + (1 - ease) * interpolation;
    }
        FlumpDisplayTexture display = getDisplayTexture();
    float width = display.getRegionWidth();
    float height = display.getRegionHeight();
    getAttributes(this, interpolation);
    yPivot += height;
    
    float sx = xPivot;
    float swx = (xPivot + width);
    float sy = -yPivot;
    float shy = (-yPivot + height);
    
    if (xScale != 1 || yScale != 1) { 
      sx *= xScale;
      swx *= xScale;
      sy *= yScale;
      shy *= yScale;
    }
    
    pos[0].set(sx, sy);
    pos[1].set(sx, shy);
    pos[2].set(swx, shy);
    pos[3].set(swx, sy);
    
    if (xSkew != 0 || ySkew != 0) {
      float cosx =  MathUtils.cos(xSkew);
      float sinx =  MathUtils.sin(xSkew);
      float cosy;
      float siny;
      
      if (xSkew != ySkew) {
        cosy =  MathUtils.cos(ySkew);
        siny = -MathUtils.sin(ySkew);
      } else {
        siny = -sinx;
        cosy = cosx;
      }
      
      for (int i = 0; i < pos.length; i++) {
        pos[i].set(cosy * pos[i].x + sinx * pos[i].y, siny * pos[i].x + cosx * pos[i].y);
      }
    }
    
    pos[0].add(xLoc, yLoc);
    pos[1].add(xLoc, yLoc);
    pos[2].add(xLoc, yLoc);
    pos[3].add(xLoc, yLoc);
    
    /****************************************************
     * Matrix Multiplication Implementation
    scale.setToScaling(xScale, yScale);
    rotation.val[Matrix3.M00] = MathUtils.cos(ySkew);
    rotation.val[Matrix3.M01] = MathUtils.sin(xSkew);
    rotation.val[Matrix3.M10] = -MathUtils.sin(ySkew);
    rotation.val[Matrix3.M11] = MathUtils.cos(xSkew);
    translation.setToTranslation(xLoc, yLoc);
    Matrix3 tmp = new Matrix3(scale.mul(rotation).mul(translation));
    for (int i = 0; i < pos.length; i++) {
      pos[i].mul(scale).mul(rotation).mul(translation);
    }
    *****************************************************/
    
  }
  
  void draw(SpriteBatch batch) {
    if (overElapsed) return;
    FlumpDisplayTexture t = getDisplayTexture();
    float u = t.getU();
    float u2 = t.getU2();
    float v = t.getV();
    float v2 = t.getV2();
    
    DISPLAY_VERTICES[U1] = u;
    DISPLAY_VERTICES[V1] = v2;
    DISPLAY_VERTICES[U2] = u;
    DISPLAY_VERTICES[V2] = v;
    DISPLAY_VERTICES[U3] = u2;
    DISPLAY_VERTICES[V3] = v;
    DISPLAY_VERTICES[U4] = u2;
    DISPLAY_VERTICES[V4] = v2;
    DISPLAY_VERTICES[C1] = DEFAULT_VERTEX_COLOR;
    DISPLAY_VERTICES[C2] = DEFAULT_VERTEX_COLOR;
    DISPLAY_VERTICES[C3] = DEFAULT_VERTEX_COLOR;
    DISPLAY_VERTICES[C4] = DEFAULT_VERTEX_COLOR;
    DISPLAY_VERTICES[X1] = pos[0].x;
    DISPLAY_VERTICES[X2] = pos[1].x;
    DISPLAY_VERTICES[X3] = pos[2].x;
    DISPLAY_VERTICES[X4] = pos[3].x;
    DISPLAY_VERTICES[Y1] = pos[0].y;
    DISPLAY_VERTICES[Y2] = pos[1].y;
    DISPLAY_VERTICES[Y3] = pos[2].y;
    DISPLAY_VERTICES[Y4] = pos[3].y;
    
    batch.draw(t.getTexture(), DISPLAY_VERTICES, 0, DISPLAY_VERTICES.length);
    
  }

}




Java Source Code List

com.flumpgdx.FlumpGDX.java
com.flumpgdx.IOSLauncher.java
com.flumpgdx.android.AndroidLauncher.java
com.flumpgdx.client.GwtLauncher.java
com.flumpgdx.desktop.DesktopLauncher.java
com.flumpgdx.display.FlumpAnimation.java
com.flumpgdx.display.FlumpDisplayBundle.java
com.flumpgdx.display.FlumpDisplayTexture.java
com.flumpgdx.display.FlumpDisplay.java
com.flumpgdx.display.TextureCache.java
com.flumpgdx.library.FlumpAtlas.java
com.flumpgdx.library.FlumpKeyFrame.java
com.flumpgdx.library.FlumpLayer.java
com.flumpgdx.library.FlumpLibraryFile.java
com.flumpgdx.library.FlumpMovie.java
com.flumpgdx.library.FlumpTextureGroup.java
com.flumpgdx.library.FlumpTextureRegion.java
com.flumpgdx.library.MovieMaker.java