예제 #1
0
        /// <summary>
        /// Create a SpriteSet from a directory
        /// </summary>
        /// <param name="name">SpriteSet name</param>
        /// <param name="path">Directory path</param>
        /// <param name="recursive">Whether to use a recursive file search</param>
        public static SpriteSet FromDirectory(string name, string path, bool recursive, bool bufferSpriteEdges)
        {
            // Create bitmaps from .png files
            var pngs    = Directory.GetFiles(path, "*.png", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
            var bitmaps = new Bitmap[pngs.Length];

            for (var i = 0; i < pngs.Length; i++)
            {
                var file     = pngs[i];
                var fullPath = Path.Combine(path, file);
                bitmaps[i] = (Bitmap)Image.FromFile(fullPath);
            }

            // Create an atlas of the bitmaps
            var atlas = TextureAtlas.CreateBitmapAtlas(bitmaps, 1, out var regions, bufferSpriteEdges);

            // Dispose the bitmaps
            foreach (var bitmap in bitmaps)
            {
                bitmap.Dispose();
            }
            // Create a texture from the atlas
            var tex = Texture2D.FromBitmap($"{name} Texture", atlas, true);

            // Dispose the atlas
            atlas.Dispose();

            // Create default shader program
            var program = ShaderProgram.Create($"{name} Shader Program");

            using (var vert = Content.LoadShader(Path.Combine("shaders", "sprites.vert"), ShaderType.Vertex))
            {
                using (var frag = Content.LoadShader(Path.Combine("shaders", "sprites.frag"), ShaderType.Fragment))
                {
                    program.AttachShaders(vert, frag);
                    program.Link();
                    program.DetachShaders(vert, frag);
                }
            }
            program.EnableBlockCamera();

            // Create a spriteset using the atlas texture
            var spriteSet = new SpriteSet(name, tex);

            // Add sprites to the spriteset using the bitmap file names and atlas regions
            for (var i = 0; i < pngs.Length; i++)
            {
                var spriteName = Path.GetFileNameWithoutExtension(pngs[i]);
                var sprite     = spriteSet.SetSprite(spriteName, program, regions[i]);
                var jsonFile   = Path.ChangeExtension(pngs[i], "json");
                if (File.Exists(jsonFile))
                {
                    var json = JsonConvert.DeserializeObject <Dictionary <string, object> >(File.ReadAllText(jsonFile));
                    if (json.TryGetValue("origin", out var origin_obj))
                    {
                        var origin = (JArray)origin_obj;
                        sprite.Origin = new Vector2 <float>((float)origin[0], (float)origin[1]);
                    }
                }
            }

            // Return the new SpriteSet
            return(spriteSet);
        }