Example #1
0
        static int Main(string[] args)
        {
            String directory = @"..\Resources\";

            if (args.Length == 1)
            {
                directory = args[0];
            }
            else if (args.Length != 0)
            {
                return(Launch(args));
            }

            Console.Clear();
            Helper.Write("SpriteSheet Creator makes sprite sheets from directory.", ConsoleColor.Yellow);
            Helper.Write("v.1.0 - @mihailogazda", ConsoleColor.Yellow);
            Helper.Write("Call with /? for help", ConsoleColor.Magenta);

            arguments = new ProgramArguments();
            PNGFiles  = Helper.GetPNGFilesInDirectory(directory);

            Helper.WriteSUCCESS("\r\nFound files counted: " + PNGFiles.Count);

            Bitmap outputImage;
            Dictionary <string, Rectangle> outputMap;

            //  Pack all in one - try at least
            ImagePacker p      = new ImagePacker();
            int         status = p.PackImage(PNGFiles, pow2, sqrt, maxWidth, maxHeight, split, true, out outputImage, out outputMap);

            if (status == 0)
            {
                //  Success
                Cocos2DMapExporter cm = new Cocos2DMapExporter();
                PngImageExporter   ie = new PngImageExporter();

                cm.Save(outMap, outputMap);
                ie.Save(outImage, outputImage);

                Helper.WriteSUCCESS("Saved " + outImage + " from first try!");
            }
            else
            {
                Helper.Write("\r\nCannot pack in single try.", ConsoleColor.Yellow);

                //  split in two and try that way
                Partitioner part = new Partitioner();
                part.ExportHalf(PNGFiles);

                if (part.success())
                {
                    Helper.WriteSUCCESS("All partitions written.");
                }
                else
                {
                    Helper.WriteError("Not all partitions written.");
                }
            }
Example #2
0
        public SpriteSheetInfo[] ExtractSpriteSheet(uint[] index, string path)
        {
            List<System.Drawing.Bitmap> bitmaps = new List<System.Drawing.Bitmap>();
            List<KeyValuePair<string, System.Drawing.Bitmap>> mapperInput = new List<KeyValuePair<string, System.Drawing.Bitmap>>();
            List<SpriteSheetInfo> retlist = new List<SpriteSheetInfo>();

            int offset = 0;
            for (int i = 0; i < index.Length; i++)
            {
                SpriteSheetInfo ssi = new SpriteSheetInfo();
                ssi.imageID = index[i];
                var subimg = ExtractOne(index[i], ref ssi.offsetX, ref ssi.offsetY);

                retlist.Add(ssi);

                if (subimg != null)
                {
                    bitmaps.Add(subimg);
                    mapperInput.Add(new KeyValuePair<string, System.Drawing.Bitmap>(index[i].ToString(), subimg));
                    //imageinfos.Add (new ImageInfo(bitmaps[offset].Width, bitmaps[offset].Height, offset));
                    offset++;
                }
            }

            if (bitmaps.Count == 0)
                return null;

            ImagePacker imagePacker = new ImagePacker();
            Bitmap outputImage;
            Lexicon<string, Rectangle> outputMap;
            List<uint> trash;

            // pack the image, generating a map only if desired
            imagePacker.PackImage(mapperInput, true, true, 2048, 2048, 1, true, out outputImage, out outputMap, out trash);

            foreach (KeyValuePair<string, Rectangle> mii in outputMap)
            {
                uint imgid = uint.Parse(mii.Key);

                var ssi = retlist.Find(x => x.imageID == imgid);

                ssi.X = mii.Value.X;
                ssi.Y = mii.Value.Y;
                ssi.width = mii.Value.Width;
                ssi.height = mii.Value.Height;
            }

            outputImage.Save(path);

            return retlist.ToArray();
        }
Example #3
0
        //  Recoursive method
        public bool ExportHalf(List <string> files)
        {
            if (files.Count == 0)
            {
                Helper.Write("SKIPPING EMPTY PARTITION");
                return(false);
            }

            filesInitiated += files.Count;

            Helper.Write("\r\nStarting new partition (" + filesCompleted + " / " + filesInitiated + "): " + files.Count);

            int           half  = files.Count / 2;
            List <string> left  = new List <string>();
            List <string> right = new List <string>();

            int ind = 0;

            foreach (String file in files)
            {
                if (ind <= half)
                {
                    left.Add(file);
                }
                else
                {
                    right.Add(file);
                }
                ++ind;
            }

            Bitmap outImage;
            Dictionary <string, Rectangle> outMap;

            ImagePacker p      = new ImagePacker();
            bool        okLeft = p.PackImage(left, Program.pow2, Program.sqrt, Program.maxHeight, Program.maxHeight, Program.split, true, out outImage, out outMap) == 0;

            if (okLeft)
            {
                filesCompleted += left.Count;

                Save(outImage, outMap);
                Helper.WriteSUCCESS("Partition done: " + left.Count + " (LEFT)");
            }
            else
            {
                ExportHalf(left);
            }

            //p = new ImagePacker();
            bool okRight = p.PackImage(right, Program.pow2, Program.sqrt, Program.maxHeight, Program.maxHeight, Program.split, true, out outImage, out outMap) == 0;

            if (okRight)
            {
                filesCompleted += right.Count;
                Save(outImage, outMap);

                Helper.WriteSUCCESS("Partition done: " + right.Count + " (RIGHT)");
            }
            else
            {
                ExportHalf(right);
            }

            return(true);
        }
Example #4
0
		public static FailCode Launch(string[] args)
		{
			ProgramArguments arguments = ProgramArguments.Parse(args);

			if (arguments == null)
				return FailCode.FailedParsingArguments;
			// make sure we have our list of exporters
			Exporters.Load();

			// try to find matching exporters
			IImageExporter imageExporter = null;
			IMapExporter mapExporter = null;

			string imageExtension = Path.GetExtension(arguments.image).Substring(1).ToLower();
			foreach (var exporter in Exporters.ImageExporters)
			{
				if (exporter.ImageExtension.ToLower() == imageExtension)
				{
					imageExporter = exporter;
					break;
				}
			}

			if (imageExporter == null)
			{
				Console.WriteLine("Failed to find exporters for specified image type.");
				return FailCode.ImageExporter;
			}

			if (!string.IsNullOrEmpty(arguments.map))
			{
				string mapExtension = Path.GetExtension(arguments.map).Substring(1).ToLower();
				foreach (var exporter in Exporters.MapExporters)
				{
					if (exporter.MapExtension.ToLower() == mapExtension)
					{
						mapExporter = exporter;
						break;
					}
				}

				if (mapExporter == null)
				{
					Console.WriteLine("Failed to find exporters for specified map type.");
					return FailCode.MapExporter;
				}
			}

			// compile a list of images
			List<string> images = new List<string>();
			FindImages(arguments, images);

			// make sure we found some images
			if (images.Count == 0)
			{
				Console.WriteLine("No images to pack.");
				return FailCode.NoImages;
			}

			// make sure no images have the same name if we're building a map
			if (mapExporter != null)
			{
				Dictionary<string, string> usedFileNames = new Dictionary<string, string>();

				for (int i = 0; i < images.Count; i++)
				{
					string packedFilename = Constants.PackedFilename(images[i]);

					if (usedFileNames.ContainsKey(packedFilename))
					{
						Console.WriteLine("Two images have the same name: {0} = {1}", images[i], usedFileNames[packedFilename]);
						return FailCode.ImageNameCollision;
					}
					usedFileNames.Add(packedFilename, images[i]);
				}
			}

			// generate our output
			ImagePacker imagePacker = new ImagePacker();
			Bitmap outputImage;
			Dictionary<string, Rectangle> outputMap;

			// pack the image, generating a map only if desired
			FailCode result = imagePacker.PackImage(images, arguments.pow2, arguments.sqr, arguments.mw, arguments.mh, arguments.pad, mapExporter != null, out outputImage, out outputMap);
			if (result != FailCode.Success)
			{
				Console.WriteLine("There was an error making the image sheet: " + result);
				return result;
			}

			// try to save using our exporters
			try
			{
				if (File.Exists(arguments.image))
					File.Delete(arguments.image);
				imageExporter.Save(arguments.image, outputImage);
			}
			catch (Exception e)
			{
				Console.WriteLine("Error saving file: " + e.Message);
				return FailCode.FailedToSaveImage;
			}

			if (mapExporter != null)
			{
				try
				{
					if (File.Exists(arguments.map))
						File.Delete(arguments.map);
					mapExporter.Save(arguments.map, outputMap, arguments.image);
				}
				catch (Exception e)
				{
					Console.WriteLine("Error saving file: " + e.Message);
					return FailCode.FailedToSaveMap;
				}
			}

			return 0;
		}
Example #5
0
        public static int Launch(string[] args)
        {
            ProgramArguments arguments = ProgramArguments.Parse(args);

            if (arguments == null)
            {
                return((int)FailCode.FailedParsingArguments);
            }
            else
            {
                // make sure we have our list of exporters
                Exporters.Load();

                // try to find matching exporters
                IImageExporter imageExporter = null;
                IMapExporter   mapExporter   = null;

                string imageExtension = Path.GetExtension(arguments.image).Substring(1).ToLower();
                foreach (var exporter in Exporters.ImageExporters)
                {
                    if (exporter.ImageExtension.ToLower() == imageExtension)
                    {
                        imageExporter = exporter;
                        break;
                    }
                }

                if (imageExporter == null)
                {
                    Console.WriteLine("Failed to find exporters for specified image type.");
                    return((int)FailCode.ImageExporter);
                }

                if (!string.IsNullOrEmpty(arguments.map))
                {
                    string mapExtension = Path.GetExtension(arguments.map).Substring(1).ToLower();
                    foreach (var exporter in Exporters.MapExporters)
                    {
                        if (exporter.MapExtension.ToLower() == mapExtension)
                        {
                            mapExporter = exporter;
                            break;
                        }
                    }

                    if (mapExporter == null)
                    {
                        Console.WriteLine("Failed to find exporters for specified map type.");
                        return((int)FailCode.MapExporter);
                    }
                }

                // compile a list of images
                List <string> images = new List <string>();
                FindImages(arguments, images);

                // make sure we found some images
                if (images.Count == 0)
                {
                    Console.WriteLine("No images to pack.");
                    return((int)FailCode.NoImages);
                }

                // make sure no images have the same name if we're building a map
                if (mapExporter != null)
                {
                    for (int i = 0; i < images.Count; i++)
                    {
                        string str1 = Path.GetFileNameWithoutExtension(images[i]);

                        for (int j = i + 1; j < images.Count; j++)
                        {
                            string str2 = Path.GetFileNameWithoutExtension(images[j]);

                            if (str1 == str2)
                            {
                                Console.WriteLine("Two images have the same name: {0} = {1}", images[i], images[j]);
                                return((int)FailCode.ImageNameCollision);
                            }
                        }
                    }
                }

                // generate our output
                ImagePacker imagePacker = new ImagePacker();
                Bitmap      outputImage;
                Dictionary <string, Rectangle> outputMap;

                // pack the image, generating a map only if desired
                int result = imagePacker.PackImage(images, arguments.pow2, arguments.sqr, arguments.mw, arguments.mh, arguments.pad, mapExporter != null, out outputImage, out outputMap);
                if (result != 0)
                {
                    Console.WriteLine("There was an error making the image sheet.");
                    return(result);
                }

                // try to save using our exporters
                try
                {
                    if (File.Exists(arguments.image))
                    {
                        File.Delete(arguments.image);
                    }
                    imageExporter.Save(arguments.image, outputImage);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error saving file: " + e.Message);
                    return((int)FailCode.FailedToSaveImage);
                }

                if (mapExporter != null)
                {
                    try
                    {
                        if (File.Exists(arguments.map))
                        {
                            File.Delete(arguments.map);
                        }
                        mapExporter.Save(arguments.map, outputMap);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error saving file: " + e.Message);
                        return((int)FailCode.FailedToSaveMap);
                    }
                }
            }

            return(0);
        }
Example #6
0
        void PackOnce()
        {
            if (mPackTasks.Count == 0) return;
            // generate our output
            ImagePacker imagePacker = new ImagePacker();
            Bitmap outputImage;
            Dictionary<string, System.Drawing.Rectangle> outputMap;
            PackTask pt = mPackTasks.Dequeue();
            Console.WriteLine("Packing {0} ({1} left to pack)", pt.outputFile, mPackTasks.Count);

            // pack the image, generating a map only if desired
            int result = imagePacker.PackImage(pt.inputFiles, REQUIRE_POW2, REQUIRE_SQUARE, 4096, 4096, PADDING, true, out outputImage, out outputMap);
            if (result != 0)
            {
                Console.WriteLine("There was an error making the image sheet.");
                return;
            }

            // try to save using our exporters
            try
            {
                if (File.Exists(pt.outputFile))
                    File.Delete(pt.outputFile);
                mImageExporter.Save(pt.outputFile, outputImage);
                Console.WriteLine("Saved atlas {0}.", pt.outputFile);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error saving file: " + e.Message);
                return;
            }

            if (mMapExporter != null)
            {
                try
                {
                    if (File.Exists(pt.outputMap))
                        File.Delete(pt.outputMap);
                    mMapExporter.Save(pt.outputMap, outputMap);
                    Console.WriteLine("Saved atlas map {0}.", pt.outputMap);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error saving file: " + e.Message);
                    return;
                }
            }
            foreach (string filename in pt.inputFiles)
            {
                if (File.Exists(filename))
                {
                    try
                    {
                        File.Delete(filename);
                    }
                    catch (IOException)
                    {
                        // Welp
                    }
                }
            }
        }
Example #7
0
        static void ExportWalls()
        {
            string last = "";
            for (int i = 0; i < thingdb.Walls.Count; i++)
            {
                ThingDb.Wall wall = thingdb.Walls[i];

                if (wall.Name == last)
                    continue;

                last = wall.Name;
                string assetPath = "Sprites/walls/" + wall.Name + ".png";

                /*if (AssetImporter.GetAtPath(assetPath) != null)
                    continue;*/

                //VideoBag.SpriteSheetInfo[] inf = bag.ExtractSpriteSheet(ttile.Variations.ToArray(), assetPath);

                HashSet<uint> indexl = new HashSet<uint>();
                foreach (ThingDb.Wall.WallRenderInfo[] wriarray in wall.RenderBreakable)
                {
                    foreach (ThingDb.Wall.WallRenderInfo wri in wriarray)
                    {
                        indexl.Add((uint)wri.SpriteIndex);
                    }
                }

                foreach (ThingDb.Wall.WallRenderInfo[] wriarray in wall.RenderNormal)
                {
                    foreach (ThingDb.Wall.WallRenderInfo wri in wriarray)
                    {
                        indexl.Add((uint)wri.SpriteIndex);
                    }
                }

                uint[] index = new uint[indexl.Count];
                indexl.CopyTo(index);

                int instance = 1;
                do
                {
                    List<System.Drawing.Bitmap> bitmaps = new List<System.Drawing.Bitmap>();
                    List<KeyValuePair<string, System.Drawing.Bitmap>> mapperInput = new List<KeyValuePair<string, System.Drawing.Bitmap>>();
                    List<VideoBag.SpriteSheetInfo> retlist = new List<VideoBag.SpriteSheetInfo>();

                    int offset = 0;
                    for (int j = 0; j < index.Length; j++)
                    {
                        VideoBag.SpriteSheetInfo ssi = new VideoBag.SpriteSheetInfo();
                        ssi.imageID = index[j];
                        ssi.file = assetPath;
                        var subimg = bag.ExtractOne(index[j], ref ssi.offsetX, ref ssi.offsetY);

                        retlist.Add(ssi);

                        if (subimg != null)
                        {
                            bitmaps.Add(subimg);
                            mapperInput.Add(new KeyValuePair<string, System.Drawing.Bitmap>(index[j].ToString(), subimg));
                            //imageinfos.Add (new ImageInfo(bitmaps[offset].Width, bitmaps[offset].Height, offset));
                            offset++;
                        }
                    }

                    if (bitmaps.Count == 0)
                        break;

                    // pack the image, generating a map only if desired

                    ImagePacker imagePacker = new ImagePacker();
                    Bitmap outputImage;
                    Lexicon<string, Rectangle> outputMap;
                    List<uint> failedPlacements;

                    FailCode returnState = (FailCode)imagePacker.PackImage(mapperInput, true, true, 2048, 2048, 1, true, out outputImage, out outputMap, out failedPlacements);

                    foreach (KeyValuePair<string, Rectangle> mii in outputMap)
                    {
                        uint imgid = uint.Parse(mii.Key);

                        var ssi = retlist.Find(x => x.imageID == imgid);

                        ssi.X = mii.Value.X;
                        ssi.Y = mii.Value.Y;
                        ssi.width = mii.Value.Width;
                        ssi.height = mii.Value.Height;

                        ssilist.Add(ssi.imageID, ssi);
                    }

                    outputImage.Save(assetPath);

                    if (failedPlacements.Count == 0)
                        break;
                    else
                    {
                        index = failedPlacements.ToArray();
                        assetPath = "Sprites/walls/" + wall.Name + "-" + instance + ".png";
                        instance++;
                    }

                } while (true);
            }
        }
Example #8
0
        static void ExportSequences()
        {
            string last = "";
            foreach (KeyValuePair<string, ThingDb.Thing> kvpair in thingdb.Things)
            {
                ThingDb.Thing thing = kvpair.Value;

                if (thing.Name == last)
                    continue;

                last = thing.Name;

                if (thing.SpriteStates.Count < 1)
                    continue;

                if (thing.SpriteStates[0].Animation.Sequences.Count < 1)
                    continue;

                Dictionary<string, HashSet<uint>> spritedict = new Dictionary<string, HashSet<uint>>();

                foreach (ThingDb.Image.State ss in thing.SpriteStates)
                {
                    foreach (ThingDb.Image.Animation.Sequence seq in ss.Animation.Sequences)
                    {
                        foreach (int i in seq.Frames)
                        {
                            HashSet<uint> hs = null;
                            spritedict.TryGetValue(seq.Name, out hs);
                            if (hs == null)
                            {
                                hs = new HashSet<uint>();
                                hs.Add((uint)i);
                                spritedict.Add(seq.Name, hs);
                            }
                            else
                            {
                                hs.Add((uint)i);
                            }
                        }
                    }
                }

                string basePath = "";
                if (spritedict.Count > 0)
                {
                    basePath = "Sprites/sequences/" + thing.Name;
                    Directory.CreateDirectory(basePath);
                }

                foreach (KeyValuePair<string, HashSet<uint>> kv in spritedict)
                {
                    uint[] index = new uint[kv.Value.Count];
                    kv.Value.CopyTo(index);
                    string assetPath = basePath + "/" + kv.Key + ".png";

                    int instance = 1;
                    do
                    {
                        List<System.Drawing.Bitmap> bitmaps = new List<System.Drawing.Bitmap>();
                        List<KeyValuePair<string, System.Drawing.Bitmap>> mapperInput = new List<KeyValuePair<string, System.Drawing.Bitmap>>();
                        List<VideoBag.SpriteSheetInfo> retlist = new List<VideoBag.SpriteSheetInfo>();

                        int offset = 0;
                        for (int j = 0; j < index.Length; j++)
                        {
                            VideoBag.SpriteSheetInfo ssi = new VideoBag.SpriteSheetInfo();
                            ssi.imageID = index[j];
                            ssi.file = assetPath;
                            var subimg = bag.ExtractOne(index[j], ref ssi.offsetX, ref ssi.offsetY);

                            retlist.Add(ssi);

                            if (subimg != null)
                            {
                                bitmaps.Add(subimg);
                                mapperInput.Add(new KeyValuePair<string, System.Drawing.Bitmap>(index[j].ToString(), subimg));
                                //imageinfos.Add (new ImageInfo(bitmaps[offset].Width, bitmaps[offset].Height, offset));
                                offset++;
                            }
                        }

                        if (bitmaps.Count == 0)
                        {
                            //Console.WriteLine("Failed to export: " + thing.Name);
                            break;
                        }

                        // pack the image, generating a map only if desired

                        ImagePacker imagePacker = new ImagePacker();
                        Bitmap outputImage;
                        Lexicon<string, Rectangle> outputMap;
                        List<uint> failedPlacements;

                        FailCode returnState = (FailCode)imagePacker.PackImage(mapperInput, true, true, 2048, 2048, 1, true, out outputImage, out outputMap, out failedPlacements);

                        if (returnState != 0)
                        {
                            Console.WriteLine("Failed to export: " + thing.Name + ":" + kv.Value);
                            break;
                        }

                        foreach (KeyValuePair<string, Rectangle> mii in outputMap)
                        {
                            uint imgid = uint.Parse(mii.Key);

                            var ssi = retlist.Find(x => x.imageID == imgid);

                            ssi.X = mii.Value.X;
                            ssi.Y = mii.Value.Y;
                            ssi.width = mii.Value.Width;
                            ssi.height = mii.Value.Height;

                            ssilist.Add(ssi.imageID, ssi);
                        }

                        outputImage.Save(assetPath);

                        if (failedPlacements.Count == 0)
                            break;
                        else
                        {
                            index = failedPlacements.ToArray();
                            //assetPath = "Sprites/objects/" + thing.Name + "-" + instance + ".png";
                            assetPath = basePath + "/" + kv.Key + "-" + instance + ".png";
                            instance++;
                        }

                    } while (true);
                }
            }
        }
Example #9
0
        static void ExportObjects()
        {
            string last = "";
            foreach (KeyValuePair<string, ThingDb.Thing> kvpair in thingdb.Things)
            {
                ThingDb.Thing thing = kvpair.Value;

                if (thing.Name == last)
                    continue;

                last = thing.Name;
                string assetPath = "Sprites/objects/" + thing.Name + ".png";

                if (File.Exists(assetPath))
                    continue;

                /*if (AssetImporter.GetAtPath(assetPath) != null)
                    continue;*/

                //VideoBag.SpriteSheetInfo[] inf = bag.ExtractSpriteSheet(ttile.Variations.ToArray(), assetPath);

                HashSet<uint> sprites = new HashSet<uint>();
                foreach (int j in thing.SpriteAnimFrames)
                {
                    sprites.Add((uint)j);
                }

                foreach (ThingDb.Image.State ss in thing.SpriteStates)
                {
                    foreach (int j in ss.Animation.Frames)
                    {
                        sprites.Add((uint)j);
                    }

                }

                foreach (ThingDb.Image.Animation a in thing.ConditionalAnimations)
                {
                    foreach (int j in a.Frames)
                    {
                        sprites.Add((uint)j);
                    }
                }

                if (sprites.Count < 1)
                    continue;

                uint[] index = new uint[sprites.Count];
                sprites.CopyTo(index);

                int instance = 1;
                do
                {
                    List<System.Drawing.Bitmap> bitmaps = new List<System.Drawing.Bitmap>();
                    List<KeyValuePair<string, System.Drawing.Bitmap>> mapperInput = new List<KeyValuePair<string, System.Drawing.Bitmap>>();
                    List<VideoBag.SpriteSheetInfo> retlist = new List<VideoBag.SpriteSheetInfo>();

                    int offset = 0;
                    for (int j = 0; j < index.Length; j++)
                    {
                        VideoBag.SpriteSheetInfo ssi = new VideoBag.SpriteSheetInfo();
                        ssi.imageID = index[j];
                        ssi.file = assetPath;
                        var subimg = bag.ExtractOne(index[j], ref ssi.offsetX, ref ssi.offsetY);

                        retlist.Add(ssi);

                        if (subimg != null)
                        {
                            bitmaps.Add(subimg);
                            mapperInput.Add(new KeyValuePair<string, System.Drawing.Bitmap>(index[j].ToString(), subimg));
                            //imageinfos.Add (new ImageInfo(bitmaps[offset].Width, bitmaps[offset].Height, offset));
                            offset++;
                        }
                    }

                    if (bitmaps.Count == 0)
                    {
                        Console.WriteLine("Failed to export: " + thing.Name);
                        break;
                    }

                    // pack the image, generating a map only if desired

                    ImagePacker imagePacker = new ImagePacker();
                    Bitmap outputImage;
                    Lexicon<string, Rectangle> outputMap;
                    List<uint> failedPlacements;

                    FailCode returnState = (FailCode)imagePacker.PackImage(mapperInput, false, false, 2048, 2048, 1, true, out outputImage, out outputMap, out failedPlacements);

                    if (returnState != 0)
                    {
                        Console.WriteLine("Failed to export: " + thing.Name);
                        break;
                    }

                    foreach (KeyValuePair<string, Rectangle> mii in outputMap)
                    {
                        uint imgid = uint.Parse(mii.Key);

                        var ssi = retlist.Find(x => x.imageID == imgid);

                        ssi.X = mii.Value.X;
                        ssi.Y = mii.Value.Y;
                        ssi.width = mii.Value.Width;
                        ssi.height = mii.Value.Height;

                        ssilist.Add(ssi.imageID, ssi);
                    }

                    outputImage.Save(assetPath);

                    if (failedPlacements.Count == 0)
                        break;
                    else
                    {
                        index = failedPlacements.ToArray();
                        assetPath = "Sprites/objects/" + thing.Name + "-" + instance + ".png";
                        instance++;
                    }

                } while (true);
            }
        }
Example #10
0
        public static void Launch(string dir)
        {
            // make sure we have our list of exporters
            Exporters.Exporters.Load();

            // try to find matching exporters
            IImageExporter imageExporter = null;
            IMapExporter   mapExporter   = null;

            string imageExtension = "png";

            foreach (var exporter in Exporters.Exporters.ImageExporters)
            {
                if (exporter.ImageExtension.ToLower() == imageExtension)
                {
                    imageExporter = exporter;
                    break;
                }
            }

            string mapExtension = "txt";

            foreach (var exporter in Exporters.Exporters.MapExporters)
            {
                if (exporter.MapExtension.ToLower() == mapExtension)
                {
                    mapExporter = exporter;
                    break;
                }
            }

            // compile a list of images
            List <string> images         = new List <string>();
            List <string> images_lowres  = new List <string>();
            List <string> images_highres = new List <string>();

            foreach (string str in Directory.GetFiles(dir, "*.png"))
            {
                if (str.EndsWith(".small.png"))
                {
                    images_lowres.Add(str.Substring(str.LastIndexOf(@"\") + 1).Replace(".small.png", ""));
                }
                else
                {
                    images.Add(str);
                }
            }

            if (Directory.Exists(dir + "_huge"))
            {
                foreach (string str in Directory.GetFiles(dir + "_huge", "*.png"))
                {
                    images_highres.Add(str.Substring(str.LastIndexOf(@"\") + 1).Replace(".png", ""));
                }
            }

            // generate our output
            ImagePacker imagePacker = new ImagePacker();
            Bitmap      outputImage;
            Dictionary <string, Rectangle> outputMap;

            // pack the image, generating a map only if desired
            int result = imagePacker.PackImage(images, true, false, 2048, 2048, 2, true, out outputImage, out outputMap);

            string sheetName = dir.Substring(dir.LastIndexOf(@"/") + 1);

            Bitmap   bmpLowres = new Bitmap(outputImage, new Size(outputImage.Width / 2, outputImage.Height / 2));
            Graphics gfxLowres = Graphics.FromImage(bmpLowres);

            gfxLowres.CompositingMode = CompositingMode.SourceCopy;

            Bitmap   bmpHighres = new Bitmap(outputImage, new Size(outputImage.Width * 2, outputImage.Height * 2));
            Graphics gfxHighres = Graphics.FromImage(bmpHighres);

            gfxHighres.CompositingMode = CompositingMode.SourceCopy;

            foreach (var m in outputMap)
            {
                if (m.Value.Width % 2 != 0)
                {
                    Console.WriteLine("FATAL: width of " + m.Key + " is not div 2");
                }
                if (m.Value.Height % 2 != 0)
                {
                    Console.WriteLine("FATAL: width of " + m.Key + " is not div 2");
                }

                string spriteName = m.Key.Substring(m.Key.LastIndexOf(@"\") + 1).Replace(".png", "");

                if (images_lowres.Contains(spriteName))
                {
                    Bitmap spriteLowres = Bitmap.FromFile(m.Key.Replace(".png", ".small.png")) as Bitmap;
                    gfxLowres.DrawImageUnscaledAndClipped(spriteLowres, new Rectangle(m.Value.X / 2, m.Value.Y / 2, m.Value.Width / 2, m.Value.Height / 2));
                }

                Brush transparentBrush = new SolidBrush(Color.Transparent);

                if (images_highres.Contains(spriteName))
                {
                    Bitmap spriteHighres = Bitmap.FromFile(dir + "_huge\\" + spriteName + ".png") as Bitmap;

                    if (spriteHighres.Width != m.Value.Width * 2 || spriteHighres.Height != m.Value.Height * 2)
                    {
                        Console.WriteLine("FATAL: dimensions of high res sprite " + m.Key + " do not match!");
                    }

                    // wipe away any sprite gunk that bled into the padding region from the upscale.
                    gfxHighres.FillRectangle(transparentBrush, new Rectangle(m.Value.X * 2 - 4, m.Value.Y * 2 - 4, m.Value.Width * 2 + 8, m.Value.Height * 2 + 8));
                    gfxHighres.DrawImageUnscaledAndClipped(spriteHighres, new Rectangle(m.Value.X * 2, m.Value.Y * 2, m.Value.Width * 2, m.Value.Height * 2));
                }

                sb.AppendFormat("            textureLocations.Add(OsuTexture.{0}, new SpriteSheetTexture(\"{1}\", {2}, {3}, {4}, {5}));\r\n",
                                spriteName, sheetName, m.Value.Left, m.Value.Top, m.Value.Width, m.Value.Height);
            }

            if (result != 0)
            {
                Console.WriteLine("There was an error making the image sheet for " + dir);
                return;
            }

            if (File.Exists(dir))
            {
                File.Delete(dir);
            }

            imageExporter.Save(dir + "_960.png", outputImage);
            bmpLowres.Save(dir + "_480.png", ImageFormat.Png);
            bmpHighres.Save(dir + "_1920.png", ImageFormat.Png);
        }