Subversion Repositories AndroidProjects

Rev

Rev 108 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.pingK.common.game;

import javax.microedition.khronos.opengles.GL10;

import com.gebauz.pingK.common.framework.GLUtil;
import com.gebauz.pingK.common.framework.MathUtil;
import com.gebauz.pingK.common.framework.Mesh;
import com.gebauz.pingK.common.framework.RenderUtil;
import com.gebauz.pingK.common.framework.Vector4;
import com.gebauz.pingK.common.framework.renderstates.RenderStates;
import com.gebauz.pingK.common.framework.renderstates.BlendingStates.BlendingMode;
import com.gebauz.pingK.common.game.GameConsts;

public class ScreenFlash
{
        private GameLogic mGameLogic = null;
       
        private Mesh mMesh = null;
        private float mFlashTime = -1.0f;
        private float mTotalFlashTime = 1.0f;
       
        public ScreenFlash(GameLogic gameLogic)
        {
                mGameLogic = gameLogic;
               
                mMesh = RenderUtil.createQuad(0.0f, 0.0f, GameConsts.VIRTUAL_SCREEN_WIDTH, GameConsts.VIRTUAL_SCREEN_HEIGHT, GameConsts.PINGK_COLOR);
               
                /*mMesh = new Mesh();
               
                mMesh.setPrimitiveType(GL10.GL_TRIANGLE_STRIP);
               
                Mesh.AttributeArray vertices = new Mesh.AttributeArray(Mesh.COLOR_ELEMENTS_COUNT * 4);
                Mesh.AttributeArray colors = new Mesh.AttributeArray(Mesh.COLOR_ELEMENTS_COUNT * 4);
                vertices.fill(0.0f, 0.0f, 0.0f);
                vertices.fill(GameConsts.VIRTUAL_SCREEN_WIDTH, 0.0f, 0.0f);
                vertices.fill(0.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT, 0.0f);
                vertices.fill(GameConsts.VIRTUAL_SCREEN_WIDTH, GameConsts.VIRTUAL_SCREEN_HEIGHT, 0.0f);
               
                colors.fill(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, GameConsts.PINGK_COLOR.w);
                colors.fill(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, GameConsts.PINGK_COLOR.w);
                colors.fill(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, GameConsts.PINGK_COLOR.w);
                colors.fill(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, GameConsts.PINGK_COLOR.w);
               
                mMesh.setVertices(vertices.getAttributeArray());
                mMesh.setColors(colors.getAttributeArray());*/

        }
       
        public void update(float deltaTime)
        {
                mFlashTime -= deltaTime;
                if (mFlashTime < 0.0f)
                        return;
               
                float alpha = MathUtil.clamp(mFlashTime/mTotalFlashTime, 0.0f, 1.0f);
               
                /*Mesh.AttributeArray colors = new Mesh.AttributeArray(Mesh.COLOR_ELEMENTS_COUNT * 4);
               
                colors.fill(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha);
                colors.fill(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha);
                colors.fill(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha);
                colors.fill(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha);
               
                mMesh.setColors(colors.getAttributeArray());*/

               
                Mesh.AttributeArray colors = new Mesh.AttributeArray(Mesh.COLOR_ELEMENTS_COUNT * mMesh.getColorCount());
                for (int i = 0; i < mMesh.getColorCount(); i++)
                {
                        colors.fill(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha);
                        //colors.fill(1.0f, 1.0f, 1.0f, alpha);
                }
                mMesh.setColors(colors.getAttributeArray());
        }
       
        public void render()
        {
                if (mFlashTime < 0.0f)
                        return;
               
                GL10 gl = GLUtil.getGL();
                gl.glMatrixMode(GL10.GL_PROJECTION);
                gl.glLoadIdentity();
                gl.glOrthof(0, GameConsts.VIRTUAL_SCREEN_WIDTH-1.0f, GameConsts.VIRTUAL_SCREEN_HEIGHT-1.0f, 0, 0, 1);
                gl.glMatrixMode(GL10.GL_MODELVIEW);
                gl.glLoadIdentity();
                       
                RenderStates rs = GLUtil.getRenderStates();
                rs.blending.setEnabled(true);
                rs.blending.setBlendingMode(BlendingMode.ALPHABLEND);
                rs.depthTest.setEnabled(false);
                rs.activate();
                mMesh.render();
                rs.deactivate();
        }
       
        public void startFlash(float time)
        {
                mFlashTime = time;
                mTotalFlashTime = time;
        }

}