Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/enoughtea/plainspritesheetanimation
Plain sprite sheet animation library is a barebone library for a sprite sheet 2D animation support. It manages all information needed to draw a frame to screen; how exactly it is drawn is up to user.
https://github.com/enoughtea/plainspritesheetanimation
animation-sequence c-sharp texturepacker
Last synced: about 1 month ago
JSON representation
Plain sprite sheet animation library is a barebone library for a sprite sheet 2D animation support. It manages all information needed to draw a frame to screen; how exactly it is drawn is up to user.
- Host: GitHub
- URL: https://github.com/enoughtea/plainspritesheetanimation
- Owner: EnoughTea
- License: other
- Created: 2015-04-05T18:35:27.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-25T09:59:18.000Z (about 7 years ago)
- Last Synced: 2024-10-13T23:11:37.926Z (2 months ago)
- Topics: animation-sequence, c-sharp, texturepacker
- Language: C#
- Homepage:
- Size: 71.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Plain sprite-sheet animation library
Plain sprite sheet animation library is a barebones library for a sprite sheet 2D animation support.
It manages all information needed to draw a frame to screen; how exactly it is drawn is up to user.It is a personal library and it is not supposed to be a complex framework, but perhaps someone could find it useful.
## Well, what is it?
Animation is represented as a sequence of frames. These could be created manually or loaded from [TexturePacker](https://www.codeandweb.com/texturepacker).
Then you can set some properties defining how resulting sequence should be played. Call sequence update method in your update logic, draw its current frame if sequence is visible, and that's it.
* Plain and simple to use and extend.
* Supports [TexturePacker](https://www.codeandweb.com/texturepacker)'s generic XML format.
* Animation sequences are serializable through DataContractSerializer.### Creating animation sequences with TexturePacker's generic XML texture atlas description
I like TexturePacker, so I will show it first. Just for the reference, TexturePacker's generic XML file looks like this:
...
Given this file, animation sequence could be created like this:
using (var atlasFileStream = File.OpenRead(atlasXmlFile)) {
var atlasData = TexturePackerAtlas.Load(atlasFileStream);
// Now that TP's data is loaded, you can do whatever with it. Lets create sequences from it:
var animationSequences = atlasData.CreateAnimationSequences().ToList();// 'animationSequences' will contain two sequences: 1) "walking" with 3 frames; 2) "standing" with 1 frame.
var walking = animationSequences.FindSequenceByName("walking");
var standing = animationSequences.FindSequenceByName("standing");
// Amount of time each frame is shown equals to 0 right now, since these are out of TexturePacker's scope.
// It possible to set duration for entire sequence:
walking.SetDuration(1f);
// 'walking' consists of 3 frames with 0 second durations, this new duration will be distributed evenly.
// Each frame's new duration would be 1/3.
// By the way, if frame durations would have been set to something other than 0,
// they would be scaled to new duration, keeping timing ratio between old and new durations.// Lets run 'walking' sequence in a loop:
walking.AnimationType = AnimationType.Looping;// And 'standing' needs its only frame to be shown continously:
standing.AnimationType = OnceHoldLast;// I want 'standing' sequence to start when 'walking' one stops:
walking.Stopped += (sequence, _) => standing.Start();
// 'Stopped' event will occur when sequence is stopped. It can happen either when its time is up,
// or when its 'Animating' property is changed by Stop() call or direct change by user.
// 'Started' event works in similar way.
}### Creating animation sequences through code:
Same sequences as in example above can be created through code.
string walkingName = "walking";
var walkingFrames = new List {
new AnimationFrame(new TextureRegion(0, 0, 32, 48), 1/3f),
new AnimationFrame(new TextureRegion(32, 0, 32, 48), 1/3f),
new AnimationFrame(new TextureRegion(64, 0, 32, 48), 1/3f),
};var walking = new AnimationSequence(walkingName, walkingFrames);
var standing = new AnimationSequence("standing",
new[] { new AnimationFrame(new TextureRegion(96, 0, 59, 83), 0) });### One last thing
Animations need to know how much time has passed, so they need to be updated somewhere:
private void YourLogicUpdate(float deltaTimeBetweenUpdatesInSeconds) {
...
animationSequences.Update(deltaTimeBetweenUpdatesInSeconds);
}There is no built-in renderer, so you will need to have your own. Then rendering logic could look like this:
private void YourDraw() {
var sequencesWhichNeedsDrawing = yourAnimationSequences.GetVisibleSequences();
foreach (var sequence in sequencesWhichNeedsDrawing) {
var texture = YourTextureManager.GetTexture(sequence.TextureId);
var frameToDraw = sequence.CurrentFrame;
var textureSourceTexels = frameToDraw.Source;
var frameDrawOffset = frameToDraw.Origin;
var screenPosition = yourObjectPosition + frameDrawOffset;
yourSpriteBatch.Draw(texture, screenPosition, textureSourceTexels, Color.White);
}
}### Unit tests NuGet references
You may notice that NuGet packages are not in the repository, so do not forget to set up package restoration in Visual Studio:
Tools menu → Options → Package Manager → General → "Allow NuGet to download missing packages during build" should be selected.
If you have a build server then it needs to be setup with an environment variable 'EnableNuGetPackageRestore' set to true.
If you do not use Visual Studio, then I guess that you already know how to restore packages from console.