using System; using System.Collections.Generic; using System.Drawing; using Blacklight.Core; namespace Konis.Blacklight { /// /// Turns sprite defs into the sprites of one window, loading each sheet once and keeping it for every def that /// shares it. A texture belongs to its window, so each window needs its own resolver. /// public class SpriteResolver { private readonly WindowHandle window; private readonly Dictionary sheets = new Dictionary(); private readonly Dictionary frames = new Dictionary(); /// /// Makes a resolver that creates its textures on , which must already be bound. /// public SpriteResolver(WindowHandle window) { this.window = window ?? throw new ArgumentNullException(nameof(window)); } /// /// Returns the part of an image that holds the grid describes. /// /// The image has no room for a sprite. public static Rectangle GridAreaOf(ResourceSheetDef sheet, Size imageSize) { int border = sheet.BorderSize; int strideX = sheet.SpriteWidth + border; int strideY = sheet.SpriteHeight + border; int columns = sheet.GridColumns; if (columns < 0) { columns = (imageSize.Width - sheet.GridOriginX + border) / strideX; } int rows = sheet.GridRows; if (rows < 0) { rows = (imageSize.Height - sheet.GridOriginY + border) / strideY; } if (columns < 1 || rows < 1) { throw new InvalidOperationException( $"An image of {imageSize} has no room for a {sheet.SpriteWidth} by {sheet.SpriteHeight} sprite " + $"at ({sheet.GridOriginX}, {sheet.GridOriginY})."); } return new Rectangle( sheet.GridOriginX, sheet.GridOriginY, columns * strideX - border, rows * strideY - border); } private SpriteSheet CreateSheet(SpriteSheetDef def, DecodedImage image, Rectangle gridArea) { // Only what the grid covers goes into the texture. if (gridArea != new Rectangle(Point.Empty, image.Size)) { image = SpriteSheet.Pack(image, new[] { gridArea }, new[] { Point.Empty }, gridArea.Size); } return SpriteSheet.Create(window, image, new Size(def.SpriteWidth, def.SpriteHeight), def.BorderSize); } private SpriteSheet LoadResourceSheet(ResourceSheetDef def) { using (System.IO.Stream stream = def.Library.Assembly.GetManifestResourceStream(def.Source)) { if (stream == null) { throw new InvalidOperationException( $"The assembly of {def.Library} has no embedded image called {def.Source}."); } DecodedImage image = DecodedImage.FromStream(stream); return CreateSheet(def, image, GridAreaOf(def, image.Size)); } } private SpriteSheet LoadPixelSheet(PixelSheetDef def) { var rgba = new byte[def.Rgba.Length]; def.Rgba.CopyTo(rgba, 0); var image = new DecodedImage(rgba, new Size(def.Width, def.Height)); int strideX = def.SpriteWidth + def.BorderSize; int strideY = def.SpriteHeight + def.BorderSize; var gridArea = new Rectangle( 0, 0, def.ColumnCount * strideX - def.BorderSize, def.RowCount * strideY - def.BorderSize); return CreateSheet(def, image, gridArea); } /// /// Returns the uploaded sheet describes, creating its texture the first time it is asked /// for. Every sprite of the sheet, and every caller, gets the same one. /// /// The def is null. /// The def or its image can't work. /// The def is a kind of sheet this doesn't know. public SpriteSheet SheetOf(SpriteSheetDef def) { if (def == null) { throw new ArgumentNullException(nameof(def)); } if (!sheets.TryGetValue(def, out SpriteSheet sheet)) { def.Validate(); switch (def) { case ResourceSheetDef resourceDef: sheet = LoadResourceSheet(resourceDef); break; case PixelSheetDef pixelDef: sheet = LoadPixelSheet(pixelDef); break; default: throw new NotSupportedException( $"A {def.GetType().Name} isn't a kind of sheet this can resolve."); } sheets.Add(def, sheet); } return sheet; } /// /// Returns the sprite describes, creating its sheet's texture the first time any sprite /// of that sheet is asked for. /// /// The def is null. /// The def or its image can't work. /// The def's sheet is a kind this doesn't know. public SpriteSource Resolve(SheetSpriteDef def) { if (def == null) { throw new ArgumentNullException(nameof(def)); } def.Validate(); return SheetOf(def.Sheet).Frame(new Point(def.Column, def.Row)); } /// /// Returns the sprite of each of the frames of , in order. The same array comes back /// each time, so asking every frame costs a lookup. /// /// The def is null. /// /// The def or its images can't work, or its frames don't share a texture. /// public SpriteSource[] ResolveFrames(SpriteDef def) { if (def == null) { throw new ArgumentNullException(nameof(def)); } if (!frames.TryGetValue(def, out SpriteSource[] returnMe)) { def.Validate(); returnMe = new SpriteSource[def.Frames.Count]; for (int i = 0; i < returnMe.Length; i++) { returnMe[i] = Resolve(def.Frames[i]); // A batch has one texture, so a frame from another sheet couldn't be drawn beside the rest. if (!ReferenceEquals(returnMe[i].Texture, returnMe[0].Texture)) { throw new InvalidOperationException( $"Frame {i} of the sprite {def.Name} is on a different texture from frame 0."); } } frames.Add(def, returnMe); } return returnMe; } /// /// Returns the sprite of each frame the particles of draw. /// /// The def is null. /// The def has no sprite, or its sprite can't work. public SpriteSource[] ResolveFramesFor(EmitterDef def) { if (def == null) { throw new ArgumentNullException(nameof(def)); } if (def.Sprite == null) { throw new InvalidOperationException($"The emitter def {def.Name ?? def.GetType().Name} has no sprite."); } return ResolveFrames(def.Sprite); } } }