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