예제 #1
0
        private static KFrame AddFrame(KSymbol parent, float width, float height)
        {
            KFrame frame = new KFrame(parent);

            frame.Index       = 0;
            frame.Duration    = 1;
            frame.ImageIndex  = 0;
            frame.PivotX      = 0;
            frame.PivotY      = -height;
            frame.PivotWidth  = 2 * width;
            frame.PivotHeight = 2 * height;
            frame.UV_X1       = 0f;
            frame.UV_Y1       = 0f;
            frame.UV_X2       = 0f;
            frame.UV_Y2       = 0f;
            frame.Time        = 0;

            parent.Frames.Add(frame);
            return(frame);
        }
예제 #2
0
        public static KBuild ReadBuild(string buildFile)
        {
            if (!File.Exists(buildFile))
            {
                throw new ArgumentException("The given file does not exist.");
            }

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

                    // Parse Build, Symbols, Frames
                    KBuild build = new KBuild();

                    build.Version     = reader.ReadInt32();
                    build.SymbolCount = reader.ReadInt32();
                    build.FrameCount  = reader.ReadInt32();
                    build.Name        = reader.ReadKString();

                    for (int s = 0; s < build.SymbolCount; s++)
                    {
                        KSymbol symbol = new KSymbol(build);
                        symbol.Hash       = reader.ReadInt32();
                        symbol.Path       = (build.Version > 9 ? reader.ReadInt32() : 0);
                        symbol.Color      = reader.ReadColor32();
                        symbol.Flags      = reader.ReadKSymbolFlags();
                        symbol.FrameCount = reader.ReadInt32();

                        int time = 0;
                        for (int f = 0; f < symbol.FrameCount; f++)
                        {
                            KFrame frame = new KFrame(symbol);
                            frame.Index       = reader.ReadInt32();
                            frame.Duration    = reader.ReadInt32();
                            frame.ImageIndex  = reader.ReadInt32();
                            frame.PivotX      = reader.ReadSingle();
                            frame.PivotY      = reader.ReadSingle();
                            frame.PivotWidth  = reader.ReadSingle();
                            frame.PivotHeight = reader.ReadSingle();
                            frame.UV_X1       = reader.ReadSingle();
                            frame.UV_Y1       = reader.ReadSingle();
                            frame.UV_X2       = reader.ReadSingle();
                            frame.UV_Y2       = reader.ReadSingle();
                            frame.Time        = time;

                            time += frame.Duration;
                            symbol.Frames.Add(frame);
                        }

                        build.Symbols.Add(symbol);
                    }

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

                    return(build);
                }
        }
예제 #3
0
        public static bool WriteBuild(string buildFile, KBuild build)
        {
            try
            {
                using (FileStream file = new FileStream(buildFile, FileMode.Create))
                    using (BinaryWriter writer = new BinaryWriter(file))
                    {
                        writer.Write(Encoding.ASCII.GetBytes(KBuild.BUILD_HEADER));

                        writer.Write(build.Version);
                        writer.Write(build.SymbolCount);
                        writer.Write(build.FrameCount);
                        writer.WriteKString(build.Name);

                        for (int s = 0; s < build.SymbolCount; s++)
                        {
                            KSymbol symbol = build.Symbols[s];

                            writer.Write(symbol.Hash);
                            if (build.Version > 9)
                            {
                                writer.Write(symbol.Path);
                            }
                            writer.Write(symbol.Color);
                            writer.Write(symbol.Flags);
                            writer.Write(symbol.FrameCount);

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

                                writer.Write(frame.Index);
                                writer.Write(frame.Duration);
                                writer.Write(frame.ImageIndex);
                                writer.Write(frame.PivotX);
                                writer.Write(frame.PivotY);
                                writer.Write(frame.PivotWidth);
                                writer.Write(frame.PivotHeight);
                                writer.Write(frame.UV_X1);
                                writer.Write(frame.UV_Y1);
                                writer.Write(frame.UV_X2);
                                writer.Write(frame.UV_Y2);
                            }
                        }

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

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
예제 #4
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());
            }
        }