package com.rabenauge.parandroid;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.rabenauge.demo.*;
import com.rabenauge.gl.*;
import javax.microedition.khronos.opengles.GL11;

public class IntroFade extends EffectManager {
    public static final long DURATION_EFFECT_DELAY=1000;
    public static final long DURATION_EFFECT_FADEOUT=500;

    private Texture2D title_droid, title_parandroid, title_trsinrab;

    // Fade-in the Droid title screen from black.
    private class FadeBlackToDroid extends Effect {
        public void onRender(GL11 gl, long t, long e, float s) {
            gl.glClear(GL11.GL_COLOR_BUFFER_BIT);

            // A simple linear fade-in looks unnatural.
            s*=s;

            gl.glColor4f(1, 1, 1, s);
            Helper.drawScreenSpaceTexture(title_droid);
        }
    }

    // Keep showing the given title screen.
    private class ShowTitle extends Effect {
        private Texture2D title;

        public ShowTitle(Texture2D title) {
            this.title=title;
        }

        public void onStart(GL11 gl) {
            gl.glColor4f(1, 1, 1, 1);
        }

        public void onRender(GL11 gl, long t, long e, float s) {
            Helper.drawScreenSpaceTexture(title);
        }
    }

    // Fade from a title screen to another title screen.
    private class FadeTitle extends Effect {
        Texture2D from, to;

        public FadeTitle(Texture2D from, Texture2D to) {
            this.from=from;
            this.to=to;
        }

        public void onRender(GL11 gl, long t, long e, float s) {
            gl.glColor4f(1, 1, 1, 1);
            Helper.drawScreenSpaceTexture(from);

            // A simple linear fade-in looks unnatural.
            s*=s;

            gl.glColor4f(1, 1, 1, s);
            Helper.drawScreenSpaceTexture(to);
        }
    }

    // Fade-out the Para 'N' droiD title screen to white.
    private class FadeParaNdroiDToWhite extends Effect {
        public void onStart(GL11 gl) {
            gl.glClearColor(1, 1, 1, 1);
        }

        public void onRender(GL11 gl, long t, long e, float s) {
            gl.glClear(GL11.GL_COLOR_BUFFER_BIT);

            // A simple linear fade-in looks unnatural.
            s=1-s*s;

            gl.glColor4f(1, 1, 1, s);
            Helper.drawScreenSpaceTexture(title_parandroid);
        }

        public void onStop(GL11 gl) {
            // Make sure the screen is finally full white
            // in case onRender() was not called with s=1.0.
            gl.glClear(GL11.GL_COLOR_BUFFER_BIT);

            gl.glClearColor(0, 0, 0, 0);
        }
    }

    public IntroFade(Demo demo, GL11 gl) {
        super(gl);

        // Load the title screens.
        Bitmap bitmap;

        title_droid=new Texture2D(gl);
        bitmap=BitmapFactory.decodeResource(demo.getActivity().getResources(), R.drawable.title_droid);
        title_droid.setData(bitmap);
        bitmap.recycle();

        title_parandroid=new Texture2D(gl);
        bitmap=BitmapFactory.decodeResource(demo.getActivity().getResources(), R.drawable.title_parandroid);
        title_parandroid.setData(bitmap);
        bitmap.recycle();

        title_trsinrab=new Texture2D(gl);
        bitmap=BitmapFactory.decodeResource(demo.getActivity().getResources(), R.drawable.title_trsinrab);
        title_trsinrab.setData(bitmap);
        bitmap.recycle();

        // Set universal OpenGL states for all effects in this part.
        title_droid.enable(true);

        gl.glEnable(GL11.GL_BLEND);
        gl.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

        // Schedule the effects in this part.
        add(new Clear(), DURATION_EFFECT_DELAY);

        long duration=(Demo.DURATION_PART_INTRO-DURATION_EFFECT_DELAY-DURATION_EFFECT_FADEOUT)/6;
        add(new FadeBlackToDroid(), duration);
        add(new ShowTitle(title_droid), duration);

        add(new FadeTitle(title_droid, title_trsinrab), duration);
        add(new ShowTitle(title_trsinrab), duration);

        add(new FadeTitle(title_trsinrab, title_parandroid), duration);
        add(new ShowTitle(title_parandroid), duration);

        add(new FadeParaNdroiDToWhite(), DURATION_EFFECT_FADEOUT);
    }
}

@@ -900,27 +900,22 @@
  given t
-itle screen
+exture
 .%0D%0A    p
@@ -932,19 +932,21 @@
 ss ShowT
-itl
+extur
 e extend
@@ -1013,19 +1013,21 @@
 ic ShowT
-itl
+extur
 e(Textur
@@ -1334,22 +1334,19 @@
 rom 
-a title screen
+one texture
  to 
@@ -1354,27 +1354,22 @@
 nother t
-itle screen
+exture
 .%0D%0A    p
@@ -1386,19 +1386,21 @@
 ss FadeT
-itl
+extur
 e extend
@@ -1419,16 +1419,24 @@
         
+private 
 Texture2
@@ -1470,19 +1470,21 @@
 ic FadeT
-itl
+extur
 e(Textur
@@ -4105,35 +4105,37 @@
    add(new ShowT
-itl
+extur
 e(title_droid), 
@@ -4161,35 +4161,37 @@
    add(new FadeT
-itl
+extur
 e(title_droid, t
@@ -4231,35 +4231,37 @@
    add(new ShowT
-itl
+extur
 e(title_trsinrab
@@ -4298,19 +4298,21 @@
 ew FadeT
-itl
+extur
 e(title_
@@ -4373,19 +4373,21 @@
 ew ShowT
-itl
+extur
 e(title_

Use more generic effect names