1 package com.cooliris.media;
3 import javax.microedition.khronos.opengles.GL11;
5 import android.util.Log;
7 public class CrossFadingTexture {
8 private static final String TAG = "CrossFadingTexture";
10 private Texture mTexture;
11 private Texture mFadingTexture;
12 private float mMixRatio = 0.0f;
13 private float mAnimatedMixRatio = 0.0f;
14 private boolean mBindUsingMixed = false;
15 private boolean mBind = false;
16 private boolean mFadeNecessary = false;
18 public CrossFadingTexture() {
21 public CrossFadingTexture(Texture initialTexture) {
23 mAnimatedMixRatio = 1.0f;
24 mFadeNecessary = false;
25 mTexture = initialTexture;
26 mFadingTexture = initialTexture;
31 mFadingTexture = null;
34 public CrossFadingTexture(Texture source, Texture destination) {
35 mFadingTexture = source;
36 mTexture = destination;
38 mAnimatedMixRatio = 0.0f;
39 Log.i(TAG, "Creating crossfading texture");
42 public Texture getTexture() {
46 public void setTexture(Texture texture) {
47 if (mTexture == texture || texture == null || mAnimatedMixRatio < 1.0f) {
50 mFadeNecessary = false;
51 if (mFadingTexture == null) {
52 mFadeNecessary = true;
54 if (mTexture != null) {
55 mFadingTexture = mTexture;
57 mFadingTexture = texture;
60 mAnimatedMixRatio = 0.0f;
64 public void setTextureImmediate(Texture texture) {
65 if (texture == null || texture.isLoaded() == false || mTexture == texture) {
68 if (mTexture != null) {
69 mFadingTexture = mTexture;
75 public boolean update(float timeElapsed) {
76 if (mTexture != null && mFadingTexture != null && mTexture.isLoaded() && mFadingTexture.isLoaded()) {
77 mAnimatedMixRatio = FloatUtils.animate(mAnimatedMixRatio, mMixRatio, timeElapsed * 0.5f);
78 return (mMixRatio != mAnimatedMixRatio);
80 mAnimatedMixRatio = 0.0f;
85 public boolean bind(RenderView view, GL11 gl) {
87 return true; // Already bound.
89 if (mFadingTexture != null && mFadingTexture.mState == Texture.STATE_ERROR) {
90 mFadingTexture = null;
92 if (mTexture != null && mTexture.mState == Texture.STATE_ERROR) {
95 mBindUsingMixed = false;
96 boolean fadingTextureLoaded = false;
97 boolean textureLoaded = false;
98 if (mFadingTexture != null) {
99 fadingTextureLoaded = view.bind(mFadingTexture);
101 if (mTexture != null) {
103 textureLoaded = mTexture.isLoaded();
105 if (mFadeNecessary) {
106 if (view.getAlpha() > mAnimatedMixRatio) {
107 view.setAlpha(mAnimatedMixRatio);
109 if (mAnimatedMixRatio == 1.0f) {
110 mFadeNecessary = false;
113 if (textureLoaded == false && fadingTextureLoaded == false) {
117 if (mAnimatedMixRatio <= 0.0f && fadingTextureLoaded) {
118 view.bind(mFadingTexture);
119 } else if (mAnimatedMixRatio >= 1.0f || !fadingTextureLoaded || view.getAlpha() < mAnimatedMixRatio
120 || mFadingTexture == mTexture) {
123 mBindUsingMixed = true;
124 view.bindMixed(mFadingTexture, mTexture, mAnimatedMixRatio);
129 public void unbind(RenderView view, GL11 gl) {
130 if (mBindUsingMixed && mBind) {
132 mBindUsingMixed = false;