Subversion Repositories AndroidProjects

Rev

Rev 1454 | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BauzoidNET.graphics.model;
using BauzoidNET.graphics.renderstates;
using BauzoidNET.graphics.shader;
using BauzoidNET.graphics.sprite;
using BauzoidNET.app;

using Tao.OpenGl;
#pragma warning disable 0618

namespace BauzoidNET.graphics
{
    public class Graphics : BauzoidObject
    {
        private int mWidth = 0;
        private int mHeight = 0;

        public RenderStates renderStates = new RenderStates();

        public TileBatch mBatch = null;

        /** Global font instance. */
        private Font mMainFont = null;
       
            /** Global sprite shader instance. */
            private SpriteShader mSpriteShader;

        /** Global primitive shader instance. */
        private PrimitiveShader mPrimitiveShader;

        public Graphics(BauzoidApp app) : base(app)
        {
            renderStates.initialize();
        }

        /** Initialize globally available objects. */
            public void init(int width, int height)
            {
            updateSurfaceDimensions(width, height);

            mSpriteShader = new SpriteShader(this);
            mPrimitiveShader = new PrimitiveShader(this);

            // TODO: implement font collection?
            //mMainFont = getGame().getFonts().loadFont("data/bauzoid/fonts/ingame.bzf");
            //mMainFont = FontUtil.createFontFromBinaryFile(this, "data/bauzoid/fonts/ingame.bzf");
            mMainFont = FontUtil.createFontFromBinaryResource(this, BauzoidNET.Properties.Resources.ingame, BauzoidNET.Properties.Resources.ingame_tex);

            mBatch = new TileBatch(this);

            }
       
            /** Destroy globally available objects. */
            public void exit()
            {
            if (mBatch != null)
            {
                mBatch.dispose();
                mBatch = null;
            }
               
            if (mPrimitiveShader != null)
            {
                mPrimitiveShader.dispose();
                mPrimitiveShader = null;
            }
               
                    if (mSpriteShader != null)
                    {
                            mSpriteShader.dispose();
                            mSpriteShader = null;
                    }

            if (mMainFont != null)
            {
                mMainFont.dispose();
                mMainFont = 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, bool clearDepth)
            {
                    clear(r, g, b, a, true, clearDepth);
            }
       
            /** Clear render surface. */
            public void clear(float r, float g, float b, float a, bool clearColor, bool clearDepth)
            {
                    int bits = 0;
                    if (clearColor)
                            bits |= Gl.GL_COLOR_BUFFER_BIT;
                    if (clearDepth)
                            bits |= Gl.GL_DEPTH_BUFFER_BIT;

                    Gl.glClearColor(r, g, b, a);
                    Gl.glClear(bits);
            }

            public void onSurfaceLost()
            {
                    // destroy resources
            }
       
            public void onSurfaceRecreated()
            {
                    // Update viewport
                    Gl.glViewport(0, 0, mWidth, mHeight);
                       
                    // recreate resources
                    renderStates.initialize();
                    VertexStream.reloadManagedStreams();
                    IndexStream.reloadManagedStreams();
                    ShaderProgram.reloadManagedShaders();
            }
       
            /** Update the GL Surface's dimensions. */
            public void updateSurfaceDimensions(int w, int h)
            {
                    mWidth = w;
                    mHeight = h;

            Gl.glViewport(0, 0, mWidth, mHeight);
            }
       
            /** Get the GL Surface's width in pixels. */
            public int getWidth()
            {
                    return mWidth;
            }
       
            /** Get the GL Surface's height in pixels. */
            public int getHeight()
            {
                    return mHeight;
            }
       
                    /** Get the aspect ratio. */
            public float getAspect()
            {
                    return ((float)mWidth / (float)mHeight);
            }

        /** Get a reference to the sprite shader. */
        public SpriteShader getSpriteShader()
        {
            return mSpriteShader;
        }

        /** Get a reference to the primitive shader. */
        public PrimitiveShader getPrimitiveShader()
        {
            return mPrimitiveShader;
        }

            public TileBatch getBatch()
            {
                    return mBatch;
            }
       
            public Font getFont()
            {
                    return mMainFont;
            }
    }
}