Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.graphics;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.gebauz.bauzoid.app.BauzoidException;
import com.gebauz.bauzoid.game.Game;
import com.gebauz.bauzoid.game.GameObject;
import com.gebauz.bauzoid.graphics.model.IndexStream;
import com.gebauz.bauzoid.graphics.model.VertexStream;
import com.gebauz.bauzoid.graphics.renderstates.RenderStates;
import com.gebauz.bauzoid.graphics.shader.ShaderProgram;
import com.gebauz.bauzoid.graphics.sprite.SpriteShader;
import com.gebauz.bauzoid.graphics.sprite.TileBatch;

/** Graphics object.
 * Root of all graphics objects.
 * Stores references to objects that need to be globally available.
 *
 */

public class Graphics extends GameObject
{
        private int mWidth = 0;
        private int mHeight = 0;
       
        public RenderStates renderStates = new RenderStates();
       
        public TileBatch mBatch = null;
       
        /** Global font instance. */
        // private Font ...
        /** Global sprite shader instance. */
        private SpriteShader mSpriteShader;
       
        /** Constructor. */
        public Graphics(Game game)
        {
                super(game);
               
                try
                {
                        renderStates.initialize();
                }
                catch (BauzoidException ex)
                {
                        ex.printStackTrace();                  
                }
        }
       
        /** Initialize globally available objects. */
        public void init()
        {
                mSpriteShader = new SpriteShader(this);
               
                mBatch = new TileBatch(this);
        }
       
        /** Destroy globally available objects. */
        public void exit()
        {
                if (mBatch != null)
                {
                        mBatch.dispose();
                        mBatch = null;
                }
               
                if (mSpriteShader != null)
                {
                        mSpriteShader.dispose();
                        mSpriteShader = null;
                }
        }
       
        /** 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);
        }

        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;
        }
       
        /** 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;
        }
       
        /** Get a reference to the sprite shader. */
        public final SpriteShader getSpriteShader()
        {
                return mSpriteShader;
        }
       
        /** Get the aspect ratio. */
        public final float getAspect()
        {
                return ((float)mWidth / (float)mHeight);
        }
       
        /** Get the scale from DIP to pixels. */
        public final float getDipToPixelScale()
        {
                if ((Gdx.app.getType() == ApplicationType.Desktop) ||
                        (Gdx.app.getType() == ApplicationType.Applet))
                        return 1.0f;
               
                return Gdx.graphics.getDensity();
        }
       
        public final TileBatch getBatch()
        {
                return mBatch;
        }

}