namespace TACO { using System; using System.Drawing; using System.Drawing.Imaging; public class SpriteAnimator { private Image mImage = null; private int mCurrentFrame = 0; private int mFrames; private int mFrameWidth; private int mFrameHeight; public SpriteAnimator(string filename) : this(filename, 0) { } public SpriteAnimator(string filename, int frames) { mImage = Image.FromFile(filename); mFrameHeight = (int)mImage.Height; if (frames == 0) mFrames = (int)mImage.Width / mFrameHeight; else mFrames = frames; mFrameWidth = (int)mImage.Width / mFrames; } public int CurrentFrame { get { return mCurrentFrame; } } public void Reset() { mCurrentFrame = 0; } public void NextFrame() { if (CurrentFrame == mFrames - 1) mCurrentFrame = 0; else mCurrentFrame++; } public void PrevFrame() { if (CurrentFrame == 0) mCurrentFrame = mFrames - 1; else mCurrentFrame--; } public void DrawFrame(Graphics g, Rectangle bounds) { Bitmap temp; Graphics tempG; temp = new Bitmap(mFrameWidth, mFrameHeight, PixelFormat.Format32bppArgb); tempG = Graphics.FromImage(temp); tempG.DrawImageUnscaled(mImage, -(mFrameWidth * CurrentFrame), 0); g.DrawImage(temp, bounds); temp.Dispose(); } } }