package com.rabenauge.parandroid;

import android.opengl.GLU;
import android.util.FloatMath;
import com.rabenauge.demo.*;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL11;

public class IntroBlink extends EffectManager {
    private static final int WIDTH=800, HEIGHT=480;
    private static final short[] JOINTS={
        270, 93,  231,130,  213,130,
        251,298,  269,298,
        185,357,
        402, 52,  402, 72,  420, 72,
        439,128,  439,147,
        335,339,  335,357,  335,375,  355,375,  373,375,
        480,288,  461,322,  461,340,
        530, 45,  548, 64,  548, 83,  548,100,
        555,397,  574,397,  574,378
    };

    private int[] colors;

    private class BlinkJoints extends Effect {
        public void onStart(GL11 gl) {
            gl.glPointSize(2.5f);
            gl.glColorPointer(4, GL11.GL_FIXED, 0, IntBuffer.wrap(colors));

            // Only enable point smoothing if we can draw sizes greater than 1,
            // else the points will be too small.
            int[] params=new int[2];
            gl.glGetIntegerv(GL11.GL_SMOOTH_POINT_SIZE_RANGE, params, 0);
            if (params[1]>1) {
                gl.glEnable(GL11.GL_POINT_SMOOTH);
            }
        }

        public void onRender(GL11 gl, long t, long e, float s) {
            // Fade the colors.
            for (int i=3; i<colors.length; i+=4) {
                colors[i]=(int)((1.0f-FloatMath.cos(DemoMath.PI*s*i))/2.0f*65536);
            }

            // Set OpenGL states that differ from the concurrently running fading part.
            gl.glDisable(GL11.GL_TEXTURE_2D);
            gl.glEnableClientState(GL11.GL_COLOR_ARRAY);

            // Set the projection to match the joint coordinates.
            gl.glMatrixMode(GL11.GL_PROJECTION);
            gl.glPushMatrix();
            gl.glLoadIdentity();
            GLU.gluOrtho2D(gl, 0, WIDTH, HEIGHT, 0);

            gl.glVertexPointer(2, GL11.GL_SHORT, 0, ShortBuffer.wrap(JOINTS));
            gl.glDrawArrays(GL11.GL_POINTS, 0, JOINTS.length/2);

            // Restore OpenGL states for the fading part.
            gl.glPopMatrix();

            gl.glDisableClientState(GL11.GL_COLOR_ARRAY);
            gl.glEnable(GL11.GL_TEXTURE_2D);

            // Yes, using color arrays seems to modify the color state!
            gl.glColor4f(1, 1, 1, 1);
        }

        public void onStop(GL11 gl) {
            gl.glDisable(GL11.GL_POINT_SMOOTH);
        }
    }

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

        colors=new int[JOINTS.length/2*4];
        for (int i=0; i<colors.length; i+=4) {
            colors[i  ]=(int)(1.0f*65536);
            colors[i+1]=0;
            colors[i+2]=0;
            // Set alpha when rendering.
        }

        // Schedule the effects in this part.
        add(new BlinkJoints(), Demo.DURATION_PART_INTRO-IntroFade.DURATION_EFFECT_FADEOUT);
    }
}

@@ -388,10 +388,10 @@
    2
-70
+69
 , 93
@@ -399,17 +399,17 @@
   231,13
-0
+1
 ,  213,1
@@ -409,17 +409,17 @@
   213,13
-0
+1
 ,%0D%0A     
@@ -427,17 +427,17 @@
   251,29
-8
+9
 ,  269,2
@@ -437,17 +437,17 @@
   269,29
-8
+9
 ,%0D%0A     
@@ -473,19 +473,19 @@
   40
-2, 52
+3, 53
 ,  40
-2
+3
 , 72
@@ -489,17 +489,17 @@
  72,  42
-0
+1
 , 72,%0D%0A 
@@ -507,17 +507,17 @@
       43
-9
+8
 ,128,  4
@@ -517,17 +517,17 @@
 128,  43
-9
+8
 ,147,%0D%0A 
@@ -563,19 +563,19 @@
 5,37
-5
+6
 ,  35
-5,375
+4,376
 ,  3
@@ -579,17 +579,17 @@
   373,37
-5
+6
 ,%0D%0A     
@@ -592,18 +592,18 @@
        4
-80
+79
 ,288,  4
@@ -607,17 +607,17 @@
   461,32
-2
+3
 ,  461,3
@@ -617,17 +617,17 @@
   461,34
-0
+1
 ,%0D%0A     
@@ -635,29 +635,29 @@
   53
-0, 45
+1, 46
 ,  54
-8, 64
+9, 65
 ,  54
-8
+9
 , 83
@@ -661,17 +661,17 @@
  83,  54
-8
+9
 ,100,%0D%0A 
@@ -679,17 +679,17 @@
       55
-5
+6
 ,397,  5
@@ -689,17 +689,17 @@
 397,  57
-4
+3
 ,397,  5
@@ -699,17 +699,17 @@
 397,  57
-4
+3
 ,378%0D%0A  

Optimize the joint coordinates again, for the Milestone