Ejemplo n.º 1
0
        private bool HasFrames(Atlas atlas, string path, int[] frames = null)
        {
            if (frames == null || frames.Length <= 0)
            {
                return(atlas.GetAtlasSubtexturesAt(path, 0) != null);
            }
            else
            {
                for (int i = 0; i < frames.Length; i++)
                {
                    if (atlas.GetAtlasSubtexturesAt(path, frames[i]) == null)
                    {
                        return(false);
                    }
                }

                return(true);
            }
        }
Ejemplo n.º 2
0
        public static Atlas FromMultiAtlas(string rootPath, string filename, AtlasDataFormat format)
        {
            var atlas = new Atlas();

            atlas.Sources = new List <Texture2D>();

            var index = 0;

            while (true)
            {
                var dataPath = Path.Combine(rootPath, filename + index.ToString() + ".xml");

                if (!File.Exists(Path.Combine(Engine.ContentDirectory, dataPath)))
                {
                    break;
                }

                ReadAtlasData(atlas, dataPath, format);
                index++;
            }

            return(atlas);
        }
Ejemplo n.º 3
0
        public static Atlas FromDirectory(string path)
        {
            var atlas = new Atlas();

            atlas.Sources = new List <Texture2D>();

            var contentDirectory       = Engine.ContentDirectory;
            var contentDirectoryLength = contentDirectory.Length;
            var contentPath            = Path.Combine(contentDirectory, path);
            var contentPathLength      = contentPath.Length;

            foreach (var file in Directory.GetFiles(contentPath, "*", SearchOption.AllDirectories))
            {
                var ext = Path.GetExtension(file);
                if (ext != ".png" && ext != ".xnb")
                {
                    continue;
                }

                // get path and load
                var fileStream = new FileStream(file.Substring(contentDirectoryLength + 1), FileMode.Open, FileAccess.Read);
                var texture    = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                fileStream.Close();

                atlas.Sources.Add(texture);

                // make nice for dictionary
                var filepath = file.Substring(contentPathLength + 1);
                filepath = filepath.Substring(0, filepath.Length - 4);
                filepath = filepath.Replace('\\', '/');

                // load
                atlas.textures.Add(filepath, new MTexture(texture));
            }

            return(atlas);
        }
Ejemplo n.º 4
0
 public SpriteBank(Atlas atlas, string xmlPath)
     : this(atlas, Calc.LoadContentXML(xmlPath))
 {
 }
Ejemplo n.º 5
0
 public SpriteData(Atlas atlas)
 {
     Sprite = new Sprite(atlas, "");
     Atlas  = atlas;
 }
