Giter Club home page Giter Club logo

spine-rs's Introduction

spine

[dependencies.spine_tiny]
git = "http://github.com/tomaka/spine-rs"
extern crate spine;

Parses a Spine document and calculates what needs to be drawn.

You can find an example here

Step 1: loading the document

Call skeleton::Skeleton::from_reader to parse the content of a document.

This function returns an Err if the document is not valid JSON or if something is not recognized in it.

let file = File::open(&Path::new("skeleton.json")).unwrap();
let skeleton = spine::skeleton::Skeleton::from_reader(file).unwrap();

Step 2: preparing for drawing

You can retrieve the list of animations and skins provided a document:

let skins = skeleton.get_skins_names();
let animations = skeleton.get_animations_names();

You can also get a list of the names of all the sprites that can possibly be drawn by this Spine animation.

let sprites = skeleton.get_attachments_names();

Note that the names do not necessarily match file names. They are the same names that you have in the Spine editor. It is your job to turn these resource names into file names if necessary.

Step 3: animating

To run an animation, you first need to call skeleton.get_animated_skin to get a SkinAnimation.

You then have 2 methods to get the sprites you need to draw:

  • directly call animation.interpolate for a given time
  • use a built-in AnimationIter iterator by calling animation.run() to run the animation with a constant period

Both methods returns a Sprites iterator over the Sprites do be drawn.

let animation = skeleton.get_animated_skin("default", Some("walk")).unwrap();

// either use `interpolate`
let sprites = animation.interpolate(0.3).unwrap();
// or use the iterator
let sprites = animation
              .run(0.1)  // iterator that interpolates sprites every 0.1s
              .nth(3);   // get the 3rd item generated when time = 0.3

The result contains an iterator over the sprites that need to be drawn with the skeleton::SRT (scale, rotate, translate)). The srt supposes that each sprite would cover the whole viewport (ie. drawn from (-1, -1) to (1, 1)). You can convert it to a premultiplied matrix using srt.to_matrix3() or srt.to_matrix4 ; if you want to apply your own matrix C over the one returned M, you need to call C * M.

for sprite in animation.interpolate(0.3).unwrap() {
    let texture = textures_list.get(&&*sprite.attachment).unwrap();
    draw(texture, &sprite.srt, &sprite.color);
}

spine-rs's People

Contributors

reefridge avatar tafia avatar tomaka avatar

Watchers

 avatar  avatar  avatar

Forkers

squirrel-labs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.