Commit 124a2a89 authored by Tim Young's avatar Tim Young

Added ability to change individual frame images. Needed to clone spriteimage and add a frame-count

parent 76af2075
This diff is collapsed.
......@@ -4,12 +4,15 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace SpriteLibrary
/// <summary>
/// A single frame of an animation
/// </summary>
internal class AnimationSingleFrame
public Image Frame;
......@@ -22,6 +25,8 @@ namespace SpriteLibrary
ResizedFrame = null;
internal class AnimationFrame
public int SingleFrameID; //The ID number of the image we want to view
......@@ -37,6 +42,7 @@ namespace SpriteLibrary
/// <summary>
/// One animation. A series of images.
/// </summary>
internal class Animation
public int AnimationID;
......@@ -228,6 +234,7 @@ namespace SpriteLibrary
List<AnimationSingleFrame> Frames = new List<AnimationSingleFrame>();
public int FrameCount { get { return Frames.Count; } }
public int AnimationCount { get { return Animations.Count; } }
private SmartImage() { }
public SmartImage(SpriteController Controller, Image SpriteImage)
......@@ -254,6 +261,25 @@ namespace SpriteLibrary
AddAnimation(Start, SpriteImage, width, height, duration, Count);
public static SmartImage Clone(SmartImage Orig)
SmartImage tClone = new SmartImage();
tClone.MyController = Orig.MyController;
tClone.Animations = PartClone<List<Animation>>(Orig.Animations);
tClone.Frames = PartClone<List<AnimationSingleFrame>>(Orig.Frames);
return tClone;
public static T PartClone<T>(T obj)
using (var ms = new MemoryStream())
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
public Animation getAnimation(int index)
......@@ -192,7 +192,7 @@ namespace SpriteLibrary
public bool CannotMoveOutsideBox = false; //If set to true, it will not automatically move outside the picture box.
/// <summary>
/// Get or set the animation nimber. It is best to change the animation using ChangeAnimation.
/// Get or set the animation number. It is best to change the animation using ChangeAnimation.
/// It is safer.
/// </summary>
public int AnimationIndex
......@@ -209,6 +209,17 @@ namespace SpriteLibrary
get { return MyImage.AnimationCount; }
/// <summary>
/// Return the number of frames in the specified animation
/// </summary>
/// <param name="animation">the index for the animation you want to find the number of frames for</param>
/// <returns>the number of indexes in the specified animation. Returns -1 if the animation does not exist</returns>
public int AnimationFrameCount(int animation)
if (animation < 0 || animation >= MyImage.AnimationCount) return -1; //error
return MyImage.AnimationFrameCount(animation);
//The location and size of the sprite
int xPositionOnImage = -1;
int yPositionOnImage = -1;
......@@ -629,13 +640,19 @@ namespace SpriteLibrary
/// </summary>
/// <param name="OldSprite">The Sprite to make a copy of</param>
/// <param name="RetainName">If we want to set this sprite name to be that of the original. This is a terrible idea. Never do it.</param>
public Sprite(Sprite OldSprite, bool RetainName = false)
/// <param name="CloneImage">When set to true, the image is duplicated, not just referenced. If you are going to mess with individual image frames, you want to do this.</param>
public Sprite(Sprite OldSprite, bool RetainName = false, bool CloneImage = false)
MySpriteController = OldSprite.MySpriteController;
ID = MySpriteController.SpriteCount;
Width = OldSprite.Width;
Height = OldSprite.Height;
MyImage = OldSprite.MyImage;
//do a deep clone of the smartimage if we want to make a duplicate..
if (!CloneImage)
MyImage = OldSprite.MyImage;
MyImage = SmartImage.Clone(OldSprite.MyImage);
MovementSpeed = OldSprite.MovementSpeed;
SpriteOriginName = OldSprite.SpriteName;
......@@ -999,12 +999,16 @@ namespace SpriteLibrary
/// </code>
/// </example>
/// <param name="Name">The name of a sprite</param>
/// <param name="CloneImage">Normally, sprites all use the same images for their animations. To keep the memory
/// footprint small, they all reference the same sprite image. CloneImage to true will duplicate the images
/// used to make the sprite. This will use up more memory, but it will allow you to change the images without
/// affecting the rest of your sprites. This function is a fair bit slower when using CloneImage.</param>
/// <returns>A duplicate of the specified sprite. It has no location, and does not retain the sprite name.</returns>
public Sprite DuplicateSprite(string Name)
public Sprite DuplicateSprite(string Name, bool CloneImage = false)
Sprite tSprite = SpriteFromName(Name);
if (tSprite == null) return null;
return new Sprite(tSprite); //Make a new sprite that is based off the original
return new Sprite(tSprite,false,CloneImage); //Make a new sprite that is based off the original
/// <summary>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment