Example #1
0
		public static int PackSprites(Config config)
		{
			// compile a list of images
			var animations = new Dictionary<string, List<string>>();
			var images = new List<string>();
			FindImages(config, images, animations);

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

			// make sure no images have the same name or path if we're building a map
			if ((!config.WritePaths && CheckDuplicateImagesNames(images))
				|| (config.WritePaths && CheckDuplicateImagesPaths(images)))
			{
				return (int)FailCode.ImageNameCollision;
			}

			// generate our output
			var imagePacker = new ImagePacker();

			// pack the image, generating a map only if desired
			int result = imagePacker.PackImage(images, config.IsPowerOfTwo, config.IsSquare, config.AtlasMaxWidth, config.AtlasMaxHeight, config.Padding, out Bitmap outputImage, out Dictionary<string, Rectangle> outputMap);
			if (result != 0)
			{
				System.Console.WriteLine("There was an error making the image sheet.");
				return result;
			}

			// try to save using our exporters
			if (File.Exists(config.AtlasOutputFile))
				File.Delete(config.AtlasOutputFile);

			var imageExtension = Path.GetExtension(config.AtlasOutputFile).Substring(1).ToLower();
			switch (imageExtension)
			{
				case "png":
					outputImage.Save(config.AtlasOutputFile, ImageFormat.Png);
					break;
				case "jpg":
					outputImage.Save(config.AtlasOutputFile, ImageFormat.Jpeg);
					break;
				case "bmp":
					outputImage.Save(config.AtlasOutputFile, ImageFormat.Bmp);
					break;
				default:
					throw new Exception("Invalid image format for output image.");
			}

			// load origins if atlas file is passed in config
			Dictionary<string, System.Numerics.Vector2> origins = null;

			if (File.Exists(config.MapInputFile))
				origins = AtlasMapOriginsImporter.ImportFromFile(config.MapInputFile);
			else
				System.Console.WriteLine("Input map file does not exist, using default origins.");

			if (config.OutputLua)
				config.MapOutputFile = config.MapOutputFile.Replace(".atlas", ".lua");

			if (File.Exists(config.MapOutputFile))
				File.Delete(config.MapOutputFile);

			if (config.OutputLua)
				LuaMapExporter.Save(config.MapOutputFile, outputMap, animations, outputImage.Width, outputImage.Height, config);
			else
				AtlasMapExporter.Save(config.MapOutputFile, outputMap, animations, origins, config);

			return 0;
		}
Example #2
0
        public static int PackSprites(Config config)
        {
            // compile a list of images
            var animations = new Dictionary <string, List <string> >();
            var images     = new List <string>();

            FindImages(config, images, animations);

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

            // make sure no images have the same name if we're building a map
            for (int i = 0; i < images.Count; i++)
            {
                var str1 = Path.GetFileNameWithoutExtension(images[i]);
                for (int j = i + 1; j < images.Count; j++)
                {
                    var str2 = Path.GetFileNameWithoutExtension(images[j]);
                    if (str1 == str2)
                    {
                        System.Console.WriteLine("Two images have the same name: {0} = {1}", images[i], images[j]);
                        return((int)FailCode.ImageNameCollision);
                    }
                }
            }

            // generate our output
            var imagePacker = new ImagePacker();

            // pack the image, generating a map only if desired
            int result = imagePacker.PackImage(images, config.IsPowerOfTwo, config.IsSquare, config.AtlasMaxWidth, config.AtlasMaxHeight, config.Padding, out Bitmap outputImage, out Dictionary <string, Rectangle> outputMap);

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

            // try to save using our exporters
            if (File.Exists(config.AtlasOutputFile))
            {
                File.Delete(config.AtlasOutputFile);
            }

            var imageExtension = Path.GetExtension(config.AtlasOutputFile).Substring(1).ToLower();

            switch (imageExtension)
            {
            case "png":
                outputImage.Save(config.AtlasOutputFile, ImageFormat.Png);
                break;

            case "jpg":
                outputImage.Save(config.AtlasOutputFile, ImageFormat.Jpeg);
                break;

            case "bmp":
                outputImage.Save(config.AtlasOutputFile, ImageFormat.Bmp);
                break;

            default:
                throw new Exception("Invalid image format for output image");
            }

            if (config.OutputLua)
            {
                config.MapOutputFile = config.MapOutputFile.Replace(".atlas", ".lua");
            }

            if (File.Exists(config.MapOutputFile))
            {
                File.Delete(config.MapOutputFile);
            }

            if (config.OutputLua)
            {
                LuaMapExporter.Save(config.MapOutputFile, outputMap, animations, outputImage.Width, outputImage.Height, config);
            }
            else
            {
                AtlasMapExporter.Save(config.MapOutputFile, outputMap, animations, config);
            }

            return(0);
        }