public static void Init() { Tiles = SpriteSet.FromDirectory("Tiles Sprite Set", Path.Combine(Content.Root, "spritesets", "tiles"), true, true); Editor = SpriteSet.FromDirectory("Editor Sprite Set", Path.Combine(Content.Root, "spritesets", "editor"), true, false); Player = SpriteSet.FromDirectory("Player Sprite Set", Path.Combine(Content.Root, "spritesets", "player"), true, false); Common = SpriteSet.FromDirectory("Common Sprite Set", Path.Combine(Content.Root, "spritesets", "common"), true, false); }
/// <summary> /// Create a sprite /// </summary> /// <param name="name"></param> /// <param name="spriteSet"></param> /// <param name="rectangle"></param> public Sprite(string name, SpriteSet spriteSet, ShaderProgram shaderProgram, Rectangle rectangle) : base(name, shaderProgram) { if (spriteSet.Texture == null) { throw new NullReferenceException("SpriteSet's texture is null"); } SpriteSet = spriteSet; TopLeft = rectangle.GetLeftTop(); Size = rectangle.GetSize(); TopLeftCoord = new Vector2 <float>( (float)TopLeft.X / spriteSet.Texture.Dimensions[0].X, 1f - (float)TopLeft.Y / spriteSet.Texture.Dimensions[0].Y ); SizeCoord = new Vector2 <float>( (float)Size.X / spriteSet.Texture.Dimensions[0].X, (float)Size.Y / -spriteSet.Texture.Dimensions[0].Y ); }
/// <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); }