Ejemplo n.º 1
0
 public KAnimBank(KAnim parent)
 {
     if (parent == null)
     {
         throw new ArgumentNullException();
     }
     Parent = parent;
 }
Ejemplo n.º 2
0
        public static KAnim CreateEmptyAnim()
        {
            KAnim anim = new KAnim();

            anim.Version       = 5;
            anim.FrameCount    = 0;
            anim.ElementCount  = 0;
            anim.BankCount     = 0;
            anim.MaxVisSymbols = 0;
            return(anim);
        }
Ejemplo 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);
        }
Ejemplo 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));
        }
Ejemplo 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);
            }
        }
Ejemplo 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);
                }
        }
Ejemplo n.º 7
0
        public static void MakeSpritePack(string name, string spritesDir, out Bitmap atlas, out KBuild build, out KAnim anim)
        {
            build             = new KBuild();
            build.Name        = name;
            build.Version     = KBuild.CURRENT_BUILD_VERSION;
            build.SymbolCount = 0;
            build.FrameCount  = 0;

            anim = KAnimUtils.CreateEmptyAnim();

            string[]      imageFiles = Directory.GetFiles(spritesDir, "*.png");
            List <Sprite> sprites    = new List <Sprite>();

            foreach (string imageFile in imageFiles)
            {
                try
                {
                    Bitmap img = new Bitmap(imageFile);

                    string fileName = Path.GetFileNameWithoutExtension(imageFile);

                    int    lastUnderscore = fileName.LastIndexOf('_');
                    string symbolName     = fileName.Substring(0, lastUnderscore);
                    string indexStr       = fileName.Substring(lastUnderscore + 1);

                    int symbolHash = symbolName.KHash();
                    int index      = int.Parse(indexStr);

                    KSymbol symbol = build.GetSymbol(symbolHash);
                    if (symbol == null)
                    {
                        symbol            = new KSymbol(build);
                        symbol.Hash       = symbolHash;
                        symbol.Path       = symbolHash;
                        symbol.Color      = Color.FromArgb(0);
                        symbol.Flags      = 0;
                        symbol.FrameCount = 0;

                        build.Symbols.Add(symbol);
                        build.SymbolNames[symbolHash] = symbolName;
                        build.SymbolCount++;
                    }

                    KFrame frame = new KFrame(symbol);
                    frame.Index       = index;
                    frame.Duration    = 1;
                    frame.ImageIndex  = 0;
                    frame.PivotX      = 0;
                    frame.PivotY      = -img.Height;
                    frame.PivotWidth  = 2 * img.Width;
                    frame.PivotHeight = 2 * img.Height;
                    frame.UV_X1       = 0f;
                    frame.UV_Y1       = 0f;
                    frame.UV_X2       = 0f;
                    frame.UV_Y2       = 0f;
                    frame.Time        = 0;

                    symbol.Frames.Add(frame);
                    symbol.FrameCount++;
                    build.FrameCount++;

                    Sprite spr = new Sprite(frame, img);
                    sprites.Add(spr);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Failed to make sprite.");
                    Debug.WriteLine(ex.ToString());
                }
            }

            try
            {
                atlas = SpriteUtils.RebuildAtlas(sprites.ToArray());
            }
            catch (Exception ex)
            {
                atlas = null;
                Debug.WriteLine("Failed to pack atlas.");
                Debug.WriteLine(ex.ToString());
            }
        }