コード例 #1
0
ファイル: FontSheet.cs プロジェクト: RogueCollab/RogueEssence
        //frompath (import) has two varieties: they will take
        // - take a folder containing all elements, each with a char number
        // - take a simple png
        //fromstream (load) will take the png and all the rectangles, and the char maps from stream
        //save will save as .font


        public static new FontSheet Import(string path)
        {
            //get all extra stat from an XML
            if (File.Exists(path + "FontData.xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path + "FontData.xml");
                int spaceWidth = Convert.ToInt32(doc.SelectSingleNode("FontData/SpaceWidth").InnerText);
                int charHeight = Convert.ToInt32(doc.SelectSingleNode("FontData/CharHeight").InnerText);
                int charSpace  = Convert.ToInt32(doc.SelectSingleNode("FontData/CharSpace").InnerText);
                int lineSpace  = Convert.ToInt32(doc.SelectSingleNode("FontData/LineSpace").InnerText);

                HashSet <int> colorlessGlyphs = new HashSet <int>();
                XmlNode       glyphNodes      = doc.SelectSingleNode("FontData/Colorless");
                foreach (XmlNode glyphNode in glyphNodes.SelectNodes("Glyph"))
                {
                    int glyphIdx = Convert.ToInt32(glyphNode.InnerText, 16);
                    colorlessGlyphs.Add(glyphIdx);
                }

                string[]         pngs   = Directory.GetFiles(path, "*.png", SearchOption.TopDirectoryOnly);
                List <ImageInfo> sheets = new List <ImageInfo>();
                foreach (string dir in pngs)
                {
                    int       png      = Convert.ToInt32(Path.GetFileNameWithoutExtension(dir), 16);
                    Texture2D newSheet = null;
                    using (FileStream fileStream = new FileStream(dir, FileMode.Open, FileAccess.Read, FileShare.Read))
                        newSheet = ImportTex(fileStream);

                    sheets.Add(new ImageInfo(png, newSheet));
                }
                if (sheets.Count == 0)
                {
                    return(null);
                }

                Canvas canvas = new Canvas();
                canvas.SetCanvasDimensions(Canvas.INFINITE_SIZE, Canvas.INFINITE_SIZE);
                OptimalMapper mapper = new OptimalMapper(canvas, 0.975f, 10);
                Atlas         atlas  = mapper.Mapping(sheets);

                Rectangle[] rects = new Rectangle[sheets.Count];
                Dictionary <int, GlyphData> chars = new Dictionary <int, GlyphData>();
                Texture2D tex = new Texture2D(device, atlas.Width, atlas.Height);
                for (int ii = 0; ii < atlas.MappedImages.Count; ii++)
                {
                    MappedImageInfo info = atlas.MappedImages[ii];
                    BaseSheet.Blit(info.ImageInfo.Texture, tex, 0, 0, info.ImageInfo.Width, info.ImageInfo.Height, info.X, info.Y);
                    rects[ii] = new Rectangle(info.X, info.Y, info.ImageInfo.Width, info.ImageInfo.Height);
                    chars[info.ImageInfo.ID] = new GlyphData(ii, colorlessGlyphs.Contains(info.ImageInfo.ID));
                    info.ImageInfo.Texture.Dispose();
                }

                return(new FontSheet(tex, rects, spaceWidth, charHeight, charSpace, lineSpace, chars));
            }
            else
            {
                throw new Exception("Error finding XML file in " + path + ".");
            }
        }
コード例 #2
0
        //frompath (import) will take a folder containing all elements
        //fromstream (load) will take the png and all the rectangles from stream
        //save will save as .atlas

        public static new SpriteSheet Import(string path)
        {
            string[]         pngs   = Directory.GetFiles(path, "*.png", SearchOption.TopDirectoryOnly);
            List <ImageInfo> sheets = new List <ImageInfo>();
            int index = 0;

            foreach (string dir in pngs)
            {
                Texture2D newSheet = null;
                using (FileStream fileStream = new FileStream(dir, FileMode.Open, FileAccess.Read, FileShare.Read))
                    newSheet = ImportTex(fileStream);

                sheets.Add(new ImageInfo(index, newSheet));
                index++;
            }
            if (sheets.Count == 0)
            {
                return(null);
            }

            Canvas canvas = new Canvas();

            canvas.SetCanvasDimensions(Canvas.INFINITE_SIZE, Canvas.INFINITE_SIZE);
            OptimalMapper mapper = new OptimalMapper(canvas);
            Atlas         atlas  = mapper.Mapping(sheets);

            Rectangle[] rects = new Rectangle[sheets.Count];
            Texture2D   tex   = new Texture2D(device, atlas.Width, atlas.Height);

            for (int ii = 0; ii < atlas.MappedImages.Count; ii++)
            {
                MappedImageInfo info = atlas.MappedImages[ii];
                BaseSheet.Blit(info.ImageInfo.Texture, tex, 0, 0, info.ImageInfo.Width, info.ImageInfo.Height, info.X, info.Y);
                rects[info.ImageInfo.ID] = new Rectangle(info.X, info.Y, info.ImageInfo.Width, info.ImageInfo.Height);
                info.ImageInfo.Texture.Dispose();
            }

            return(new SpriteSheet(tex, rects));
        }