Ejemplo n.º 1
0
        public SpriterKeyframe(BinReader br, int version)
        {
            time = br.readInt();

            int stamp_count = br.readInt();

            stamps = new SpriterStamp[stamp_count];

            for (int i = 0; i < stamp_count; ++i)
            {
                stamps[i] = new SpriterStamp(br, version, i);
            }
        }
Ejemplo n.º 2
0
        public void draw(string animation, int frame, Vector2 position, Vector2 scale, float angle, Vector4 color)
        {
            object_camera = Matrix.Identity(object_camera);
            object_camera = Matrix.Scale(object_camera, scale);
            object_camera = Matrix.Rotate(object_camera, angle);
            object_camera = Matrix.Translation(object_camera, position);

            if (!animations.ContainsKey(animation))
            {
                return;
            }

            SpriterAnimation anim = animations[animation];

            // determine which frames to interpolate between
            var frame_a = anim.keyframes[0];
            var frame_b = frame_a;

            if (anim.keyframes.Length > 1)
            {
                frame_b = anim.keyframes[1];
            }

            frame %= anim.length;
            for (int i = 1; i < anim.keyframes.Length; i++)
            {
                if (frame > anim.keyframes[i].time)
                {
                    frame_a = anim.keyframes[i];
                    if (i + 1 < anim.keyframes.Length)
                    {
                        frame_b = anim.keyframes[i + 1];
                    }
                    else
                    {
                        frame_b = anim.keyframes[0];
                    }
                }
            }

            // determine proportion of each frame
            float a_amt = 1, b_amt = 0;

            if (frame_a != frame_b)
            {
                int frame_b_time = frame_b.time;
                if (frame_b_time == 0)
                {
                    frame_b_time = anim.length;
                }

                b_amt = (float)(frame - frame_a.time) / (frame_b_time - frame_a.time);
                a_amt = 1.0f - b_amt;
            }

            // for each stamp in frame A
            for (int i = 0; i < frame_a.stamps.Length; ++i)
            {
                // figure out what stamp in frame B we're interpolating to
                SpriterStamp stamp_a = frame_a.stamps[i];
                SpriterStamp stamp_b = stamp_a;

                if (stamp_a.z_next_frame != -1 && frame_b.stamps.GetLength(0) > stamp_a.z_next_frame)
                {
                    stamp_b = frame_b.stamps[stamp_a.z_next_frame];
                }

                if (stamp_b.atlas_id != stamp_a.atlas_id)
                {
                    stamp_b = stamp_a;
                }

                // find stamp display transform properties
                int     spin        = stamp_a.spin;
                Vector2 pos         = stamp_a.position * a_amt + stamp_b.position * b_amt;
                Vector2 origin      = stamp_a.origin * a_amt + stamp_b.origin * b_amt;
                Vector2 stamp_scale = stamp_a.scale * a_amt + stamp_b.scale * b_amt;

                Vector2 stamp_size = atlas.sprites[stamp_a.atlas_id].tlmargin +
                                     atlas.sprites[stamp_a.atlas_id].size +
                                     atlas.sprites[stamp_a.atlas_id].brmargin;

                // find stamp rotation, correcting for spin direction
                float angle_a = stamp_a.rotation * (float)System.Math.PI / 180;
                float angle_b = stamp_b.rotation * (float)System.Math.PI / 180;

                if (spin == 1 && angle_b - angle_a < 0)
                {
                    angle_b += (float)System.Math.PI * 2;
                }

                if (spin == -1 && angle_b - angle_a > 0)
                {
                    angle_b -= (float)System.Math.PI * 2;
                }

                float rotation = angle_a * a_amt + angle_b * b_amt;

                // build stamp transform matrix
                stamp_matrix = Matrix.Identity(stamp_matrix);
                stamp_matrix = Matrix.Translation(stamp_matrix, new Vector2(-origin.x * stamp_size.x, -stamp_size.y + origin.y * stamp_size.y));
                stamp_matrix = Matrix.Scale(stamp_matrix, stamp_scale);
                stamp_matrix = Matrix.Rotate(stamp_matrix, -rotation);
                stamp_matrix = Matrix.Translation(stamp_matrix, new Vector2(pos.x, -pos.y));
                stamp_matrix = Matrix.Multiply(stamp_matrix, object_camera);

                // display!
                AppMain.renderer.addSprite(atlas.textures[atlas.active_texture],
                                           Matrix.Apply(stamp_matrix, Vector2.zero),
                                           Matrix.Apply(stamp_matrix, new Vector2(stamp_size.x, 0)),
                                           Matrix.Apply(stamp_matrix, stamp_size),
                                           Matrix.Apply(stamp_matrix, new Vector2(0, stamp_size.y)),
                                           atlas.sprites[stamp_a.atlas_id].bounds, color, 0, 0, false);
            }
        }