commit 7366d5cb22ca96340df6b85d88cd4f0f159efbb5 Author: Perry-Man Date: Sat Jul 19 19:12:21 2025 +0200 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/.idea/.idea.MonoGameLibrary/.idea/.gitignore b/.idea/.idea.MonoGameLibrary/.idea/.gitignore new file mode 100644 index 0000000..fc89f1a --- /dev/null +++ b/.idea/.idea.MonoGameLibrary/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/contentModel.xml +/projectSettingsUpdater.xml +/.idea.MonoGameLibrary.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.MonoGameLibrary/.idea/indexLayout.xml b/.idea/.idea.MonoGameLibrary/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.MonoGameLibrary/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.MonoGameLibrary/.idea/vcs.xml b/.idea/.idea.MonoGameLibrary/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.MonoGameLibrary/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MonoGameLibrary.sln b/MonoGameLibrary.sln new file mode 100644 index 0000000..968892a --- /dev/null +++ b/MonoGameLibrary.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonoGameLibrary", "MonoGameLibrary\MonoGameLibrary.csproj", "{16577832-EF98-4CEC-A3A9-4BB0EB5D3907}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {16577832-EF98-4CEC-A3A9-4BB0EB5D3907}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {16577832-EF98-4CEC-A3A9-4BB0EB5D3907}.Debug|Any CPU.Build.0 = Debug|Any CPU + {16577832-EF98-4CEC-A3A9-4BB0EB5D3907}.Release|Any CPU.ActiveCfg = Release|Any CPU + {16577832-EF98-4CEC-A3A9-4BB0EB5D3907}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/MonoGameLibrary/.config/dotnet-tools.json b/MonoGameLibrary/.config/dotnet-tools.json new file mode 100644 index 0000000..b345335 --- /dev/null +++ b/MonoGameLibrary/.config/dotnet-tools.json @@ -0,0 +1,30 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-mgcb-editor": { + "version": "3.8.4", + "commands": [ + "mgcb-editor" + ] + }, + "dotnet-mgcb-editor-linux": { + "version": "3.8.4", + "commands": [ + "mgcb-editor-linux" + ] + }, + "dotnet-mgcb-editor-windows": { + "version": "3.8.4", + "commands": [ + "mgcb-editor-windows" + ] + }, + "dotnet-mgcb-editor-mac": { + "version": "3.8.4", + "commands": [ + "mgcb-editor-mac" + ] + } + } +} \ No newline at end of file diff --git a/MonoGameLibrary/Core.cs b/MonoGameLibrary/Core.cs new file mode 100644 index 0000000..0e031c4 --- /dev/null +++ b/MonoGameLibrary/Core.cs @@ -0,0 +1,91 @@ +using System; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; + +namespace MonoGameLibrary; + +public class Core : Game +{ + internal static Core s_instance; + + /// + /// Gets a reference to the Core instance. + /// + public static Core Instance => s_instance; + + /// + /// Gets the graphics device manager to control the presentation of graphics. + /// + public static GraphicsDeviceManager graphics { get; private set; } + + /// + /// Gets the graphics device used to create graphical resources and perform primitive rendering. + /// + public static new GraphicsDevice graphicsDevice { get; private set; } + + /// + /// Gets the sprite batch used for all 2D rendering. + /// + public static SpriteBatch spriteBatch { get; private set; } + + /// + /// Gets the content manager used to load global assets. + /// + public static new ContentManager content { get; private set; } + + /// + /// Creates a new Core instance. + /// + /// The title to display in the title bar of the game window. + /// The initial width, in pixels, of the game window. + /// The initial height, in pixels, of the game window. + /// Indicates if the game should start in fullscreen mode. + public Core(string title, int width, int height, bool fullScreen) + { + // Ensure that multiple cores are not created. + if (s_instance != null) + { + throw new InvalidOperationException($"Only a single Core instance can be created"); + } + + // Store reference to engine for global member access. + s_instance = this; + + // Create a new graphics device manager. + graphics = new GraphicsDeviceManager(this); + + // Set the graphics defaults. + graphics.PreferredBackBufferWidth = width; + graphics.PreferredBackBufferHeight = height; + graphics.IsFullScreen = fullScreen; + + // Apply the graphic presentation changes. + graphics.ApplyChanges(); + + // Set the window title. + Window.Title = title; + + // Set the core's content manager to a reference of the base Game's + // content manager. + content = base.Content; + + // Set the root directory for content. + content.RootDirectory = "Content"; + + // Mouse is visible by default. + IsMouseVisible = true; + } + + protected override void Initialize() + { + base.Initialize(); + + // Set the core's graphics device to a reference of the base Game's + // graphics device. + graphicsDevice = base.GraphicsDevice; + + // Create the sprite batch instance. + spriteBatch = new SpriteBatch(graphicsDevice); + } +} \ No newline at end of file diff --git a/MonoGameLibrary/Graphics/TextureAtlas.cs b/MonoGameLibrary/Graphics/TextureAtlas.cs new file mode 100644 index 0000000..65a271c --- /dev/null +++ b/MonoGameLibrary/Graphics/TextureAtlas.cs @@ -0,0 +1,137 @@ +using System.Collections.Generic; +using System.IO; +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 TextureRegion[] _regions; + + /// + /// Gets or Sets the source texture represented by this texture atlas. + /// + public Texture2D Texture { get; set; } + + // /// + // /// Creates a new texture atlas. + // /// + // public TextureAtlas() { + // _regions = new TextureRegion[0]; + // } + + /// + /// Creates a new texture atlas instance using the given texture. + /// + /// The source texture represented by the texture atlas. + public TextureAtlas(Texture2D texture, int spriteCount) { + Texture = texture; + _regions = new TextureRegion[spriteCount]; + } + + // /// + // /// Creates a new region and adds it to this texture atlas. + // /// + // /// The name to give the texture region. + // /// The top-left x-coordinate position of the region boundary relative to the top-left corner of the source texture boundary. + // /// The top-left y-coordinate position of the region boundary relative to the top-left corner of the source texture boundary. + // /// The width, in pixels, of the region. + // /// The height, in pixels, of the region. + // 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); + // } + + /// + /// Gets the region from this texture atlas with the specified name. + /// + /// The name of the region to retrieve. + /// The TextureRegion with the specified name. + + public TextureRegion this[int id] { + get => _regions[id]; + set => _regions[id] = value; + } + + // public TextureRegion GetRegion(string name) { + // return _regions[name]; + // } + + // /// + // /// Removes the region from this texture atlas with the specified name. + // /// + // /// The name of the region to remove. + // /// + // public bool RemoveRegion(string name) { + // return _regions.Remove(name); + // } + + // /// + // /// Removes all regions from this texture atlas. + // /// + // public void Clear() { + // _regions.Clear(); + // } + + /// + /// Creates a new texture atlas based a texture atlas xml configuration file. + /// + /// The content manager used to load the texture for the atlas. + /// The path to the xml file, relative to the content root directory. + /// The texture atlas created by this method. + public static TextureAtlas FromFile(ContentManager content, string fileName) { + TextureAtlas atlas; + + 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; + + // The 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(texturePath), spriteCount); + + // The element contains individual elements, each one describing + // a different texture region within the atlas. + // + // Example: + // + // + // + // + // + // So we retrieve all of the elements then loop through each one + // and generate a new TextureRegion instance from it and add it to this atlas. + var regions = root.Element("Regions")?.Elements("Region"); + + if (regions == null) return atlas; + + foreach (var region in regions) { + // int i = int.Parse(region.Attribute("id").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"); + + atlas[int.Parse(region.Attribute("id").Value)] = new TextureRegion( + atlas.Texture, + int.Parse(region.Attribute("x")?.Value ?? "0"), + int.Parse(region.Attribute("y")?.Value ?? "0"), + int.Parse(region.Attribute("width")?.Value ?? "0"), + int.Parse(region.Attribute("height")?.Value ?? "0")); + + } + + 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..3e1af3d --- /dev/null +++ b/MonoGameLibrary/Graphics/TextureRegion.cs @@ -0,0 +1,113 @@ +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() { } + + /// + /// Creates a new texture region using the specified source texture. + /// + /// The texture to use as the source texture for this texture region. + /// The x-coordinate position of the upper-left corner of this texture region relative to the upper-left corner of the source texture. + /// The y-coordinate position of the upper-left corner of this texture region relative to the upper-left corner of the source texture. + /// The width, in pixels, of this texture region. + /// The height, in pixels, of this texture region. + public TextureRegion(Texture2D texture, int x, int y, int width, int height) + { + Texture = texture; + SourceRectangle = new Rectangle(x, y, width, height); + } + + /// + /// Submit this texture region for drawing in the current batch. + /// + /// The spritebatch instance used for batching draw calls. + /// The xy-coordinate location to draw this texture region on the screen. + /// The color mask to apply when drawing this texture region on screen. + public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color) + { + Draw(spriteBatch, position, color, 0.0f, Vector2.Zero, Vector2.One, SpriteEffects.None, 0.0f); + } + + /// + /// Submit this texture region for drawing in the current batch. + /// + /// The spritebatch instance used for batching draw calls. + /// The xy-coordinate location to draw this texture region on the screen. + /// The color mask to apply when drawing this texture region on screen. + /// The amount of rotation, in radians, to apply when drawing this texture region on screen. + /// The center of rotation, scaling, and position when drawing this texture region on screen. + /// The scale factor to apply when drawing this texture region on screen. + /// Specifies if this texture region should be flipped horizontally, vertically, or both when drawing on screen. + /// The depth of the layer to use when drawing this texture region on screen. + 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 + ); + } + + /// + /// Submit this texture region for drawing in the current batch. + /// + /// The spritebatch instance used for batching draw calls. + /// The xy-coordinate location to draw this texture region on the screen. + /// The color mask to apply when drawing this texture region on screen. + /// The amount of rotation, in radians, to apply when drawing this texture region on screen. + /// The center of rotation, scaling, and position when drawing this texture region on screen. + /// The amount of scaling to apply to the x- and y-axes when drawing this texture region on screen. + /// Specifies if this texture region should be flipped horizontally, vertically, or both when drawing on screen. + /// The depth of the layer to use when drawing this texture region on screen. + 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 diff --git a/MonoGameLibrary/MonoGameLibrary.csproj b/MonoGameLibrary/MonoGameLibrary.csproj new file mode 100644 index 0000000..e28736b --- /dev/null +++ b/MonoGameLibrary/MonoGameLibrary.csproj @@ -0,0 +1,10 @@ + + + net8.0 + + + + All + + + \ No newline at end of file