Ejemplo n.º 6
0
        private static void ReadAtlasData(Atlas atlas, string path, AtlasDataFormat format)
        {
            switch (format)
            {
            case AtlasDataFormat.TexturePacker_Sparrow:
            {
                XmlDocument xml = Calc.LoadContentXML(path);
                XmlElement  at  = xml["TextureAtlas"];

                var texturePath = at.Attr("imagePath", "");
                var fileStream  = new FileStream(Path.Combine(Path.GetDirectoryName(path), texturePath), FileMode.Open, FileAccess.Read);
                var texture     = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                fileStream.Close();

                var mTexture = new MTexture(texture);
                atlas.Sources.Add(texture);

                var subtextures = at.GetElementsByTagName("SubTexture");

                foreach (XmlElement sub in subtextures)
                {
                    var name     = sub.Attr("name");
                    var clipRect = sub.Rect();
                    if (sub.HasAttr("frameX"))
                    {
                        atlas.textures[name] = new MTexture(mTexture, name, clipRect, new Vector2(-sub.AttrInt("frameX"), -sub.AttrInt("frameY")), sub.AttrInt("frameWidth"), sub.AttrInt("frameHeight"));
                    }
                    else
                    {
                        atlas.textures[name] = new MTexture(mTexture, name, clipRect);
                    }
                }
            }
            break;

            case AtlasDataFormat.CrunchXml:
            {
                XmlDocument xml = Calc.LoadContentXML(path);
                XmlElement  at  = xml["atlas"];

                foreach (XmlElement tex in at)
                {
                    var    texturePath = tex.Attr("n", "");
                    string fsloc       = Engine.ContentDirectory + "\\" + Path.GetDirectoryName(path) + texturePath + ".png";
                    var    fileStream  = new FileStream(fsloc, FileMode.Open, FileAccess.Read);
                    var    texture     = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                    fileStream.Close();

                    var mTexture = new MTexture(texture);
                    atlas.Sources.Add(texture);

                    foreach (XmlElement sub in tex)
                    {
                        var name     = sub.Attr("n");
                        var clipRect = new Rectangle(sub.AttrInt("x"), sub.AttrInt("y"), sub.AttrInt("w"), sub.AttrInt("h"));
                        if (sub.HasAttr("fx"))
                        {
                            atlas.textures[name] = new MTexture(mTexture, name, clipRect, new Vector2(-sub.AttrInt("fx"), -sub.AttrInt("fy")), sub.AttrInt("fw"), sub.AttrInt("fh"));
                        }
                        else
                        {
                            atlas.textures[name] = new MTexture(mTexture, name, clipRect);
                        }
                    }
                }
            }
            break;

            case AtlasDataFormat.CrunchBinary:
                using (var stream = File.OpenRead(Path.Combine(Engine.ContentDirectory, path)))
                {
                    var reader   = new BinaryReader(stream);
                    var textures = reader.ReadInt16();

                    for (int i = 0; i < textures; i++)
                    {
                        var textureName = reader.ReadNullTerminatedString();
                        var texturePath = Path.Combine(Path.GetDirectoryName(path), textureName + ".png");
                        var fileStream  = new FileStream(texturePath, FileMode.Open, FileAccess.Read);
                        var texture     = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                        fileStream.Close();

                        atlas.Sources.Add(texture);

                        var mTexture    = new MTexture(texture);
                        var subtextures = reader.ReadInt16();
                        for (int j = 0; j < subtextures; j++)
                        {
                            var name = reader.ReadNullTerminatedString();
                            var x    = reader.ReadInt16();
                            var y    = reader.ReadInt16();
                            var w    = reader.ReadInt16();
                            var h    = reader.ReadInt16();
                            var fx   = reader.ReadInt16();
                            var fy   = reader.ReadInt16();
                            var fw   = reader.ReadInt16();
                            var fh   = reader.ReadInt16();

                            atlas.textures[name] = new MTexture(mTexture, name, new Rectangle(x, y, w, h), new Vector2(-fx, -fy), fw, fh);
                        }
                    }
                }
                break;

            case AtlasDataFormat.CrunchBinaryNoAtlas:
                using (var stream = File.OpenRead(Path.Combine(Engine.ContentDirectory, path + ".bin")))
                {
                    var reader  = new BinaryReader(stream);
                    var folders = reader.ReadInt16();

                    for (int i = 0; i < folders; i++)
                    {
                        var folderName = reader.ReadNullTerminatedString();
                        var folderPath = Path.Combine(Path.GetDirectoryName(path), folderName);

                        var subtextures = reader.ReadInt16();
                        for (int j = 0; j < subtextures; j++)
                        {
                            var name = reader.ReadNullTerminatedString();
                            var x    = reader.ReadInt16();
                            var y    = reader.ReadInt16();
                            var w    = reader.ReadInt16();
                            var h    = reader.ReadInt16();
                            var fx   = reader.ReadInt16();
                            var fy   = reader.ReadInt16();
                            var fw   = reader.ReadInt16();
                            var fh   = reader.ReadInt16();

                            var fileStream = new FileStream(Path.Combine(folderPath, name + ".png"), FileMode.Open, FileAccess.Read);
                            var texture    = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                            fileStream.Close();

                            atlas.Sources.Add(texture);
                            atlas.textures[name] = new MTexture(texture, new Vector2(-fx, -fy), fw, fh);
                        }
                    }
                }
                break;

            case AtlasDataFormat.Packer:

                using (var stream = File.OpenRead(Path.Combine(Engine.ContentDirectory, path + ".meta")))
                {
                    var reader = new BinaryReader(stream);
                    reader.ReadInt32();     // version
                    reader.ReadString();    // args
                    reader.ReadInt32();     // hash

                    var textures = reader.ReadInt16();
                    for (int i = 0; i < textures; i++)
                    {
                        var textureName = reader.ReadString();
                        var texturePath = Path.Combine(Path.GetDirectoryName(path), textureName + ".data");
                        var fileStream  = new FileStream(texturePath, FileMode.Open, FileAccess.Read);
                        var texture     = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                        fileStream.Close();

                        atlas.Sources.Add(texture);

                        var mTexture    = new MTexture(texture);
                        var subtextures = reader.ReadInt16();
                        for (int j = 0; j < subtextures; j++)
                        {
                            var name = reader.ReadString().Replace('\\', '/');
                            var x    = reader.ReadInt16();
                            var y    = reader.ReadInt16();
                            var w    = reader.ReadInt16();
                            var h    = reader.ReadInt16();
                            var fx   = reader.ReadInt16();
                            var fy   = reader.ReadInt16();
                            var fw   = reader.ReadInt16();
                            var fh   = reader.ReadInt16();

                            atlas.textures[name] = new MTexture(mTexture, name, new Rectangle(x, y, w, h), new Vector2(-fx, -fy), fw, fh);
                        }
                    }
                }

                break;

            case AtlasDataFormat.PackerNoAtlas:
                using (var stream = File.OpenRead(Path.Combine(Engine.ContentDirectory, path + ".meta")))
                {
                    var reader = new BinaryReader(stream);
                    reader.ReadInt32();     // version
                    reader.ReadString();    // args
                    reader.ReadInt32();     // hash

                    var folders = reader.ReadInt16();
                    for (int i = 0; i < folders; i++)
                    {
                        var folderName = reader.ReadString();
                        var folderPath = Path.Combine(Path.GetDirectoryName(path), folderName);

                        var subtextures = reader.ReadInt16();
                        for (int j = 0; j < subtextures; j++)
                        {
                            var name = reader.ReadString().Replace('\\', '/');
                            var x    = reader.ReadInt16();
                            var y    = reader.ReadInt16();
                            var w    = reader.ReadInt16();
                            var h    = reader.ReadInt16();
                            var fx   = reader.ReadInt16();
                            var fy   = reader.ReadInt16();
                            var fw   = reader.ReadInt16();
                            var fh   = reader.ReadInt16();

                            var fileStream = new FileStream(Path.Combine(folderPath, name + ".data"), FileMode.Open, FileAccess.Read);
                            var texture    = Texture2D.FromStream(Engine.Instance.GraphicsDevice, fileStream);
                            fileStream.Close();

                            atlas.Sources.Add(texture);
                            atlas.textures[name] = new MTexture(texture, new Vector2(-fx, -fy), fw, fh);
                        }
                    }
                }
                break;

            case AtlasDataFormat.CrunchXmlOrBinary:

                if (File.Exists(Path.Combine(Engine.ContentDirectory, path + ".bin")))
                {
                    ReadAtlasData(atlas, path + ".bin", AtlasDataFormat.CrunchBinary);
                }
                else
                {
                    ReadAtlasData(atlas, path + ".xml", AtlasDataFormat.CrunchXml);
                }

                break;


            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 7
0
        public PixelFontSize AddFontSize(string path, XmlElement data, Atlas atlas = null, bool outline = false)
        {
            // check if size already exists
            var size = data["info"].AttrFloat("size");

            foreach (var fs in Sizes)
            {
                if (fs.Size == size)
                {
                    return(fs);
                }
            }

            // get textures
            Textures = new List <MTexture>();
            var pages = data["pages"];

            foreach (XmlElement page in pages)
            {
                var file      = page.Attr("file");
                var atlasPath = Path.GetFileNameWithoutExtension(file);

                if (atlas != null && atlas.Has(atlasPath))
                {
                    Textures.Add(atlas[atlasPath]);
                }
                else
                {
                    var dir = Path.GetDirectoryName(path);
                    dir = dir.Substring(Engine.ContentDirectory.Length + 1);
                    Textures.Add(MTexture.FromFile(Path.Combine(dir, file)));
                }
            }

            // create font size
            var fontSize = new PixelFontSize()
            {
                Textures   = Textures,
                Characters = new Dictionary <int, PixelFontCharacter>(),
                LineHeight = data["common"].AttrInt("lineHeight"),
                Size       = size,
                Outline    = outline
            };

            // get characters
            foreach (XmlElement character in data["chars"])
            {
                int id   = character.AttrInt("id");
                int page = character.AttrInt("page", 0);
                fontSize.Characters.Add(id, new PixelFontCharacter(id, Textures[page], character));
            }

            // get kerning
            if (data["kernings"] != null)
            {
                foreach (XmlElement kerning in data["kernings"])
                {
                    var from = kerning.AttrInt("first");
                    var to   = kerning.AttrInt("second");
                    var push = kerning.AttrInt("amount");

                    PixelFontCharacter c = null;
                    if (fontSize.Characters.TryGetValue(from, out c))
                    {
                        c.Kerning.Add(to, push);
                    }
                }
            }

            // add font size
            Sizes.Add(fontSize);
            Sizes.Sort((a, b) => { return(Math.Sign(a.Size - b.Size)); });

            return(fontSize);
        }
Ejemplo n.º 8
0
        public PixelFontSize AddFontSize(string path, Atlas atlas = null, bool outline = false)
        {
            var data = Calc.LoadXML(path)["font"];

            return(AddFontSize(path, data, atlas, outline));
        }