Esempio n. 1
0
 public KAnimFrame(KAnimBank parent)
 {
     if (parent == null)
     {
         throw new ArgumentNullException();
     }
     Parent = parent;
 }
Esempio n. 2
0
        private static KAnimFrame AddAnimFrame(KAnimBank parent, int hash, int size)
        {
            KAnimFrame frame = new KAnimFrame(parent);

            frame.X      = size;
            frame.Y      = size;
            frame.Width  = 2 * size;
            frame.Height = 2 * size;

            AddAnimElement(frame, hash);

            frame.ElementCount = frame.Elements.Count;

            parent.Frames.Add(frame);
            return(frame);
        }
Esempio n. 3
0
        private static KAnimBank AddBank(KAnim parent, string name, int size)
        {
            int hash = name.KHash();

            parent.BankNames[hash] = name;

            KAnimBank bank = new KAnimBank(parent);

            bank.Name = name;
            bank.Hash = hash;
            bank.Rate = 1;

            AddAnimFrame(bank, hash, size);

            bank.FrameCount = bank.Frames.Count;

            parent.Banks.Add(bank);
            return(bank);
        }
Esempio n. 4
0
        public static void MakePlaceholderBuilding(string name, int cellWidth, int cellHeight, out Bitmap atlas, out KBuild build, out KAnim anim)
        {
            int buildingWidth  = cellWidth * CELL_SIZE;
            int buildingHeight = cellHeight * CELL_SIZE;

            build             = new KBuild();
            build.Name        = name;
            build.Version     = KBuild.CURRENT_BUILD_VERSION;
            build.SymbolCount = 4;
            build.FrameCount  = 4;

            KSymbol ui    = AddSymbol(build, "ui", UI_IMG_WIDTH, UI_IMG_HEIGHT);
            Bitmap  uiBmp = MakePlaceholderUIBitmap((int)UI_IMG_WIDTH, Color.Blue);
            Sprite  uiSpr = new Sprite(ui.Frames[0], uiBmp);

            KSymbol place    = AddSymbol(build, "place", buildingWidth, buildingHeight);
            Bitmap  placeBmp = MakePlaceholderPlaceBitmap(name, buildingWidth, buildingHeight);
            Sprite  placeSpr = new Sprite(place.Frames[0], placeBmp);

            KSymbol off    = AddSymbol(build, "off", buildingWidth, buildingHeight);
            Bitmap  offBmp = MakePlaceholderBitmap(name, buildingWidth, buildingHeight, Color.Red);
            Sprite  offSpr = new Sprite(off.Frames[0], offBmp);

            KSymbol on    = AddSymbol(build, "on", buildingWidth, buildingHeight);
            Bitmap  onBmp = MakePlaceholderBitmap(name, buildingWidth, buildingHeight, Color.Green);
            Sprite  onSpr = new Sprite(on.Frames[0], onBmp);

            build.SymbolCount = build.Symbols.Count;

            atlas = SpriteUtils.RebuildAtlas(new Sprite[] { uiSpr, placeSpr, offSpr, onSpr });

            anim               = new KAnim();
            anim.Version       = 5;
            anim.ElementCount  = 0;
            anim.FrameCount    = 0;
            anim.BankCount     = 4;
            anim.MaxVisSymbols = 1;

            KAnimBank uiAnim    = AddBank(anim, "ui", Math.Max(buildingWidth, buildingHeight));
            KAnimBank placeAnim = AddBank(anim, "place", Math.Max(buildingWidth, buildingHeight));
            KAnimBank onAnim    = AddBank(anim, "on", Math.Max(buildingWidth, buildingHeight));
            KAnimBank offAnim   = AddBank(anim, "off", Math.Max(buildingWidth, buildingHeight));
        }
