Example #1
0
        /// <summary>Gets an already loaded SPA from the cache.</summary>
        /// <returns>An SPA object if found; null otherwise.</returns>
        public static SPA Get(string name)
        {
            SPA result = null;

            Instances.TryGetValue(name, out result);
            return(result);
        }
        public override bool LoadData(byte[] data, ImagePackage package)
        {
            // Create it now:
            SPAFile = new SPA(package.location.absoluteNoHash, data);

            return(true);
        }
Example #3
0
        /// <summary>Creates a new playable instance of the given SPA animation. You'll need to call Setup.</summary>
        public SPAInstance(SPA animation)
        {
            Animation = animation;
            float fr = (float)animation.FrameRate;

            if (fr == 0f)
            {
                FrameDelay = float.MaxValue;
            }
            else
            {
                FrameDelay = 1f / fr;
            }
        }
        public override bool InternallyCached(Location path, ImagePackage package)
        {
            // Is it an animation that has been cached?

            // Might already be loaded - let's check:
            SPAFile = SPA.Get(path.absoluteNoHash);

            if (SPAFile != null)
            {
                //It's already been loaded - use that.
                package.Done();
                return(true);
            }

            return(false);
        }
Example #5
0
        /// <summary>Creates a new playable instance of the given SPA animation using the given shader set.</summary>
        public SPAInstance(SPA animation, ShaderSet shaders)
        {
            Animation = animation;
            float fr = (float)animation.FrameRate;

            if (fr == 0f)
            {
                FrameDelay = float.MaxValue;
            }
            else
            {
                FrameDelay = 1f / fr;
            }

            Setup(shaders);
        }
Example #6
0
        /// <summary>Loads a new sprite from the given binary stream.</summary>
        /// <param name="animation">The animation this sprite belongs to.</param>
        /// <param name="reader">The binary stream that contains this sprites data.</param>
        /// <param name="id">The ID of the sprite in the animation.</param>
        public SPASprite(SPA animation, SPAReader reader, int id)
        {
            ID        = id;
            Animation = animation;

            // Read some bit flags - these give information about this frame:
            byte flags = reader.ReadByte();

            // Does it also have an alpha frame?
            // If it does, there are two images in this frame (alpha one second).
            bool hasAlphaFrame = ((flags & 1) == 1);

            // Does it have a map?
            // That says where objects are on this sprite.
            bool hasMap = ((flags & 2) == 2);

            // How many frames this sprite holds:
            FrameCount = reader.ReadUInt16();

            // How big is the image, in bytes:
            int dataSize = reader.ReadInt32();

            // Setup the sprite now:
            Sprite = new Texture2D(0, 0);

            // And load the image data:
            Sprite.LoadImage(reader.ReadBytes(dataSize));
            Width  = Sprite.width;
            Height = Sprite.height;

            // Make sure it filters correctly.
            // This is so we don't see parts of other frames around the edge of the image onscreen:
            Sprite.filterMode = FilterMode.Point;

            // Setup the scale:
            TextureScale       = new Vector2((float)Animation.FrameWidth / (float)Width, (float)Animation.FrameHeight / (float)Height);
            VerticalFrameCount = Height / Animation.FrameHeight;

            if (hasAlphaFrame)
            {
                int alphaImageSize = reader.ReadInt32();

                // Setup the temporary alpha texture:
                Texture2D alphaImage = new Texture2D(0, 0);

                // And load it's data:
                alphaImage.LoadImage(reader.ReadBytes(alphaImageSize));

                // Next, merge the alpha pixels into our main sprite.
                Color[] spritePixels = Sprite.GetPixels();
                Color[] alphaPixels  = alphaImage.GetPixels();

                if (spritePixels.Length != alphaPixels.Length)
                {
                    throw new Exception("Invalid SPA alpha channel image.");
                }

                // Set each alpha value from the grayscale of the alpha image:
                for (int i = spritePixels.Length - 1; i >= 0; i--)
                {
                    Color pixel = spritePixels[i];
                    pixel.a         = alphaPixels[i].grayscale;
                    spritePixels[i] = pixel;
                }

                // Write the pixels back:
                Sprite.SetPixels(spritePixels);
            }

            // Has it got a map?
            if (hasMap)
            {
                // Reset the readers X/Y position:
                reader.ResetCoordinates();

                // Yep! Read map flags:
                byte mapFlags = reader.ReadByte();

                // Acting as a font?
                bool isFont = ((mapFlags & 1) == 1);

                // How many entries:
                int count = (int)reader.ReadCompressed();

                // For each map entry..
                for (int i = 0; i < count; i++)
                {
                    SPAMapEntry entry;

                    if (isFont)
                    {
                        // It's a font entry:
                        entry = new SPACharacter(this, reader);
                    }
                    else
                    {
                        // It's a "normal" mapping:
                        entry = new SPAMapEntry(this, reader);
                    }

                    // Add the mapping to the SPA:
                    animation.AddToMap(entry.ID, entry);
                }

                // If it's acting as a font, we've also got things like kerning info.
                if (isFont)
                {
                    // Got kerning?
                    bool hasKerning = ((mapFlags & 2) == 2);

                    // Got additional charcodes?
                    bool additionalCharcodes = ((mapFlags & 4) == 4);

                    // Font meta?
                    bool fontMeta = ((mapFlags & 8) == 8);

                    if (hasKerning)
                    {
                        // Get the # of kerning pairs:
                        int kernCount = (int)reader.ReadCompressed();

                        // Previous char - stored relative for better compression.
                        int previousChar = 0;

                        // For each one..
                        for (int i = 0; i < kernCount; i++)
                        {
                            // Read the first:
                            int firstChar  = (int)reader.ReadCompressed() + previousChar;
                            int secondChar = (int)reader.ReadCompressed();

                            // Advance amount:
                            int advance = (int)reader.ReadCompressedSigned();

                            // Get the char and add a pair to it:
                            SPACharacter before = animation.GetCharacter(firstChar);
                            SPACharacter after  = animation.GetCharacter(secondChar);

                            if (after != null && before != null)
                            {
                                // Add the pair!
                                after.AddKerningPair(before, advance);
                            }

                            // Update previous:
                            previousChar = firstChar;
                        }
                    }

                    // Got additional charcodes?
                    // -> 1 letter with multiple charcodes that use it.

                    if (additionalCharcodes)
                    {
                        // Get the # of additionals:
                        int addCount = (int)reader.ReadCompressed();

                        // Previous char - stored relative for better compression.
                        int previousChar = 0;

                        // For each one..
                        for (int i = 0; i < addCount; i++)
                        {
                            // Read the character ID:
                            int firstChar     = (int)reader.ReadCompressed() + previousChar;
                            int extraCharcode = (int)reader.ReadCompressed();

                            // Get the char:
                            SPACharacter character = animation.GetCharacter(firstChar);

                            // Add the CC to it:
                            character.AddCharcode(extraCharcode);

                            // Add as another charcode:
                            animation.AddToMap(extraCharcode, character);

                            // Update previous:
                            previousChar = firstChar;
                        }
                    }

                    if (fontMeta)
                    {
                        // Load the font meta:
                        Animation.FontMeta = new SPAFontMeta(reader);
                    }
                }
            }
        }
Example #7
0
 /// <summary>Creates an empty sprite.</summary>
 public SPASprite(SPA animation, int id)
 {
     Animation = animation;
     ID        = id;
 }
 public SpaFormat(SPA spa)
 {
     SPAFile = spa;
 }
 public override void ClearX()
 {
     SPAFile = null;
 }