Example #1
0
        public static int PackSprites(Config config)
        {
            //original config holds the input paths at minimum
            //search those for an existing .config and replace if exists
            config = LoadConfig(config);

            // compile a list of images
            var animations = new Dictionary <string, List <string> >();
            var images     = new List <string>();

            //FindImages(config, images, animations);
            int error = FindImages(config, images);

            if (error != 0)
            {
                return(error);
            }


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

            if (config.CreateAnimations)
            {
                error = CreateAnimations(config, images, animations);
            }
            if (error != 0)
            {
                return(error);
            }

            // 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);
            }
            //else
            //AtlasMapExporter.Save(config.MapOutputFile, outputMap, ,animations, config.FrameRate);

            return(0);
        }