Beispiel #1
0
        static public Bitmap Pack(Atlas atlas, bool removeDuplicates)
        {
            tileMap = new bool[atlas.Width, atlas.Height];
            var atlasLen = atlas.Count;
            var i        = 0;
            var d        = 0;
            var ret      = new Bitmap(atlas.Width, atlas.Height);

            using (Graphics g = Graphics.FromImage(ret))
            {
                foreach (var s in atlas)
                {
                    Slice same = FindSameSlice(atlas, s);
                    if (same != null)
                    {
                        s.DestRect = same.DestRect;
                        // Console.WriteLine(s.Name + " and " + same.Name + " are the same!");
                        continue;
                    }

                    s.DestRect = FindEmptyPoint(atlas, s.DestRect);
                    if (s.DestRect.X < 0)
                    {
                        Console.WriteLine(s.Name + " " + s.ColoredRect + " not fit in atlas");
                        continue;
                    }
                    g.DrawImage(s.Bitmap, s.DestRect, s.ColoredRect.X, s.ColoredRect.Y, s.ColoredRect.Width, s.ColoredRect.Height, GraphicsUnit.Pixel);
                    i++;
                    if (i > atlasLen * 0.1 * d)
                    {
                        Console.Write(".");
                        d++;
                    }
                }
            }

            /*var kk = passeds.Keys;
             * foreach (string k in kk)
             *  Console.Write(k + ":" + passeds[k] + " ");*/

            return(ret);
        }
Beispiel #2
0
        static public string Serialize(Atlas atlas, String name)
        {
            StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF - 8\"?>\n<TextureAtlas imagePath=\"" + name + "\" width=\"" + atlas.Width + "\" height=\"" + atlas.Height + "\">");

            foreach (var s in atlas)
            {
                sb.Append("\n\t<SubTexture name=\"");
                sb.Append(s.Name);
                sb.Append("\" x=\"");
                sb.Append(s.DestRect.X);
                sb.Append("\" y=\"");
                sb.Append(s.DestRect.Y);
                sb.Append("\" width=\"");
                sb.Append(s.DestRect.Width);
                sb.Append("\" height=\"");
                sb.Append(s.DestRect.Height);

                if (s.DestRect.Width != s.Dimentions.X)
                {
                    sb.Append("\" frameX=\"");
                    sb.Append(-(int)Math.Round(s.ColoredRect.X * atlas.Scale));
                    sb.Append("\" frameY=\"");
                    sb.Append(-(int)Math.Round(s.ColoredRect.Y * atlas.Scale));
                    sb.Append("\" frameWidth=\"");
                    sb.Append(s.Dimentions.X);
                    sb.Append("\" frameHeight=\"");
                    sb.Append(s.Dimentions.Y);
                }

                sb.Append("\"/>");
            }


            sb.Append("\n</TextureAtlas>");
            return(sb.ToString());
        }
Beispiel #3
0
 static public void Sort(Atlas atlas)
 {
     atlas.Sort(Comparison);
 }
Beispiel #4
0
        static void Main(string[] args)
        {
            float  _scale            = 1;
            int    _width            = 1024;
            int    _height           = 1024;
            string _atlasImage       = "atlas.png";
            string _atlasData        = "atlas.xml";
            string _source           = "images";
            string _prefix           = "";
            bool   _trim             = true;
            int    _packing          = 1;
            bool   _removeDuplicates = true;

            for (var i = 0; i < args.Length; i++)
            {
                if (args[i].Substring(0, 1) != "-")
                {
                    continue;
                }
                switch (args[i])
                {
                case "-atlas":  _atlasImage = args[i + 1]; break;

                case "-prefix": _prefix = args[i + 1]; break;

                case "-data":   _atlasData = args[i + 1]; break;

                case "-source": _source = args[i + 1]; break;

                case "-width":  _width = int.Parse(args[i + 1]); break;

                case "-height": _height = int.Parse(args[i + 1]); break;

                case "-scale":  _scale = float.Parse(args[i + 1]); break;

                case "-trim":   _trim = bool.Parse(args[i + 1]); break;

                case "-packing": _packing = int.Parse(args[i + 1]); break;

                case "-removeDuplicates": _removeDuplicates = bool.Parse(args[i + 1]); break;
                }
            }

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            var imgs  = Directory.GetFiles(_source, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".png") || s.EndsWith(".PNG") || s.EndsWith(".tif") || s.EndsWith(".TIF")).ToList();
            var atlas = new Atlas(_width, _height, _scale);

            Console.WriteLine("loading images ...");
            var folderNameLen = _source.Length + 1;

            foreach (var i in imgs)
            {
                atlas.Add(item: new Slice(i, _prefix, folderNameLen, _trim, atlas.Scale));
            }

            Console.WriteLine("subtextures created " + stopwatch.ElapsedMilliseconds + " ms.");
            Engine.Sort(atlas);

            Console.WriteLine("packing started ");
            Bitmap atlasBMP = Engine.Pack(atlas, _removeDuplicates, _packing);

            Console.WriteLine(" in: " + stopwatch.ElapsedMilliseconds + " ms.");
            atlasBMP.Save(_atlasImage);

            File.WriteAllText(_atlasData, Engine.Serialize(atlas, _atlasImage));

            Console.WriteLine("finished.");
//            Console.ReadKey();
        }