Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1051 | chris | 1 | package com.gebauz.bauzoid.graphics.sprite; |
| 2 | |||
| 3 | import com.gebauz.bauzoid.graphics.renderstates.RenderStates; |
||
| 4 | import com.gebauz.bauzoid.math.Matrix4; |
||
| 5 | import com.gebauz.bauzoid.math.Vector4; |
||
| 6 | |||
| 7 | /** A single instance of a sprite that uses a SpriteRegion and SpriteParameters instance to render the frame in the world. */ |
||
| 8 | public class SpriteInstance |
||
| 9 | { |
||
| 10 | // Constants======================================================================================== |
||
| 11 | |||
| 12 | // Embedded Types=================================================================================== |
||
| 13 | |||
| 14 | // Fields=========================================================================================== |
||
| 15 | |||
| 16 | private SpriteRegion[] mSpriteRegions = null; |
||
| 17 | |||
| 18 | private int mCurrentFrame = 0; |
||
| 19 | |||
| 20 | public SpriteTransform transform = new SpriteTransform(); |
||
| 21 | |||
| 22 | /** Alpha multiplier for the color. */ |
||
| 23 | public float alpha = 1.0f; |
||
| 24 | public Vector4 color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); |
||
| 25 | |||
| 26 | /** For cases where we don't need/want an extra Sprite field variable externally. */ |
||
| 27 | private Sprite mInternalSprite = null; |
||
| 28 | |||
| 29 | // Matrices for temp storage so we don't have to re-allocate new matrices every frame. |
||
| 30 | private Matrix4 mScale = new Matrix4(); |
||
| 31 | private Matrix4 mMirror = new Matrix4(); |
||
| 32 | private Matrix4 mPivotTranslate = new Matrix4(); |
||
| 33 | private Matrix4 mRotateZ = new Matrix4(); |
||
| 34 | private Matrix4 mTranslate = new Matrix4(); |
||
| 35 | |||
| 36 | private Matrix4 mModelMatrix = new Matrix4(); |
||
| 37 | |||
| 38 | // Methods========================================================================================== |
||
| 39 | |||
| 40 | public SpriteInstance(Sprite sprite) |
||
| 41 | { |
||
| 42 | mInternalSprite = sprite; |
||
| 43 | } |
||
| 44 | |||
| 45 | public SpriteInstance(SpriteRegion[] regions) |
||
| 46 | { |
||
| 47 | mSpriteRegions = regions; |
||
| 48 | |||
| 49 | // set default parameters |
||
| 50 | transform.w = mSpriteRegions[0].getWidth(); |
||
| 51 | transform.h = mSpriteRegions[0].getHeight(); |
||
| 52 | transform.pivotX = transform.w/2; |
||
| 53 | transform.pivotY = transform.h/2; |
||
| 54 | } |
||
| 55 | |||
| 56 | public SpriteInstance(SpriteRegion region) |
||
| 57 | { |
||
| 58 | this (new SpriteRegion[]{region}); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** Initialize internal sprite asynchronously. */ |
||
| 62 | public void initAsync() |
||
| 63 | { |
||
| 64 | if (mInternalSprite != null) |
||
| 65 | mInternalSprite.initAsync(); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** Initialize internal sprite synchronously. */ |
||
| 69 | public void init() |
||
| 70 | { |
||
| 71 | if (mInternalSprite != null) |
||
| 72 | { |
||
| 73 | mInternalSprite.init(); |
||
| 74 | |||
| 75 | // Reference all regions we have defined (if none defined, it's the entire sprite) |
||
| 76 | mSpriteRegions = mInternalSprite.getRegions(); |
||
| 77 | |||
| 78 | // set default parameters |
||
| 79 | transform.w = mSpriteRegions[0].getWidth(); |
||
| 80 | transform.h = mSpriteRegions[0].getHeight(); |
||
| 81 | transform.pivotX = transform.w/2; |
||
| 82 | transform.pivotY = transform.h/2; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | public void dispose() |
||
| 87 | { |
||
| 88 | if (mInternalSprite != null) |
||
| 89 | { |
||
| 90 | mInternalSprite.dispose(); |
||
| 91 | mInternalSprite = null; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** Render using sprite parameters. */ |
||
| 96 | public void render() |
||
| 97 | { |
||
| 98 | render(transform.x, transform.y, transform.w, transform.h, transform.pivotX, transform.pivotY, transform.angle, transform.mirrorX, transform.mirrorY); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** Render by overriding some parameters. */ |
||
| 102 | public void render(float _x, float _y, float _w, float _h) |
||
| 103 | { |
||
| 104 | render(_x, _y, _w, _h, transform.pivotX, transform.pivotY, transform.angle, transform.mirrorX, transform.mirrorY); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** Render by overriding some parameters. */ |
||
| 108 | public void render(float _x, float _y, float _w, float _h, float _pivotX, float _pivotY) |
||
| 109 | { |
||
| 110 | render(_x, _y, _w, _h, _pivotX, _pivotY, transform.angle, transform.mirrorX, transform.mirrorY); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** Render by overriding some parameters. */ |
||
| 114 | public void render(float _x, float _y, float _w, float _h, float _pivotX, float _pivotY, float _angle) |
||
| 115 | { |
||
| 116 | render(_x, _y, _w, _h, _pivotX, _pivotY, _angle, transform.mirrorX, transform.mirrorY); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** Render by overriding some parameters. */ |
||
| 120 | public void render(float _x, float _y, float _w, float _h, float _pivotX, float _pivotY, float _angle, boolean _mirrorX, boolean _mirrorY) |
||
| 121 | { |
||
| 122 | Sprite sprite = getSprite(); |
||
| 123 | |||
| 124 | RenderStates rs = sprite.getRenderStates(); |
||
| 125 | |||
| 126 | mScale.setScale(_w/2, _h/2, 1.0f); |
||
| 127 | mPivotTranslate.setTranslation(-_pivotX+_w/2, -_pivotY+_h/2, 0); |
||
| 128 | mMirror.setScale((_mirrorX ? -1 : 1), (_mirrorY ? -1 : 1), 1); |
||
| 129 | mRotateZ.setRotationZ(_angle); |
||
| 130 | mTranslate.setTranslation(_x, _y, 0); |
||
| 131 | |||
| 132 | mModelMatrix.identity(); |
||
| 133 | |||
| 134 | Matrix4.multiply(mModelMatrix, mModelMatrix, mMirror); |
||
| 135 | Matrix4.multiply(mModelMatrix, mModelMatrix, mScale); |
||
| 136 | Matrix4.multiply(mModelMatrix, mModelMatrix, mPivotTranslate); |
||
| 137 | Matrix4.multiply(mModelMatrix, mModelMatrix, mRotateZ); |
||
| 138 | Matrix4.multiply(mModelMatrix, mModelMatrix, mTranslate); |
||
| 139 | |||
| 140 | rs.pushModelMatrix(); |
||
| 141 | { |
||
| 142 | rs.model = mModelMatrix; |
||
| 143 | sprite.performRender(getRegionIndex(), alpha, color); |
||
| 144 | } |
||
| 145 | rs.popModelMatrix(); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** Center the pivot. */ |
||
| 149 | public void centerPivot() |
||
| 150 | { |
||
| 151 | transform.centerPivot(); |
||
| 152 | } |
||
| 153 | |||
| 154 | // Getters/Setters================================================================================== |
||
| 155 | |||
| 156 | /** Get the last used model matrix. */ |
||
| 157 | public final Matrix4 getModelMatrix() |
||
| 158 | { |
||
| 159 | return mModelMatrix; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** Get the associated region. */ |
||
| 163 | public final SpriteRegion getSpriteRegion(int frame) |
||
| 164 | { |
||
| 165 | return mSpriteRegions[frame]; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** Get the currently associated sprite region. */ |
||
| 169 | public final SpriteRegion getSpriteRegion() |
||
| 170 | { |
||
| 171 | return getSpriteRegion(mCurrentFrame); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** Get the sprite region index. */ |
||
| 175 | public final int getRegionIndex(int frame) |
||
| 176 | { |
||
| 177 | return mSpriteRegions[frame].getRegionIndex(); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** Get the current region index. */ |
||
| 181 | public final int getRegionIndex() |
||
| 182 | { |
||
| 183 | return getRegionIndex(mCurrentFrame); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** Get the associated sprite. */ |
||
| 187 | public final Sprite getSprite(int frame) |
||
| 188 | { |
||
| 189 | return mSpriteRegions[frame].getSprite(); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** Get the current associated sprite. */ |
||
| 193 | public final Sprite getSprite() |
||
| 194 | { |
||
| 195 | return getSprite(mCurrentFrame); |
||
| 196 | } |
||
| 197 | |||
| 198 | public final void setCurrentFrame(int frame) |
||
| 199 | { |
||
| 200 | mCurrentFrame = frame; |
||
| 201 | } |
||
| 202 | |||
| 203 | public final int getCurrentFrame() |
||
| 204 | { |
||
| 205 | return mCurrentFrame; |
||
| 206 | } |
||
| 207 | |||
| 208 | public final int getNumFrames() |
||
| 209 | { |
||
| 210 | return mSpriteRegions.length; |
||
| 211 | } |
||
| 212 | |||
| 213 | } |
||
| 214 | |||
| 215 |