This commit is contained in:
Perry-Man 2025-07-19 19:12:21 +02:00
commit 7366d5cb22
10 changed files with 429 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

13
.idea/.idea.MonoGameLibrary/.idea/.gitignore generated vendored Normal file
View file

@ -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

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

16
MonoGameLibrary.sln Normal file
View file

@ -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

View file

@ -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"
]
}
}
}

91
MonoGameLibrary/Core.cs Normal file
View file

@ -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;
/// <summary>
/// Gets a reference to the Core instance.
/// </summary>
public static Core Instance => s_instance;
/// <summary>
/// Gets the graphics device manager to control the presentation of graphics.
/// </summary>
public static GraphicsDeviceManager graphics { get; private set; }
/// <summary>
/// Gets the graphics device used to create graphical resources and perform primitive rendering.
/// </summary>
public static new GraphicsDevice graphicsDevice { get; private set; }
/// <summary>
/// Gets the sprite batch used for all 2D rendering.
/// </summary>
public static SpriteBatch spriteBatch { get; private set; }
/// <summary>
/// Gets the content manager used to load global assets.
/// </summary>
public static new ContentManager content { get; private set; }
/// <summary>
/// Creates a new Core instance.
/// </summary>
/// <param name="title">The title to display in the title bar of the game window.</param>
/// <param name="width">The initial width, in pixels, of the game window.</param>
/// <param name="height">The initial height, in pixels, of the game window.</param>
/// <param name="fullScreen">Indicates if the game should start in fullscreen mode.</param>
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);
}
}

View file

@ -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;
/// <summary>
/// Gets or Sets the source texture represented by this texture atlas.
/// </summary>
public Texture2D Texture { get; set; }
// /// <summary>
// /// Creates a new texture atlas.
// /// </summary>
// public TextureAtlas() {
// _regions = new TextureRegion[0];
// }
/// <summary>
/// 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) {
Texture = texture;
_regions = new TextureRegion[spriteCount];
}
// /// <summary>
// /// Creates a new region and adds it to this texture atlas.
// /// </summary>
// /// <param name="name">The name to give the texture region.</param>
// /// <param name="x">The top-left x-coordinate position of the region boundary relative to the top-left corner of the source texture boundary.</param>
// /// <param name="y">The top-left y-coordinate position of the region boundary relative to the top-left corner of the source texture boundary.</param>
// /// <param name="width">The width, in pixels, of the region.</param>
// /// <param name="height">The height, in pixels, of the region.</param>
// 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);
// }
/// <summary>
/// Gets the region from this texture atlas with the specified name.
/// </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 GetRegion(string name) {
// return _regions[name];
// }
// /// <summary>
// /// Removes the region from this texture atlas with the specified name.
// /// </summary>
// /// <param name="name">The name of the region to remove.</param>
// /// <returns></returns>
// public bool RemoveRegion(string name) {
// return _regions.Remove(name);
// }
// /// <summary>
// /// Removes all regions from this texture atlas.
// /// </summary>
// public void Clear() {
// _regions.Clear();
// }
/// <summary>
/// Creates a new texture atlas based a texture atlas xml configuration file.
/// </summary>
/// <param name="content">The content manager used to load the texture for the atlas.</param>
/// <param name="fileName">The path to the xml file, relative to the content root directory.</param>
/// <returns>The texture atlas created by this method.</returns>
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 <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);
// The <Regions> element contains individual <Region> elements, each one describing
// a different texture region within the atlas.
//
// Example:
// <Regions>
// <Region name="spriteOne" x="0" y="0" width="32" height="32" />
// <Region name="spriteTwo" x="32" y="0" width="32" height="32" />
// </Regions>
//
// So we retrieve all of the <Region> 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;
}
}
}
}

View file

@ -0,0 +1,113 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MonoGameLibrary.Graphics;
/// <summary>
/// Represents a rectangular region within a texture.
/// </summary>
public class TextureRegion
{
/// <summary>
/// Gets or Sets the source texture this texture region is part of.
/// </summary>
public Texture2D Texture { get; set; }
/// <summary>
/// Gets or Sets the source rectangle boundary of this texture region within the source texture.
/// </summary>
public Rectangle SourceRectangle { get; set; }
/// <summary>
/// Gets the width, in pixels, of this texture region.
/// </summary>
public int Width => SourceRectangle.Width;
/// <summary>
/// Gets the height, in pixels, of this texture region.
/// </summary>
public int Height => SourceRectangle.Height;
/// <summary>
/// Creates a new texture region.
/// </summary>
public TextureRegion() { }
/// <summary>
/// Creates a new texture region using the specified source texture.
/// </summary>
/// <param name="texture">The texture to use as the source texture for this texture region.</param>
/// <param name="x">The x-coordinate position of the upper-left corner of this texture region relative to the upper-left corner of the source texture.</param>
/// <param name="y">The y-coordinate position of the upper-left corner of this texture region relative to the upper-left corner of the source texture.</param>
/// <param name="width">The width, in pixels, of this texture region.</param>
/// <param name="height">The height, in pixels, of this texture region.</param>
public TextureRegion(Texture2D texture, int x, int y, int width, int height)
{
Texture = texture;
SourceRectangle = new Rectangle(x, y, width, height);
}
/// <summary>
/// Submit this texture region for drawing in the current batch.
/// </summary>
/// <param name="spriteBatch">The spritebatch instance used for batching draw calls.</param>
/// <param name="position">The xy-coordinate location to draw this texture region on the screen.</param>
/// <param name="color">The color mask to apply when drawing this texture region on screen.</param>
public void Draw(SpriteBatch spriteBatch, Vector2 position, Color color)
{
Draw(spriteBatch, position, color, 0.0f, Vector2.Zero, Vector2.One, SpriteEffects.None, 0.0f);
}
/// <summary>
/// Submit this texture region for drawing in the current batch.
/// </summary>
/// <param name="spriteBatch">The spritebatch instance used for batching draw calls.</param>
/// <param name="position">The xy-coordinate location to draw this texture region on the screen.</param>
/// <param name="color">The color mask to apply when drawing this texture region on screen.</param>
/// <param name="rotation">The amount of rotation, in radians, to apply when drawing this texture region on screen.</param>
/// <param name="origin">The center of rotation, scaling, and position when drawing this texture region on screen.</param>
/// <param name="scale">The scale factor to apply when drawing this texture region on screen.</param>
/// <param name="effects">Specifies if this texture region should be flipped horizontally, vertically, or both when drawing on screen.</param>
/// <param name="layerDepth">The depth of the layer to use when drawing this texture region on screen.</param>
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
);
}
/// <summary>
/// Submit this texture region for drawing in the current batch.
/// </summary>
/// <param name="spriteBatch">The spritebatch instance used for batching draw calls.</param>
/// <param name="position">The xy-coordinate location to draw this texture region on the screen.</param>
/// <param name="color">The color mask to apply when drawing this texture region on screen.</param>
/// <param name="rotation">The amount of rotation, in radians, to apply when drawing this texture region on screen.</param>
/// <param name="origin">The center of rotation, scaling, and position when drawing this texture region on screen.</param>
/// <param name="scale">The amount of scaling to apply to the x- and y-axes when drawing this texture region on screen.</param>
/// <param name="effects">Specifies if this texture region should be flipped horizontally, vertically, or both when drawing on screen.</param>
/// <param name="layerDepth">The depth of the layer to use when drawing this texture region on screen.</param>
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
);
}
}

View file

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.*">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>