Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.Bauzoid.graphics.sprite;

import com.badlogic.gdx.graphics.Texture;
import com.gebauz.Bauzoid.graphics.Graphics;
import com.gebauz.Bauzoid.graphics.GraphicsObject;
import com.gebauz.Bauzoid.graphics.model.Geometry.PrimitiveType;
import com.gebauz.Bauzoid.graphics.model.SimpleGeometry;
import com.gebauz.Bauzoid.graphics.renderstates.RenderStates;
import com.gebauz.Bauzoid.math.Matrix4;
import com.gebauz.Bauzoid.math.Vector4;

/** Sprite class.
 * Implements a 2D sprite that renders from a portion of a
 * texture to a quad onscreen.
 *
 * The class is capable of
 * - Rotation and scale transformation via pivot point
 * - Fading (done through shader instead of setting vertex colors)
 *
 * For using multiple texture regions as frames,
 * use @link AtlasSprite.
 */

public class Sprite extends GraphicsObject
{
        public float x = 0.0f;
        public float y = 0.0f;
        public float w = 0.0f;
        public float h = 0.0f;
        public float angle = 0.0f;
        public float alpha = 1.0f;
        public Vector4 color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
       
        public boolean mirrorX = false;
        public boolean mirrorY = false;
       
        /** Rotation and scaling pivot point in absolute coordinates. */
        public float pivotX = 0.0f;
        public float pivotY = 0.0f;
       
        protected Texture mTexture = null;
        protected SimpleGeometry mMesh = null;

        /** Constructor by specifying a texture.
         * The texture's cleanup responsibility is subsequently owned by this Sprite.
         */

        public Sprite(Graphics graphics, Texture texture)
        {
                super(graphics);
                mTexture = texture;
               
                if (mTexture != null)
                {
                        w = texture.getWidth();
                        h = texture.getHeight();
                        pivotX = w/2;
                        pivotY = h/2;
                }
               
                initGeometry();
        }
       
        protected void initGeometry()
        {
                /*float[] vertices = {
                        -1.0f, -1.0f, 0.0f,
                        1.0f,  -1.0f, 0.0f,
                        1.0f,   1.0f, 0.0f,
                        -1.0f,  1.0f, 0.0f
                };*/

               
                float[] vertices = {
                        0.0f, 0.0f, 0.0f,
                        1.0f, 0.0f, 0.0f,
                        1.0f, 1.0f, 0.0f,
                        0.0f, 1.0f, 0.0f
                };
               
                /*float[] vertices = {
                        0, 0, 0.0f,
                        64.0f,  0, 0.0f,
                        64.0f,   64.0f, 0.0f,
                        0,  64.0f, 0.0f
                };              */

               
               
                float[] texCoords = {
                        0.0f, 0.0f,
                        1.0f, 0.0f,
                        1.0f, 1.0f,
                        0.0f, 1.0f
                };
               
                float[] colors = {
                        0.5f, 0.3f, 0.8f,
                        0.9f, 0.2f, 0.5f,
                        0.2f, 0.8f, 0.7f,
                        0.4f, 0.2f, 0.2f
                };
               
                short[] indices = {
                        0, 1, 2,
                        0, 2, 3
                };
               
                mMesh = new SimpleGeometry(getGraphics(), PrimitiveType.TRIANGLES);
                mMesh.setPositions(vertices);
                mMesh.setTexCoords(texCoords, true);
                mMesh.setColors(colors);
                mMesh.setIndices(indices);             
        }
       
        public void dispose()
        {
                if (mTexture != null)
                {
                        mTexture.dispose();
                        mTexture = null;
                }
        }
       
        public void update(float deltaTime)
        {
               
        }
       
        public void render()
        {
                render(x, y, w, h);
        }
       
        public void render(float _x, float _y, float _w, float _h)
        {
                SpriteShader shader = getGraphics().getSpriteShader();
                RenderStates rs = getRenderStates();
               
                Matrix4 modelMatrix = Matrix4.multiply(Matrix4.createScale(_w, _h, 1.0f), Matrix4.createTranslation(-pivotX, -pivotY, 0));
               
                modelMatrix = Matrix4.multiply(modelMatrix, Matrix4.createScale((mirrorX ? -1 : 1), (mirrorY ? -1 : 1), 1));

                modelMatrix = Matrix4.multiply(modelMatrix, Matrix4.createRotationZ(angle));

                modelMatrix = Matrix4.multiply(modelMatrix, Matrix4.createTranslation(_x, _y, 0));
               
                rs.pushModelMatrix();
                {
                        rs.model = modelMatrix;
                       
                        // draw sprite
                        shader.activate(mTexture, alpha, color);
                        {
                                rs.blending.setEnabled(true);
                                rs.culling.setEnabled(false);
                                rs.activate();
                                {
                                        mMesh.render();
                                }
                                rs.deactivate();
                        }
                        shader.deactivate();
                }
                rs.popModelMatrix();
        }

        /** Get the sprite texture's total width. */
        public final int getTextureWidth()
        {
                if (mTexture == null)
                        return 0;
                return mTexture.getWidth();
        }
       
        /** Get the sprite texture's total height. */
        public final int getTextureHeight()
        {
                if (mTexture == null)
                        return 0;
                return mTexture.getHeight();
        }
       
        /** Get a reference to the texture. */
        public final Texture getTexture()
        {
                return mTexture;
        }
       
        /** Set a new texture, with the old one (if any) getting destroyed. */
        public final void setTexture(Texture texture)
        {
                if (mTexture != null)
                        mTexture.dispose();
               
                mTexture = texture;
        }
}