Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.Bauzoid.graphics;

import javax.microedition.khronos.opengles.GL10;

import com.gebauz.Bauzoid.graphics.model.SimpleGeometry;
import com.gebauz.Bauzoid.graphics.renderstate.BlendingStates.BlendingMode;
import com.gebauz.Bauzoid.graphics.texture.Texture2D;
import com.gebauz.Bauzoid.math.Vector2;
import com.gebauz.Bauzoid.math.Vector4;

public class Sprite2D {
       
        public float x;
        public float y;
        public float w;
        public float h;
        public float angle;
        public float pivotX;
        public float pivotY;
       
        public boolean mirrorX = false;
        public boolean mirrorY = false;
       
        protected SimpleGeometry mSpriteMesh = new SimpleGeometry();
        //protected Texture2D mSpriteTexture = new Texture2D();
       
        public Sprite2D()
        {
        }
       
        public void init(int resourceID)
        {
                //mSpriteTexture.loadTexture(resourceID);
               
                float vertices[] = {-0.5f, 0.5f, 0.0f,
                                -0.5f, -0.5f, 0.0f,
                                0.5f, -0.5f, 0.0f,
                                0.5f,  0.5f, 0.0f
                };
               
/*              float vertices[] = {-10.0f, 10.0f, 0.0f,
                                -10.0f, -10.0f, 0.0f,
                                10.0f, -10.0f, 0.0f,
                                10.0f,  10.0f, 0.0f
                };*/

               
                short[] indices = { 0, 1, 2, 0, 2, 3 };
               
                float colors[] = {1.0f, 1.0f, 1.0f, 1.0f,
                                1.0f, 1.0f, 1.0f, 1.0f,
                                1.0f, 1.0f, 1.0f, 1.0f,
                                1.0f, 1.0f, 1.0f, 1.0f
                };
               
                float texCoords[] = {0.0f, 1.0f,
                                0.0f, 0.0f,
                                1.0f, 0.0f,
                                1.0f, 1.0f
                };

                mSpriteMesh.setVertices(vertices);
                mSpriteMesh.setColors(colors);
                mSpriteMesh.setIndices(indices);
                mSpriteMesh.setTexCoords(texCoords);

                x = 0.0f;
                y = 0.0f;
                angle = 0.0f;
                //w = mSpriteTexture.width;
                //h = mSpriteTexture.height;
                pivotX = w/2.0f;
                pivotY = h/2.0f;
        }
       
        public void init(int resourceID, float _x, float _y, float _w, float _h)
        {
                init(resourceID);
               
                x = _x; y = _y; w = _w; h = _h;
                pivotX = w/2.0f;
                pivotY = h/2.0f;
        }
       
        public void update(float deltaTime)
        {
               
        }
       
        public void render()
        {
                GL10 gl = GLUtil.getGL();
               
                // transform
                gl.glMatrixMode(GL10.GL_MODELVIEW);
                gl.glPushMatrix();
                {
                        float px = pivotX - w/2.0f;
                        float py = pivotY - h/2.0f;

                        px /= w;
                        py /= h;
                       
                        gl.glLoadIdentity();

                        gl.glTranslatef(px, py, 0.0f);
                       
                        gl.glTranslatef(x, y, 0.0f);

                        gl.glRotatef(angle, 0.0f, 0.0f, 1.0f);
                       
                        gl.glScalef(w, h, 1.0f);
                       
                        float sx = 1.0f;
                        float sy = 1.0f;
                        if (mirrorX)
                                sx = -1.0f;
                        if (mirrorY)
                                sy = -1.0f;

                        gl.glScalef(sx, sy, 1.0f);
                       
                        gl.glTranslatef(-px, -py, 0.0f);

                        GLUtil.getRenderStates().depthTest.setEnabled(false);
                        //GLUtil.getRenderStates().getTextureStage(0).bindTexture(mSpriteTexture);
                       
                        GLUtil.getRenderStates().blending.setEnabled(true);
                        GLUtil.getRenderStates().blending.setBlendingMode(BlendingMode.ALPHABLEND);

                        if (mirrorX || mirrorY)
                                GLUtil.getRenderStates().culling.setEnabled(false);
                       
                        GLUtil.getRenderStates().activate();
                        mSpriteMesh.render();
                        GLUtil.getRenderStates().deactivate();
                       
                        GLUtil.getRenderStates().getTextureStage(0).bindTexture(null);
                       
                }
                gl.glPopMatrix();

        }
       
/*      public int getTextureWidth()
        {
                return mSpriteTexture.width;
        }
       
        public int getTextureHeight()
        {
                return mSpriteTexture.height;
        }
       
        public void setTextureArea(float _x, float _y, float _w, float _h)
        {
                float tw = mSpriteTexture.width;
                float th = mSpriteTexture.height;                              
                float texCoords[] = {_x/tw, (_y+_h)/th,
                                _x/tw, _y/th,
                                (_x+_w)/tw, _y/th,
                                (_x+_w)/tw, (_y+_h)/th
                };
               
                mSpriteMesh.setTexCoords(texCoords);
        }
       
        public void setColor(float r, float g, float b, float a)
        {
                float colors[] = {
                                r, g, b, a,
                                r, g, b, a,
                                r, g, b, a,
                                r, g, b, a
                };
               
                mSpriteMesh.setColors(colors);
        }
       
        public void setColor(Vector4 color)
        {
                setColor(color.x, color.y, color.z, color.w);
        }
       
        public void replaceTexture(Texture2D texture)
        {
                mSpriteTexture = texture;
                w = mSpriteTexture.width;
                h = mSpriteTexture.height;
                pivotX = w/2.0f;
                pivotY = h/2.0f;
        }
       
        public Texture2D getTexture()
        {
                return mSpriteTexture;
        }

        public SimpleGeometry getMesh()
        {
                return mSpriteMesh;
        }*/

       
        /** Get the top left point considering pivot. */
        public Vector2 getTopLeft()
        {
                return new Vector2(x - pivotX, y - pivotY);
        }
       
        /** Get the bottom right point considering pivot. */
        public Vector2 getBottomRight()
        {
                return new Vector2(x - pivotX + w, y - pivotY + h);
        }
       
        /** check if the point is inside the sprite. */
        public boolean isInside(float x, float y)
        {
                Vector2 topLeft = getTopLeft();
                Vector2 bottomRight = getBottomRight();
               
                return ( (x >= topLeft.x) && (y >= topLeft.y) &&
                                 (x <= bottomRight.x) && (y <= bottomRight.y) );
        }
       
       
}