Esempio n. 1
0
        private static void CompileSpriteToFile(string spriteDir, Dictionary <string, byte[]> spriteData, int num, int form, int shiny, int gender)
        {
            //check to see if files exist
            string[] pngs = Directory.GetFiles(spriteDir, "*.png", SearchOption.TopDirectoryOnly);
            if (pngs.Length < 1)
            {
                return;
            }

            string outForm = "r";

            if (form >= 0)
            {
                outForm += "-" + form;
                if (shiny >= 0)
                {
                    outForm += "-" + shiny;
                    if (gender >= 0)
                    {
                        outForm += "-" + gender;
                    }
                }
            }

            try
            {
                SpriteSheet sprite = CompileSpriteInternal(spriteDir);

                using (MemoryStream spriteStream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(spriteStream))
                    {
                        //Go through each frame type, get metadata, sprite data
                        foreach (FrameType frameType in Enum.GetValues(typeof(FrameType)))
                        {
                            if (!SpriteSheet.IsFrameTypeDirectionless(frameType))
                            {
                                for (int j = 0; j < 8; j++)
                                {
                                    Maps.Direction8 dir            = (Maps.Direction8)j;
                                    int             frameCountSize = sprite.FrameData.GetFrameCount(frameType, dir);

                                    TileSheet anim = sprite.GetSheet(frameType, dir);

                                    // Add frame count size
                                    writer.Write(frameCountSize);

                                    int    sheetSize      = 0;
                                    byte[] memStreamArray = new byte[0];
                                    if (anim != null)
                                    {
                                        using (MemoryStream memoryStream = new MemoryStream())
                                        {
                                            anim.Save(memoryStream, ImageFormat.Png);
                                            memStreamArray = memoryStream.ToArray();
                                            sheetSize      = memStreamArray.Length;
                                        }
                                        anim.Dispose();
                                    }
                                    // Add the animation size
                                    writer.Write(sheetSize);
                                    //add the animation itself
                                    if (sheetSize > 0)
                                    {
                                        spriteStream.Write(memStreamArray, 0, memStreamArray.Length);
                                    }
                                }
                            }
                            else
                            {
                                int frameCountSize = sprite.FrameData.GetFrameCount(frameType, Maps.Direction8.Down);

                                TileSheet anim = sprite.GetSheet(frameType, Maps.Direction8.Down);

                                // Add frame count size
                                writer.Write(frameCountSize);

                                int    sheetSize      = 0;
                                byte[] memStreamArray = new byte[0];
                                if (anim != null)
                                {
                                    using (MemoryStream memoryStream = new MemoryStream())
                                    {
                                        anim.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
                                        memStreamArray = memoryStream.ToArray();
                                        sheetSize      = memStreamArray.Length;
                                    }
                                    anim.Dispose();
                                }
                                // Add the animation size
                                writer.Write(sheetSize);
                                //add the animation itself
                                if (sheetSize > 0)
                                {
                                    spriteStream.Write(memStreamArray, 0, memStreamArray.Length);
                                }
                            }
                        }
                    }

                    byte[] writingBytes = spriteStream.ToArray();
                    spriteData.Add(outForm, writingBytes);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error compiling sprite" + spriteDir + "\n", ex);
            }
        }
Esempio n. 2
0
        private static SpriteSheet CompileSpriteInternal(string spriteRootDirectory)
        {
            Dictionary <FrameType, Dictionary <Maps.Direction8, List <Bitmap> > > frameCollection = new Dictionary <FrameType, Dictionary <Maps.Direction8, List <Bitmap> > >();
            Dictionary <FrameType, List <Bitmap> > miscFrameCollection = new Dictionary <FrameType, List <Bitmap> >();
            List <string> usedFrames = new List <string>();

            //get all frames
            foreach (FrameType frameType in Enum.GetValues(typeof(FrameType)))
            {
                if (!SpriteSheet.IsFrameTypeDirectionless(frameType))
                {
                    Dictionary <Maps.Direction8, List <Bitmap> > frameTypeDirectionCollection = new Dictionary <Maps.Direction8, List <Bitmap> >();
                    for (int j = 0; j < 8; j++)
                    {
                        Maps.Direction8 direction = (Maps.Direction8)j;

                        List <Bitmap> frameTypeCollection = new List <Bitmap>();
                        frameTypeDirectionCollection.Add(direction, frameTypeCollection);

                        foreach (Tuple <string, Bitmap> frameSurface in GetAllFrames(spriteRootDirectory, frameType + "-" + direction + "-"))
                        {
                            frameTypeCollection.Add(frameSurface.Item2);
                            usedFrames.Add(frameSurface.Item1);
                        }
                        //conversionNotes.Text += "Found " + frameTypeCollection.Count + " frames for " + frameType.ToString() + " (" + direction.ToString() + ") animation.\r\n";
                    }

                    frameCollection.Add(frameType, frameTypeDirectionCollection);
                }
                else
                {
                    List <Bitmap> frameTypeCollection = new List <Bitmap>();
                    miscFrameCollection.Add(frameType, frameTypeCollection);

                    foreach (Tuple <string, Bitmap> frameSurface in GetAllFrames(spriteRootDirectory, frameType.ToString() + "-"))
                    {
                        frameTypeCollection.Add(frameSurface.Item2);
                        usedFrames.Add(frameSurface.Item1);
                    }
                    //conversionNotes.Text += "Found " + frameTypeCollection.Count + " frames for " + frameType.ToString() + " (Directionless) animation.\r\n";
                }
            }

            if (usedFrames.Count < 1)
            {
                throw new Exception("No frames");
            }

            List <string> pngs = new List <string>(Directory.GetFiles(spriteRootDirectory, "*.png", SearchOption.TopDirectoryOnly));

            if (pngs.Count > usedFrames.Count)
            {
                foreach (Dictionary <Maps.Direction8, List <Bitmap> > dict in frameCollection.Values)
                {
                    foreach (List <Bitmap> list in dict.Values)
                    {
                        foreach (Bitmap bitmap in list)
                        {
                            bitmap.Dispose();
                        }
                    }
                }

                foreach (List <Bitmap> list in miscFrameCollection.Values)
                {
                    foreach (Bitmap bitmap in list)
                    {
                        bitmap.Dispose();
                    }
                }

                foreach (string path in usedFrames)
                {
                    for (int i = pngs.Count - 1; i >= 0; i--)
                    {
                        if (pngs[i] == path)
                        {
                            pngs.RemoveAt(i);
                        }
                    }
                }

                string errorMsg = spriteRootDirectory + ": The following files do not match the naming pattern, or a preceding file is missing:";
                for (int i = 0; i < pngs.Count; i++)
                {
                    errorMsg += "\n" + pngs[i].Substring(pngs[i].LastIndexOf('\\'));
                }

                throw new Exception(errorMsg);
            }

            SpriteSheet sprite = BuildSpriteSurface(frameCollection, miscFrameCollection, spriteRootDirectory);

            return(sprite);
        }