From b6b40402db89b407365e70a0f0562a52b1667def Mon Sep 17 00:00:00 2001 From: BoredGuy Date: Sun, 8 Feb 2026 09:39:24 +0300 Subject: Back on track fully --- MonoGameLibrary/Graphics/TextureAtlas.cs | 89 +++++++++++++++++++++++++++++++ MonoGameLibrary/Graphics/TextureRegion.cs | 75 ++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 MonoGameLibrary/Graphics/TextureAtlas.cs create mode 100644 MonoGameLibrary/Graphics/TextureRegion.cs (limited to 'MonoGameLibrary/Graphics') diff --git a/MonoGameLibrary/Graphics/TextureAtlas.cs b/MonoGameLibrary/Graphics/TextureAtlas.cs new file mode 100644 index 0000000..751c341 --- /dev/null +++ b/MonoGameLibrary/Graphics/TextureAtlas.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Runtime.Serialization; +using System.Xml; +using System.Xml.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; + +namespace MonoGameLibrary.Graphics; + +public class TextureAtlas +{ + private Dictionary _regions; + public Texture2D Texture { get; set; } + + public TextureAtlas() + { + _regions = []; + } + + public TextureAtlas(Texture2D texture) + { + Texture = texture; + _regions = []; + } + + public void AddRegion(string name, int x, int y, int width, int height) + { + TextureRegion region = new TextureRegion(Texture, x, y, width, height); + _regions.Add(name, region); + } + + public TextureRegion GetRegion(string name) + { + return _regions[name]; + } + + public bool RemoveRegion(string name) + { + return _regions.Remove(name); + } + + public void Clear() + { + _regions.Clear(); + } + + public static TextureAtlas FromFile(ContentManager content, string fileName) + { + TextureAtlas atlas = new TextureAtlas(); + + string filePath = Path.Combine(content.RootDirectory, fileName); + + using (Stream stream = TitleContainer.OpenStream(filePath)) + { + using (XmlReader reader = XmlReader.Create(stream)) + { + XDocument doc = XDocument.Load(reader); + XElement root = doc.Root; + + string texturePath = root.Element("Texture").Value; + atlas.Texture = content.Load(texturePath); + + var regions = root.Element("Regions")?.Elements("Region"); + + if (regions != null) + { + foreach (var region in regions) + { + string name = region.Attribute("name")?.Value; + int x = int.Parse(region.Attribute("x")?.Value ?? "0"); + int y = int.Parse(region.Attribute("y")?.Value ?? "0"); + int width = int.Parse(region.Attribute("width")?.Value ?? "0"); + int height = int.Parse(region.Attribute("height")?.Value ?? "0"); + + if (!string.IsNullOrEmpty(name)) + { + atlas.AddRegion(name, x, y, width, height); + } + } + } + } + } + + return atlas; + } +} \ No newline at end of file diff --git a/MonoGameLibrary/Graphics/TextureRegion.cs b/MonoGameLibrary/Graphics/TextureRegion.cs new file mode 100644 index 0000000..f03097c --- /dev/null +++ b/MonoGameLibrary/Graphics/TextureRegion.cs @@ -0,0 +1,75 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace MonoGameLibrary.Graphics; + +/// +/// Represents a rectangular region within a texture +/// +public class TextureRegion +{ + /// + /// Gets or Sets the source texture this texture region is part of + /// + public Texture2D Texture { get; set; } + + /// + /// Gets or Sets the source rectangle boundary of this texture region within the source texture + /// + public Rectangle SourceRectangle { get; set; } + + /// + /// Gets the width, in pixels, of this texture region + /// + public int Width => SourceRectangle.Width; + + /// + /// Gets the height, in pixels, of this texture region/ + /// + public int Height => SourceRectangle.Height; + + /// + /// Creates a new texture region. + /// + public TextureRegion() { } + + public TextureRegion(Texture2D texture, int x, int y, int width, int height) + { + Texture = texture; + SourceRectangle = new Rectangle(x, y, width, height); + } + + public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color) + { + Draw(spriteBatch, position, color, 0.0f, Vector2.Zero, Vector2.One, SpriteEffects.None, 0.0f); + } + + public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) + { + Draw( + spriteBatch, + position, + color, + rotation, + origin, + new Vector2(scale, scale), + effects, + layerDepth + ); + } + + public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) + { + spriteBatch.Draw( + Texture, + position, + SourceRectangle, + color, + rotation, + origin, + scale, + effects, + layerDepth + ); + } +} \ No newline at end of file -- cgit v1.2.3