Android Open Source - fun-gl Engine Renderer






From Project

Back to project page fun-gl.

License

The source code is released under:

Apache License

If you think the Android project fun-gl listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.jcxavier.android.opengl.engine;
/*from  w w  w.  j  a v  a 2 s . c o  m*/
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import com.jcxavier.android.opengl.engine.cache.GLState;
import com.jcxavier.android.opengl.engine.shader.ShaderManager;
import com.jcxavier.android.opengl.engine.texture.TextureManager;
import com.jcxavier.android.opengl.game.GameStage;
import com.jcxavier.android.opengl.math.Vector3;

import static android.opengl.GLES20.*;

/**
 * Created on 31/01/2014.
 *
 * @author Joo Xavier <jcxavier@jcxavier.com>
 */
class EngineRenderer implements GLSurfaceView.Renderer, RendererOptions {

    private EngineActivity mActivity;

    private final Vector3 mBgColor;
    private boolean mBlendingEnabled;
    private boolean mBackfaceCullingEnabled;

    Bitmap.Config mBitmapConfig;
    int mDepthBufferSize;
    int mStencilBufferSize;
    int mSamples;

    private long mLastUpdateTimeStamp;
    private GameStage mGame;

    EngineRenderer(final EngineActivity activity) {
        mActivity = activity;

        // default options
        mBgColor = new Vector3(0, 0, 0); // black
        mBitmapConfig = Bitmap.Config.ARGB_8888;
        mDepthBufferSize = 16;
        mStencilBufferSize = 8;
        mSamples = 0;
        mBlendingEnabled = true;
        mBackfaceCullingEnabled = false;
    }

    public void clean() {
        mGame.onUnload();
        mActivity = null;
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // reset state
        GLState.clean();
        TextureManager.getInstance().clean();
        ShaderManager.getInstance().clean();

        // setup initial GL options
        mActivity.onGlContextLoad();
        applyRendererOptions();

        // load game
        resetTimestamp();
        mGame.onLoad();
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        glViewport(0, 0, width, height);
        mGame.onLayout(new Point(width, height));
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // calculate deltaTime
        long now = System.nanoTime();
        double dt = (now - mLastUpdateTimeStamp) / 1.0e9;
        mLastUpdateTimeStamp = now;

        glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

        mGame.onUpdate(dt);
        mGame.onDraw();
    }

    @Override
    public void setBackgroundColor(final Vector3 backgroundColor) {
        mBgColor.set(backgroundColor);
    }

    @Override
    public void setStencilBufferSize(final int size) {
        mStencilBufferSize = size;
    }

    @Override
    public void setDepthBufferSize(final int size) {
        mDepthBufferSize = size;
    }

    @Override
    public void setSamples(final int numberOfSamples) {
        mSamples = numberOfSamples;
    }

    @Override
    public void setBitmapConfig(final Bitmap.Config bitmapConfig) {
        mBitmapConfig = bitmapConfig;
    }

    @Override
    public void setBlendingEnabled(final boolean enabled) {
        mBlendingEnabled = enabled;
    }

    @Override
    public void setBackfaceCullingEnabled(final boolean enabled) {
        mBackfaceCullingEnabled = enabled;
    }

    private void applyRendererOptions() {
        glClearColor(mBgColor.x, mBgColor.y, mBgColor.z, 1.0f);

        if (mBlendingEnabled) {
            glEnable(GL_BLEND);
        } else {
            glDisable(GL_BLEND);
        }

        if (mBackfaceCullingEnabled) {
            glCullFace(GL_BACK);
            glEnable(GL_CULL_FACE);
        } else {
            glDisable(GL_CULL_FACE);
        }

        if (mDepthBufferSize > 0) {
            glDepthFunc(GL_LEQUAL);
            glEnable(GL_DEPTH_TEST);
        } else {
            glDisable(GL_DEPTH_TEST);
        }
    }

    private void resetTimestamp() {
        mLastUpdateTimeStamp = System.nanoTime();
    }

    public void setGame(final GameStage game) {
        mGame = game;
    }

    public boolean onTouchEvent(final MotionEvent event) {
        return mGame.onTouchEvent(event);
    }
}




Java Source Code List

com.jcxavier.android.opengl.engine.BitmapConfigHelper.java
com.jcxavier.android.opengl.engine.EngineActivity.java
com.jcxavier.android.opengl.engine.EngineRenderer.java
com.jcxavier.android.opengl.engine.EngineView.java
com.jcxavier.android.opengl.engine.RendererOptions.java
com.jcxavier.android.opengl.engine.cache.GLState.java
com.jcxavier.android.opengl.engine.gdx.GdxEglConfigChooser.java
com.jcxavier.android.opengl.engine.shader.ColorShader.java
com.jcxavier.android.opengl.engine.shader.ShaderManager.java
com.jcxavier.android.opengl.engine.shader.Shader.java
com.jcxavier.android.opengl.engine.shader.TextureShader.java
com.jcxavier.android.opengl.engine.texture.TextureFilteringMode.java
com.jcxavier.android.opengl.engine.texture.TextureManager.java
com.jcxavier.android.opengl.engine.texture.TextureWrap.java
com.jcxavier.android.opengl.engine.texture.Texture.java
com.jcxavier.android.opengl.engine.type.RotationMode.java
com.jcxavier.android.opengl.file.FileManager.java
com.jcxavier.android.opengl.game.GameStage.java
com.jcxavier.android.opengl.game.SimpleGameStage.java
com.jcxavier.android.opengl.game.camera.Camera.java
com.jcxavier.android.opengl.game.camera.DefaultCamera.java
com.jcxavier.android.opengl.game.camera.OrthographicCamera.java
com.jcxavier.android.opengl.game.manager.GameManager.java
com.jcxavier.android.opengl.game.manager.ScreenManager.java
com.jcxavier.android.opengl.game.manager.input.InputHandler.java
com.jcxavier.android.opengl.game.manager.input.InputManager.java
com.jcxavier.android.opengl.game.object.DrawableObject.java
com.jcxavier.android.opengl.game.object.GameObject.java
com.jcxavier.android.opengl.game.object.Sprite.java
com.jcxavier.android.opengl.game.type.Resizeable.java
com.jcxavier.android.opengl.game.type.Touchable.java
com.jcxavier.android.opengl.game.type.Transformable.java
com.jcxavier.android.opengl.game.type.Updateable.java
com.jcxavier.android.opengl.math.IVector.java
com.jcxavier.android.opengl.math.Matrix4.java
com.jcxavier.android.opengl.math.Vector2.java
com.jcxavier.android.opengl.math.Vector3.java
com.jcxavier.android.opengl.math.Vector4.java
com.jcxavier.android.opengl.sample.GameActivity.java
com.jcxavier.android.opengl.sample.TestStage.java
com.jcxavier.android.opengl.util.BitmapUtils.java
com.jcxavier.android.opengl.util.Constants.java
com.jcxavier.android.opengl.util.ReflectionUtils.java
com.jcxavier.android.opengl.util.WeakList.java
com.sample.clean.TestActivity.java
com.sample.clean.TestStage.java