using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BauzoidNET.math;
using BauzoidNET.graphics.renderstates;
namespace BauzoidNET
.graphics.sprite
{
public class SpriteInstance
{
private SpriteRegion
[] mSpriteRegions
= null;
private int mCurrentFrame
= 0;
public SpriteTransform transform
= new SpriteTransform
();
/** Alpha multiplier for the color. */
public float alpha
= 1
.0f
;
public Vector4 color
= new Vector4
(1
.0f, 1
.0f, 1
.0f, 1
.0f
);
public Vector4 fogColor
= new Vector4
(0,
0,
0,
0);
/** For cases where we don't need/want an extra Sprite field variable externally. */
private Sprite mInternalSprite
= null;
// Matrices for temp storage so we don't have to re-allocate new matrices every frame.
private Matrix4 mScale
= new Matrix4
();
private Matrix4 mMirror
= new Matrix4
();
private Matrix4 mPivotTranslate
= new Matrix4
();
private Matrix4 mRotateZ
= new Matrix4
();
private Matrix4 mTranslate
= new Matrix4
();
private Matrix4 mModelMatrix
= new Matrix4
();
// Methods==========================================================================================
public SpriteInstance
(Sprite sprite
)
{
mInternalSprite
= sprite
;
}
public SpriteInstance
(SpriteRegion
[] regions
)
{
mSpriteRegions
= regions
;
// set default parameters
transform
.w = mSpriteRegions
[0].getWidth();
transform
.h = mSpriteRegions
[0].getHeight();
transform
.pivotX = transform
.w/2;
transform
.pivotY = transform
.h/2;
}
public SpriteInstance
(SpriteRegion region
)
: this(new SpriteRegion
[]{region
})
{
}
/** Initialize internal sprite synchronously. */
public void init
()
{
if (mInternalSprite
!= null)
{
mInternalSprite
.init();
// Reference all regions we have defined (if none defined, it's the entire sprite)
mSpriteRegions
= mInternalSprite
.getRegions();
// set default parameters
transform
.w = mSpriteRegions
[0].getWidth();
transform
.h = mSpriteRegions
[0].getHeight();
transform
.pivotX = transform
.w/2;
transform
.pivotY = transform
.h/2;
}
}
public void dispose
()
{
dispose
(false);
}
public void dispose
(bool disposeInternalSprite
)
{
// dont destroy, we may not be the only instance
if (disposeInternalSprite
&& (mInternalSprite
!= null))
{
mInternalSprite
.dispose();
mInternalSprite
= null;
}
}
/** Render using sprite parameters. */
public void render
()
{
render
(transform
.x, transform
.y, transform
.w, transform
.h, transform
.pivotX, transform
.pivotY, transform
.angle, transform
.mirrorX, transform
.mirrorY);
}
/** Render by overriding some parameters. */
public void render
(float _x,
float _y,
float _w,
float _h
)
{
render
(_x, _y, _w, _h, transform
.pivotX, transform
.pivotY, transform
.angle, transform
.mirrorX, transform
.mirrorY);
}
/** Render by overriding some parameters. */
public void render
(float _x,
float _y,
float _w,
float _h,
float _pivotX,
float _pivotY
)
{
render
(_x, _y, _w, _h, _pivotX, _pivotY, transform
.angle, transform
.mirrorX, transform
.mirrorY);
}
/** Render by overriding some parameters. */
public void render
(float _x,
float _y,
float _w,
float _h,
float _pivotX,
float _pivotY,
float _angle
)
{
render
(_x, _y, _w, _h, _pivotX, _pivotY, _angle, transform
.mirrorX, transform
.mirrorY);
}
/** Render by overriding some parameters. */
public void render
(float _x,
float _y,
float _w,
float _h,
float _pivotX,
float _pivotY,
float _angle,
bool _mirrorX,
bool _mirrorY
)
{
Sprite sprite
= getSprite
();
RenderStates rs
= sprite
.getRenderStates();
mScale
.setScale(_w
/2, _h
/2, 1
.0f
);
mPivotTranslate
.setTranslation(-_pivotX
+_w
/2,
-_pivotY
+_h
/2,
0);
mMirror
.setScale((_mirrorX
? -1 : 1),
(_mirrorY
? -1 : 1),
1);
mRotateZ
.setRotationZ(_angle
);
mTranslate
.setTranslation(_x, _y,
0);
mModelMatrix
.identity();
Matrix4
.multiply(mModelMatrix, mModelMatrix, mMirror
);
Matrix4
.multiply(mModelMatrix, mModelMatrix, mScale
);
Matrix4
.multiply(mModelMatrix, mModelMatrix, mPivotTranslate
);
Matrix4
.multiply(mModelMatrix, mModelMatrix, mRotateZ
);
Matrix4
.multiply(mModelMatrix, mModelMatrix, mTranslate
);
rs
.pushModelMatrix();
{
rs
.model = mModelMatrix
;
sprite
.performRender(getRegionIndex
(), alpha, color, fogColor
);
}
rs
.popModelMatrix();
}
/** Center the pivot. */
public void centerPivot
()
{
transform
.centerPivot();
}
// Getters/Setters==================================================================================
/** Get the last used model matrix. */
public Matrix4 getModelMatrix
()
{
return mModelMatrix
;
}
/** Get the associated region. */
public SpriteRegion getSpriteRegion
(int frame
)
{
return mSpriteRegions
[frame
];
}
/** Get the currently associated sprite region. */
public SpriteRegion getSpriteRegion
()
{
return getSpriteRegion
(mCurrentFrame
);
}
/** Get the sprite region index. */
public int getRegionIndex
(int frame
)
{
return mSpriteRegions
[frame
].getRegionIndex();
}
/** Get the current region index. */
public int getRegionIndex
()
{
return getRegionIndex
(mCurrentFrame
);
}
/** Get the associated sprite. */
public Sprite getSprite
(int frame
)
{
return mSpriteRegions
[frame
].getSprite();
}
/** Get the current associated sprite. */
public Sprite getSprite
()
{
return getSprite
(mCurrentFrame
);
}
public void setCurrentFrame
(int frame
)
{
mCurrentFrame
= frame
;
}
public int getCurrentFrame
()
{
return mCurrentFrame
;
}
public int getNumFrames
()
{
return mSpriteRegions
.Length;
}
}
}