Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.pingK.game;

import javax.microedition.khronos.opengles.GL10;

import com.gebauz.framework.util.GLUtil;
import com.gebauz.framework.util.MathUtil;
import com.gebauz.framework.util.Mesh;
import com.gebauz.framework.util.renderstates.RenderStates;
import com.gebauz.framework.util.renderstates.BlendingStates.BlendingMode;

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 = 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());
        }
       
        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.activate();
                mMesh.render();
                rs.deactivate();
        }
       
        public void startFlash(float time)
        {
                mFlashTime = time;
                mTotalFlashTime = time;
        }

}