Subversion Repositories AndroidProjects

Rev

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

Rev Author Line No. Line
226 chris 1
package com.gebauz.Bauzoid.graphics.sprite;
2
 
3
import com.badlogic.gdx.graphics.Texture;
4
import com.gebauz.Bauzoid.graphics.Graphics;
5
 
6
/** Sprite atlas.
7
 * Implements a sprite that defines multiple regions that can be used
8
 * as Sprite Atlas or as Animated Sprite.
9
 *
10
 */
11
public class AtlasSprite extends Sprite
12
{
238 chris 13
        private SpriteRegion[] mRegions = null;
226 chris 14
 
233 chris 15
        /** Constructor. */
238 chris 16
        public AtlasSprite(Graphics graphics, Texture texture, SpriteRegion[] regions)
226 chris 17
        {
18
                super(graphics, texture);
19
 
20
                mRegions = regions;
21
        }
22
 
23
        public void dispose()
24
        {
25
                super.dispose();
26
                mRegions = null;
27
        }
236 chris 28
 
238 chris 29
        public final AtlasSpriteInstance createSpriteInstance(int regionIndex)
236 chris 30
        {
31
                return new AtlasSpriteInstance(this, regionIndex);
32
        }
233 chris 33
 
226 chris 34
        public void update(float deltaTime)
35
        {
36
 
37
        }
38
 
39
        public void render()
40
        {
233 chris 41
        }
42
 
43
        public void render(int regionIndex)
44
        {
45
                if (mRegions == null)
46
                        return;
226 chris 47
 
238 chris 48
                SpriteRegion r = mRegions[regionIndex];
233 chris 49
 
50
                float[] texCoords = {
51
                        r.left, r.top,
52
                        r.right, r.top,
53
                        r.right, r.bottom,
54
                        r.left, r.bottom
55
                };
56
 
57
                mMesh.setTexCoords(texCoords);
58
 
59
                super.render();
226 chris 60
        }
61
 
233 chris 62
        /** Set absolute coordinates for given region. */
63
        public void setAbsolute(int regionIndex, float x, float y, float w, float h)
64
        {
65
                if ((mTexture == null) || (mRegions == null) || (regionIndex >= mRegions.length))
66
                        return;
67
 
68
                if (mRegions[regionIndex] == null)
69
                {
70
                        // create region
238 chris 71
                        mRegions[regionIndex] = new SpriteRegion(x, y, w, h);
233 chris 72
                        return;
73
                }
74
 
75
                mRegions[regionIndex].setAbsolute(mTexture, x, y, w, h);
76
        }
77
 
78
        /** Get number of defined regions. */
79
        public final int getRegionCount()
80
        {
81
                if (mRegions == null)
82
                        return 0;
83
 
84
                return mRegions.length;
85
        }
86
 
87
        /** Get region. */
238 chris 88
        public final SpriteRegion getRegion(int i)
233 chris 89
        {
90
                if (mRegions == null)
91
                        return null;
92
 
93
                return mRegions[i];
94
        }
95
 
96
        /** Get a region's width. */
97
        public final float getRegionWidth(int i)
98
        {
99
                if (mRegions == null)
100
                        return 0.0f;
101
 
102
                return (mRegions[i].right - mRegions[i].left) * mTexture.getWidth();
103
        }
104
 
105
        /** Get a region's height. */
106
        public final float getRegionHeight(int i)
107
        {
108
                if (mRegions == null)
109
                        return 0.0f;
110
 
111
                return (mRegions[i].bottom - mRegions[i].top) * mTexture.getHeight();
112
        }
113
 
226 chris 114
}
233 chris 115
 
116
 
117