diff --git a/CHANGELOG.md b/CHANGELOG.md index 37f65d1..d918228 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ Jump to version: ## 8.0.0 (In Development) ### MLEM +Improvements +- Allow getting and setting TextureData by index +- Improve TextureExtensions.PremultipliedCopy memory performance + Fixes - Fixed formatting codes at the start of strings not being added to the AllCodes collection diff --git a/MLEM/Textures/TextureExtensions.cs b/MLEM/Textures/TextureExtensions.cs index adaaa69..7a080d6 100644 --- a/MLEM/Textures/TextureExtensions.cs +++ b/MLEM/Textures/TextureExtensions.cs @@ -24,15 +24,12 @@ namespace MLEM.Textures { /// The texture of which to create a premultiplied copy. /// The premultiplied copy of the . public static Texture2D PremultipliedCopy(this Texture2D texture) { + var data = new Color[texture.Width * texture.Height]; + texture.GetData(data); + for (var i = 0; i < data.Length; i++) + data[i] = Color.FromNonPremultiplied(data[i].ToVector4()); var ret = new Texture2D(texture.GraphicsDevice, texture.Width, texture.Height); - using (var textureData = texture.GetTextureData()) { - using (var retData = ret.GetTextureData()) { - for (var x = 0; x < ret.Width; x++) { - for (var y = 0; y < ret.Height; y++) - retData[x, y] = Color.FromNonPremultiplied(textureData[x, y].ToVector4()); - } - } - } + ret.SetData(data); return ret; } @@ -51,20 +48,34 @@ namespace MLEM.Textures { /// The x coordinate of the texture location /// The y coordinate of the texture location public Color this[int x, int y] { - get => this.data[this.ToIndex(x, y)]; + get => this[this.ToIndex(x, y)]; + set => this[this.ToIndex(x, y)] = value; + } + /// + /// Returns the color at the given x,y position of the texture, where 0,0 represents the bottom left. + /// + /// The x and y coordinates of the texture location + public Color this[Point point] { + get => this[point.X, point.Y]; + set => this[point.X, point.Y] = value; + } + /// + /// Returns the color at the given index of the texture data, which is the index in the array. + /// + /// The index. + public Color this[int index] { + get => this.data[index]; set { - var index = this.ToIndex(x, y); if (this.data[index] != value) { this.data[index] = value; this.dirty = true; } } } - /// - public Color this[Point point] { - get => this[point.X, point.Y]; - set => this[point.X, point.Y] = value; - } + /// + /// The length of the underlying texture data array, which is the texture's width multiplied by its height. + /// + public int Length => this.data.Length; /// /// Creates a new texture data instance for the given texture. @@ -107,7 +118,7 @@ namespace MLEM.Textures { /// The corresponding texture coordinate /// If the given index is out of bounds public Point FromIndex(int index) { - if (index < 0 || index >= this.data.Length) + if (index < 0 || index >= this.Length) throw new ArgumentException(); return new Point(index % this.texture.Width, index / this.texture.Width); }