Subversion Repositories AndroidProjects

Rev

Rev 88 | Blame | Compare with Previous | 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.RenderImmediate;
import com.gebauz.framework.util.RenderTexture;
import com.gebauz.framework.util.RenderUtil;
import com.gebauz.framework.util.renderstates.RenderStates;
import com.gebauz.framework.util.renderstates.BlendingStates.BlendingMode;

public class MosaicEffect
{
        private GameLogic mGameLogic;
       
        private RenderTexture mOffscreen = null;
        private Mesh mMesh = null;
       
        private float mCurrentAlpha = 0.0f;
       
        public MosaicEffect(GameLogic gameLogic)
        {
                mGameLogic = gameLogic;
                mOffscreen = RenderTexture.create(GameConsts.POWERUP_MOSAIC_OFFSCREEN_WIDTH, GameConsts.POWERUP_MOSAIC_OFFSCREEN_HEIGHT);
               
                mMesh = RenderUtil.createQuad(0.0f, 0.0f, GameConsts.VIRTUAL_SCREEN_WIDTH, mGameLogic.getVirtualPlayFieldHeight());
               
                /*mMesh = new Mesh();
               
                Mesh.AttributeArray vertices = new Mesh.AttributeArray(Mesh.COLOR_ELEMENTS_COUNT * 6);
                vertices.fill(0.0f, 0.0f, 0.0f);
                vertices.fill(GameConsts.VIRTUAL_SCREEN_WIDTH, 0.0f, 0.0f);
                vertices.fill(0.0f, mGameLogic.getVirtualPlayFieldHeight(), 0.0f);
               
                vertices.fill(0.0f, mGameLogic.getVirtualPlayFieldHeight(), 0.0f);
                vertices.fill(GameConsts.VIRTUAL_SCREEN_WIDTH, 0.0f, 0.0f);
                vertices.fill(GameConsts.VIRTUAL_SCREEN_WIDTH, mGameLogic.getVirtualPlayFieldHeight(), 0.0f);

                Mesh.AttributeArray colors = new Mesh.AttributeArray(Mesh.COLOR_ELEMENTS_COUNT * 6);
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
                colors.fill(1.0f, 1.0f, 1.0f, 1.0f);
               
                Mesh.AttributeArray texCoords = new Mesh.AttributeArray(Mesh.TEX_COORD_ELEMENTS_COUNT * 6);
                texCoords.fill(0.0f, 1.0f);
                texCoords.fill(1.0f, 1.0f);
                texCoords.fill(0.0f, 0.0f);
               
                texCoords.fill(0.0f, 0.0f);
                texCoords.fill(1.0f, 1.0f);
                texCoords.fill(1.0f, 0.0f);
               
                mMesh.setVertices(vertices.getAttributeArray());
                mMesh.setColors(colors.getAttributeArray());
                mMesh.setTexCoords(texCoords.getAttributeArray());             
                mMesh.setPrimitiveType(GL10.GL_TRIANGLES);*/

               
                mCurrentAlpha = 0.0f;
        }
       
        public void update(float deltaTime)
        {
                if (mGameLogic.getPowerUpEffect().isMosaicActive())
                {
                        float mosaicTime = mGameLogic.getPowerUpEffect().getMoasicTime();
                        float alpha = 1.0f;
                        if (mosaicTime < GameConsts.POWERUP_MOSAIC_FADE_TIME)
                        {
                                alpha = MathUtil.clamp(mosaicTime / GameConsts.POWERUP_MOSAIC_FADE_TIME, 0.0f, 1.0f);
                        }                              
                        else if (mosaicTime > (GameConsts.POWERUP_MOSAIC_TIME - GameConsts.POWERUP_MOSAIC_FADE_TIME))
                        {
                                float v = mosaicTime - (GameConsts.POWERUP_MOSAIC_TIME - GameConsts.POWERUP_MOSAIC_FADE_TIME);
                                alpha = 1.0f - MathUtil.clamp(v / GameConsts.POWERUP_MOSAIC_FADE_TIME, 0.0f, 1.0f);
                        }
                       
                        // go to target
                        float diff = alpha - mCurrentAlpha;
                        mCurrentAlpha += (diff * deltaTime * GameConsts.POWERUP_MOSAIC_FADE_TIME);
                       
                        float colors[] = {
                                1.0f, 1.0f, 1.0f, mCurrentAlpha,
                                1.0f, 1.0f, 1.0f, mCurrentAlpha,
                                1.0f, 1.0f, 1.0f, mCurrentAlpha,
                                1.0f, 1.0f, 1.0f, mCurrentAlpha,
                                1.0f, 1.0f, 1.0f, mCurrentAlpha,
                                1.0f, 1.0f, 1.0f, mCurrentAlpha
                        };
                       
                        mMesh.setColors(colors);                       
                }
        }
       
        public void render()
        {
                // pass-through if effect not active
                if (mGameLogic.getPowerUpEffect().isMosaicActive())
                {
                        // fade out effect
                       
                        RenderStates rs = GLUtil.getRenderStates();
                       
                        rs.blending.setEnabled(true);
                        rs.blending.setBlendingMode(BlendingMode.ALPHABLEND);
                        rs.culling.setEnabled(false);
                        rs.getTextureStage(0).bindTexture(mOffscreen);
                       
                        rs.activate();                 
                        mMesh.render();
                        rs.deactivate();
                       
                        rs.getTextureStage(0).bindTexture(null);
                }
        }
       
        /** Called before the game is rendered. */
        public void beginRenderToTexture()
        {
                // pass-through if effect not active
                if (mGameLogic.getPowerUpEffect().isMosaicActive())
                {
                        mOffscreen.activate();
                       
                        GL10 gl = GLUtil.getGL();
                        gl.glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
                        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
                       
                        gl.glViewport(0, 0, GameConsts.POWERUP_MOSAIC_OFFSCREEN_WIDTH, GameConsts.POWERUP_MOSAIC_OFFSCREEN_HEIGHT);
                       
                        gl.glMatrixMode(GL10.GL_PROJECTION);
                        gl.glLoadIdentity();
                        gl.glOrthof(0, GameConsts.VIRTUAL_SCREEN_WIDTH-1.0f, mGameLogic.getVirtualPlayFieldHeight()-1.0f, 0, 0, 1);

                }              
        }
       
        /** Called after the game is rendered. */
        public void endRenderToTexture()
        {
                // pass-through if effect not active
                if (mGameLogic.getPowerUpEffect().isMosaicActive())
                {
                        mOffscreen.deactivate();
                        GL10 gl = GLUtil.getGL();
                        gl.glViewport(0, 0, GLUtil.getInstance().getWidth(), GLUtil.getInstance().getHeight());
                        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);
                }              
        }
}