Subversion Repositories AndroidProjects

Rev

Rev 265 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
265 chris 1
package com.gebauz.Bauzoid.graphics.sprite;
2
 
3
import com.badlogic.gdx.graphics.Texture;
4
import com.gebauz.Bauzoid.graphics.Graphics;
5
import com.gebauz.Bauzoid.graphics.GraphicsObject;
6
import com.gebauz.Bauzoid.graphics.model.Geometry.PrimitiveType;
7
import com.gebauz.Bauzoid.graphics.model.SimpleGeometry;
8
import com.gebauz.Bauzoid.graphics.renderstates.RenderStates;
9
import com.gebauz.Bauzoid.math.Matrix4;
10
import com.gebauz.Bauzoid.math.Vector4;
11
 
12
/** Sprite class.
13
 * Implements a 2D sprite that renders from a portion of a
14
 * texture to a quad onscreen.
15
 *
16
 * The class is capable of
17
 * - Rotation and scale transformation via pivot point
18
 * - Fading (done through shader instead of setting vertex colors)
19
 *
20
 * For using multiple texture regions as frames,
21
 * use @link AtlasSprite.
22
 */
23
public class Sprite extends GraphicsObject
24
{
25
        public float x = 0.0f;
26
        public float y = 0.0f;
27
        public float w = 0.0f;
28
        public float h = 0.0f;
29
        public float angle = 0.0f;
30
        public float alpha = 1.0f;
31
        public Vector4 color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
32
 
33
        public boolean mirrorX = false;
34
        public boolean mirrorY = false;
35
 
36
        /** Rotation and scaling pivot point in absolute coordinates. */
37
        public float pivotX = 0.0f;
38
        public float pivotY = 0.0f;
39
 
40
        protected Texture mTexture = null;
41
        protected SimpleGeometry mMesh = null;
42
 
43
        /** Constructor by specifying a texture.
44
         * The texture's cleanup responsibility is subsequently owned by this Sprite.
45
         */
46
        public Sprite(Graphics graphics, Texture texture)
47
        {
48
                super(graphics);
49
                mTexture = texture;
50
 
51
                if (mTexture != null)
52
                {
53
                        w = texture.getWidth();
54
                        h = texture.getHeight();
55
                        pivotX = w/2;
56
                        pivotY = h/2;
57
                }
58
 
59
                initGeometry();
60
        }
61
 
62
        protected void initGeometry()
63
        {
64
                /*float[] vertices = {
65
                        -1.0f, -1.0f, 0.0f,
66
                        1.0f,  -1.0f, 0.0f,
67
                        1.0f,   1.0f, 0.0f,
68
                        -1.0f,  1.0f, 0.0f
69
                };*/
70
 
71
                float[] vertices = {
72
                        0.0f, 0.0f, 0.0f,
73
                        1.0f, 0.0f, 0.0f,
74
                        1.0f, 1.0f, 0.0f,
75
                        0.0f, 1.0f, 0.0f
76
                };
77
 
78
                /*float[] vertices = {
79
                        0, 0, 0.0f,
80
                        64.0f,  0, 0.0f,
81
                        64.0f,   64.0f, 0.0f,
82
                        0,  64.0f, 0.0f
83
                };              */
84
 
85
 
86
                float[] texCoords = {
87
                        0.0f, 0.0f,
88
                        1.0f, 0.0f,
89
                        1.0f, 1.0f,
90
                        0.0f, 1.0f
91
                };
92
 
93
                float[] colors = {
94
                        0.5f, 0.3f, 0.8f,
95
                        0.9f, 0.2f, 0.5f,
96
                        0.2f, 0.8f, 0.7f,
97
                        0.4f, 0.2f, 0.2f
98
                };
99
 
100
                short[] indices = {
101
                        0, 1, 2,
102
                        0, 2, 3
103
                };
104
 
105
                mMesh = new SimpleGeometry(getGraphics(), PrimitiveType.TRIANGLES);
106
                mMesh.setPositions(vertices);
107
                mMesh.setTexCoords(texCoords, true);
108
                mMesh.setColors(colors);
109
                mMesh.setIndices(indices);             
110
        }
111
 
112
        public void dispose()
113
        {
114
                if (mTexture != null)
115
                {
116
                        mTexture.dispose();
117
                        mTexture = null;
118
                }
119
        }
120
 
121
        public void update(float deltaTime)
122
        {
123
 
124
        }
125
 
126
        public void render()
127
        {
128
                render(x, y, w, h);
129
        }
130
 
131
        public void render(float _x, float _y, float _w, float _h)
132
        {
133
                SpriteShader shader = getGraphics().getSpriteShader();
134
                RenderStates rs = getRenderStates();
135
 
136
                Matrix4 modelMatrix = Matrix4.multiply(Matrix4.createScale(_w, _h, 1.0f), Matrix4.createTranslation(-pivotX, -pivotY, 0));
137
 
138
                modelMatrix = Matrix4.multiply(modelMatrix, Matrix4.createScale((mirrorX ? -1 : 1), (mirrorY ? -1 : 1), 1));
139
 
140
                modelMatrix = Matrix4.multiply(modelMatrix, Matrix4.createRotationZ(angle));
141
 
142
                modelMatrix = Matrix4.multiply(modelMatrix, Matrix4.createTranslation(_x, _y, 0));
143
 
144
                rs.pushModelMatrix();
145
                {
146
                        rs.model = modelMatrix;
147
 
148
                        // draw sprite
149
                        shader.activate(mTexture, alpha, color);
150
                        {
151
                                rs.blending.setEnabled(true);
152
                                rs.culling.setEnabled(false);
153
                                rs.activate();
154
                                {
155
                                        mMesh.render();
156
                                }
157
                                rs.deactivate();
158
                        }
159
                        shader.deactivate();
160
                }
161
                rs.popModelMatrix();
162
        }
269 chris 163
 
164
        /** Center the pivot. */
165
        public void centerPivot()
166
        {
167
                pivotX = w/2.0f;
168
                pivotY = h/2.0f;
169
        }
265 chris 170
 
171
        /** Get the sprite texture's total width. */
172
        public final int getTextureWidth()
173
        {
174
                if (mTexture == null)
175
                        return 0;
176
                return mTexture.getWidth();
177
        }
178
 
179
        /** Get the sprite texture's total height. */
180
        public final int getTextureHeight()
181
        {
182
                if (mTexture == null)
183
                        return 0;
184
                return mTexture.getHeight();
185
        }
186
 
187
        /** Get a reference to the texture. */
188
        public final Texture getTexture()
189
        {
190
                return mTexture;
191
        }
192
 
193
        /** Set a new texture, with the old one (if any) getting destroyed. */
194
        public final void setTexture(Texture texture)
195
        {
196
                if (mTexture != null)
197
                        mTexture.dispose();
198
 
199
                mTexture = texture;
200
        }
201
}
202
 
203