/// <summary> /// Initializes a new instance of the <see cref="DynamicTextureAtlas"/> class. /// </summary> /// <param name="uv">The Ultraviolet context.</param> /// <param name="width">The width of the texture atlas in pixels.</param> /// <param name="height">The height of the texture atlas in pixels.</param> /// <param name="spacing">The number of pixels between cells on the texture atlas.</param> /// <param name="options">The texture's configuration options.</param> private DynamicTextureAtlas(UltravioletContext uv, Int32 width, Int32 height, Int32 spacing, TextureOptions options) : base(uv) { Contract.EnsureRange(width > 0, nameof(width)); Contract.EnsureRange(height > 0, nameof(height)); Contract.EnsureRange(spacing >= 0, nameof(spacing)); var isSrgb = (options & TextureOptions.SrgbColor) == TextureOptions.SrgbColor; var isLinear = (options & TextureOptions.LinearColor) == TextureOptions.LinearColor; if (isSrgb && isLinear) { throw new ArgumentException(UltravioletStrings.TextureCannotHaveMultipleEncodings); } var caps = uv.GetGraphics().Capabilities; var srgbEncoded = (isLinear ? false : (isSrgb ? true : uv.Properties.SrgbDefaultForTexture2D)) && caps.SrgbEncodingEnabled; var surfOptions = (srgbEncoded ? SurfaceOptions.SrgbColor : SurfaceOptions.LinearColor); this.IsFlipped = Ultraviolet.GetGraphics().Capabilities.FlippedTextures; this.Width = width; this.Height = height; this.Spacing = spacing; this.Surface = Surface2D.Create(width, height, surfOptions); this.Texture = Texture2D.CreateDynamicTexture(width, height, options, this, (dt2d, state) => { ((DynamicTextureAtlas)state).Flush(); }); Clear(true); Invalidate(); }
/// <summary> /// Creates the output surface for a texture atlas. /// </summary> private static Surface2D CreateOutputSurface(TextureAtlasDescription atlasDesc, IEnumerable <TextureAtlasImage> atlasImages, ContentManager content, IContentProcessorMetadata metadata, Int32 width, Int32 height, Dictionary <String, Rectangle> images) { var output = Surface2D.Create(width, height); foreach (var image in atlasImages) { var imageArea = images[image.Name]; using (var imageSurface = content.Load <Surface2D>(image.Path, metadata.AssetDensity, false, metadata.IsLoadedFromSolution)) { var areaWithoutPadding = new Rectangle( imageArea.X, imageArea.Y, imageArea.Width - atlasDesc.Metadata.Padding, imageArea.Height - atlasDesc.Metadata.Padding); imageSurface.Blit(output, areaWithoutPadding); } } return(output); }