Subversion Repositories AndroidProjects

Rev

Rev 1731 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.bauzoid2.graphics;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.gebauz.bauzoid2.game.BauzoidException;
import com.gebauz.bauzoid2.game.Engine;
import com.gebauz.bauzoid2.graphics.font.Font;
import com.gebauz.bauzoid2.graphics.geometry.IndexStream;
import com.gebauz.bauzoid2.graphics.geometry.VertexStream;
import com.gebauz.bauzoid2.graphics.renderstates.RenderStates;
import com.gebauz.bauzoid2.graphics.shader.PrimitiveShader;
import com.gebauz.bauzoid2.graphics.shader.ShaderLibrary;
import com.gebauz.bauzoid2.graphics.shader.ShaderProgram;
import com.gebauz.bauzoid2.graphics.sprite.SpriteShader;
import com.gebauz.bauzoid2.graphics.sprite.TileBatch;

public class Graphics
{

        // Constants========================================================================================

        // Embedded Types===================================================================================

        // Fields===========================================================================================

        public RenderStates renderStates = new RenderStates();

    private PrimitiveShader mPrimitiveShader = null;
    private SpriteShader mSpriteShader = null;
    private TileBatch mTileBatch = null;
    private Font mMainFont = null;

    public ShaderLibrary shaders = null;

    private int mWidth = 0;
    private int mHeight = 0;

        // Methods==========================================================================================

        public Graphics()
        {
                try
                {
                        renderStates.initialize();
                }
                catch (BauzoidException ex)
                {
                        ex.printStackTrace();                  
                }
        }
       
        public void init()
        {
        shaders = new ShaderLibrary();

                mPrimitiveShader = new PrimitiveShader();
        mSpriteShader = new SpriteShader();
        mTileBatch = new TileBatch();

        mMainFont = Engine.fonts.loadFont("bauzoid/fonts/ingame.bzf");
        }
       
        public void exit()
        {
                if (mPrimitiveShader != null)
        {
            mPrimitiveShader.dispose();
            mPrimitiveShader = null;
        }

        if (mSpriteShader != null)
        {
            mSpriteShader.dispose();
            mSpriteShader = null;
        }

        if (mTileBatch != null)
        {
            mTileBatch.dispose();
            mTileBatch = null;
        }

        if (mMainFont != null)
        {
            mMainFont.dispose();
            mMainFont = null;
        }

        if (shaders != null)
        {
            shaders.dispose();
            shaders = null;
        }
        }
       
        public void update()
        {
               
        }
       
        public void render()
        {
               
        }

    public void onSurfaceLost()
    {
        // destroy resources
    }

    public void onSurfaceRecreated()
    {
        // Update viewport
        Gdx.gl20.glViewport(0, 0, mWidth, mHeight);

        // recreate resources
        try
        {
            renderStates.initialize();
        }
        catch (BauzoidException ex)
        {
            ex.printStackTrace();
        }
        VertexStream.reloadManagedStreams();
        IndexStream.reloadManagedStreams();
        ShaderProgram.reloadManagedShaders();
    }

    /** Update the GL Surface's dimensions. */
    public void updateSurfaceDimensions(int w, int h)
    {
        mWidth = w;
        mHeight = h;
    }

    /** Clear render surface (color and depth buffer). */
    public void clear(float r, float g, float b, float a)
    {
        clear(r, g, b, a, true, true);
    }

    /** Same as clear(r, g, b, a, true, clearDepth). */
    public void clear(float r, float g, float b, float a, boolean clearDepth)
    {
        clear(r, g, b, a, true, clearDepth);
    }

    /** Clear render surface. */
    public void clear(float r, float g, float b, float a, boolean clearColor, boolean clearDepth)
    {
        int bits = 0;
        if (clearColor)
            bits |= GL20.GL_COLOR_BUFFER_BIT;
        if (clearDepth)
            bits |= GL20.GL_DEPTH_BUFFER_BIT;

        Gdx.gl20.glClearColor(r, g, b, a);
        Gdx.gl20.glClear(bits);
    }
       
        // Getters/Setters==================================================================================

    /** Get the GL Surface's width in pixels. */
    public final int getWidth() { return mWidth; }

    /** Get the GL Surface's height in pixels. */
    public final int getHeight() { return mHeight; }

    public final PrimitiveShader getPrimitiveShader() { return mPrimitiveShader; }
    public final SpriteShader getSpriteShader() { return mSpriteShader; }
    public final TileBatch getTileBatch() { return mTileBatch; }
    public final Font getFont() { return mMainFont; }

}