Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.framework.util;

import javax.microedition.khronos.opengles.GL10;

import com.gebauz.framework.util.renderstates.BlendingStates.BlendingMode;


public class Sprite2D {
       
        public float x;
        public float y;
        public float w;
        public float h;
        public float angle;
        public float pivotX;
        public float pivotY;
       
        protected Mesh mSpriteMesh = new Mesh();
        protected Texture mSpriteTexture = new Texture();
       
        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(long deltaTimeMs)
        {
               
        }
       
        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.glScalef(w, h, 1.0f);

                        gl.glRotatef(angle, 0.0f, 0.0f, 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);
                       
                        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;
        }
       
}