Reverted TextureAtlas indexing to the original string system

This commit is contained in:
Perry 2026-03-14 11:40:04 +01:00
parent 182ebfc31c
commit de2b2a56f0

View file

@ -10,7 +10,8 @@ namespace MonoGameLibrary.Graphics;
public class TextureAtlas
{
private TextureRegion[] _regions;
// private TextureRegion[] _regions;
private readonly Dictionary<string, TextureRegion> _regions;
/// <summary>
/// Gets or Sets the source texture represented by this texture atlas.
@ -28,9 +29,9 @@ public class TextureAtlas
/// Creates a new texture atlas instance using the given texture.
/// </summary>
/// <param name="texture">The source texture represented by the texture atlas.</param>
public TextureAtlas(Texture2D texture, int spriteCount) {
public TextureAtlas(Texture2D texture) {
Texture = texture;
_regions = new TextureRegion[spriteCount];
_regions = new();
}
// /// <summary>
@ -51,10 +52,9 @@ public class TextureAtlas
/// </summary>
/// <param name="name">The name of the region to retrieve.</param>
/// <returns>The TextureRegion with the specified name.</returns>
public TextureRegion this[int id] {
get => _regions[id];
set => _regions[id] = value;
public TextureRegion this[string name] {
get => _regions[name];
set => _regions[name] = value;
}
// public TextureRegion GetRegion(string name) {
@ -96,8 +96,8 @@ public class TextureAtlas
// The <Texture> element contains the content path for the Texture2D to load.
// So we will retrieve that value then use the content manager to load the texture.
string texturePath = root.Element("Texture").Value;
int spriteCount = int.Parse(root.Attribute("count").Value);
atlas = new TextureAtlas(content.Load<Texture2D>(texturePath), spriteCount);
// int spriteCount = int.Parse(root.Attribute("count").Value);
atlas = new TextureAtlas(content.Load<Texture2D>(texturePath));
// The <Regions> element contains individual <Region> elements, each one describing
// a different texture region within the atlas.
@ -121,7 +121,7 @@ public class TextureAtlas
// int width = int.Parse(region.Attribute("width")?.Value ?? "0");
// int height = int.Parse(region.Attribute("height")?.Value ?? "0");
atlas[int.Parse(region.Attribute("id").Value)] = new TextureRegion(
atlas[region.Attribute("name").Value] = new TextureRegion(
atlas.Texture,
int.Parse(region.Attribute("x")?.Value ?? "0"),
int.Parse(region.Attribute("y")?.Value ?? "0"),