Esempio n. 5
0
        public static bool WriteAnim(string animFile, KAnim anim)
        {
            try
            {
                using (FileStream file = new FileStream(animFile, FileMode.Create))
                    using (BinaryWriter writer = new BinaryWriter(file))
                    {
                        writer.Write(Encoding.ASCII.GetBytes(ANIM_HEADER));

                        writer.Write(anim.Version);
                        writer.Write(anim.FrameCount);
                        writer.Write(anim.ElementCount);
                        writer.Write(anim.BankCount);

                        for (int b = 0; b < anim.BankCount; b++)
                        {
                            KAnimBank bank = anim.Banks[b];

                            writer.WriteKString(bank.Name);
                            writer.Write(bank.Hash);
                            writer.Write(bank.Rate);
                            writer.Write(bank.FrameCount);

                            for (int f = 0; f < bank.FrameCount; f++)
                            {
                                KAnimFrame frame = bank.Frames[f];

                                writer.Write(frame.X);
                                writer.Write(frame.Y);
                                writer.Write(frame.Width);
                                writer.Write(frame.Height);
                                writer.Write(frame.ElementCount);

                                for (int e = 0; e < frame.ElementCount; e++)
                                {
                                    KAnimElement element = frame.Elements[e];

                                    writer.Write(element.SymbolHash);
                                    writer.Write(element.FrameNumber);
                                    writer.Write(element.FolderHash);
                                    writer.Write(element.Flags);
                                    writer.Write(element.Alpha);
                                    writer.Write(element.Blue);
                                    writer.Write(element.Green);
                                    writer.Write(element.Red);
                                    writer.Write(element.M00);
                                    writer.Write(element.M10);
                                    writer.Write(element.M01);
                                    writer.Write(element.M11);
                                    writer.Write(element.M02);
                                    writer.Write(element.M12);
                                    writer.Write(element.Unused);
                                }
                            }
                        }

                        writer.Write(anim.MaxVisSymbols);

                        int numHashes = anim.BankNames.Count;
                        writer.Write(numHashes);
                        foreach (KeyValuePair <int, string> kvp in anim.BankNames)
                        {
                            writer.Write(kvp.Key);
                            writer.WriteKString(kvp.Value);
                        }
                    }

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Esempio n. 6
0
        public static KAnim ReadAnim(string animFile)
        {
            if (!File.Exists(animFile))
            {
                throw new ArgumentException("The given file does not exist.");
            }

            using (FileStream file = new FileStream(animFile, FileMode.Open))
                using (BinaryReader reader = new BinaryReader(file))
                {
                    // Verify header
                    string header = Encoding.ASCII.GetString(reader.ReadBytes(ANIM_HEADER.Length));
                    if (header != ANIM_HEADER)
                    {
                        throw new Exception("Header is not valid.");
                    }

                    // Parse Anim
                    KAnim anim = new KAnim();

                    anim.Version      = reader.ReadInt32();
                    anim.FrameCount   = reader.ReadInt32();
                    anim.ElementCount = reader.ReadInt32();
                    anim.BankCount    = reader.ReadInt32();

                    for (int a = 0; a < anim.BankCount; a++)
                    {
                        KAnimBank bank = new KAnimBank(anim);

                        bank.Name       = reader.ReadKString();
                        bank.Hash       = reader.ReadInt32();
                        bank.Rate       = reader.ReadSingle();
                        bank.FrameCount = reader.ReadInt32();

                        for (int f = 0; f < bank.FrameCount; f++)
                        {
                            KAnimFrame frame = new KAnimFrame(bank);

                            frame.X            = reader.ReadSingle();
                            frame.Y            = reader.ReadSingle();
                            frame.Width        = reader.ReadSingle();
                            frame.Height       = reader.ReadSingle();
                            frame.ElementCount = reader.ReadInt32();

                            for (int e = 0; e < frame.ElementCount; e++)
                            {
                                KAnimElement element = new KAnimElement(frame);

                                element.SymbolHash  = reader.ReadInt32();
                                element.FrameNumber = reader.ReadInt32();
                                element.FolderHash  = reader.ReadInt32();
                                element.Flags       = reader.ReadInt32();
                                element.Alpha       = reader.ReadSingle();
                                element.Blue        = reader.ReadSingle();
                                element.Green       = reader.ReadSingle();
                                element.Red         = reader.ReadSingle();
                                element.M00         = reader.ReadSingle();
                                element.M10         = reader.ReadSingle();
                                element.M01         = reader.ReadSingle();
                                element.M11         = reader.ReadSingle();
                                element.M02         = reader.ReadSingle();
                                element.M12         = reader.ReadSingle();
                                element.Unused      = reader.ReadSingle();

                                frame.Elements.Add(element);
                            }

                            bank.Frames.Add(frame);
                        }

                        anim.Banks.Add(bank);
                    }

                    anim.MaxVisSymbols = reader.ReadInt32();

                    // Read Anim Hashes
                    int numHashes = reader.ReadInt32();
                    for (int h = 0; h < numHashes; h++)
                    {
                        int    hash = reader.ReadInt32();
                        string str  = reader.ReadKString();
                        anim.BankNames[hash] = str;
                    }

                    return(anim);
                }
        }