1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 22:18:34 +01:00

Compare commits

..

No commits in common. "42993f1a0bb79e03466903630904df83125a2d52" and "144062fa645803dbfdf2c4268880cc432309d5fe" have entirely different histories.

4 changed files with 7 additions and 215 deletions

View file

@ -49,13 +49,6 @@ Removals
- Marked old Draw and DrawTransformed overloads as obsolete in favor of SpriteBatchContext ones - Marked old Draw and DrawTransformed overloads as obsolete in favor of SpriteBatchContext ones
- Marked Tooltip.Paragraph as obsolete in favor of new Paragraphs collection - Marked Tooltip.Paragraph as obsolete in favor of new Paragraphs collection
### MLEM.Extended
Additions
- Added LayerPositionF
Improvements
- Allow using a StaticSpriteBatch to render an IndividualTiledMapRenderer
### MLEM.Data ### MLEM.Data
Additions Additions
- Added the ability to add padding to RuntimeTexturePacker texture regions - Added the ability to add padding to RuntimeTexturePacker texture regions

View file

@ -4,7 +4,6 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Cameras; using MLEM.Cameras;
using MLEM.Extensions; using MLEM.Extensions;
using MLEM.Graphics;
using MLEM.Misc; using MLEM.Misc;
using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled;
using RectangleF = MonoGame.Extended.RectangleF; using RectangleF = MonoGame.Extended.RectangleF;
@ -93,20 +92,6 @@ namespace MLEM.Extended.Tiled {
} }
} }
/// <summary>
/// Adds this individual tiled map renderer to the given <see cref="StaticSpriteBatch"/>.
/// Optionally, a frustum can be supplied that determines which positions, in pixel space, are visible at this time. <see cref="Camera"/> provides <see cref="Camera.GetVisibleRectangle"/> for this purpose.
/// </summary>
/// <param name="batch">The static sprite batch to use for drawing.</param>
/// <param name="frustum">The area that is visible, in pixel space.</param>
/// <param name="addFunction">The add function to use, or null to use <see cref="DefaultAdd"/>.</param>
public void Add(StaticSpriteBatch batch, RectangleF? frustum = null, AddDelegate addFunction = null) {
for (var i = 0; i < this.map.TileLayers.Count; i++) {
if (this.map.TileLayers[i].IsVisible)
this.AddLayer(batch, i, frustum, addFunction);
}
}
/// <summary> /// <summary>
/// Draws the given layer of this individual tiled map renderer. /// Draws the given layer of this individual tiled map renderer.
/// Optionally, a frustum can be supplied that determines which positions, in pixel space, are visible at this time. <see cref="Camera"/> provides <see cref="Camera.GetVisibleRectangle"/> for this purpose. /// Optionally, a frustum can be supplied that determines which positions, in pixel space, are visible at this time. <see cref="Camera"/> provides <see cref="Camera.GetVisibleRectangle"/> for this purpose.
@ -117,7 +102,11 @@ namespace MLEM.Extended.Tiled {
/// <param name="drawFunction">The draw function to use, or null to use <see cref="DefaultDraw"/></param> /// <param name="drawFunction">The draw function to use, or null to use <see cref="DefaultDraw"/></param>
public void DrawLayer(SpriteBatch batch, int layerIndex, RectangleF? frustum = null, DrawDelegate drawFunction = null) { public void DrawLayer(SpriteBatch batch, int layerIndex, RectangleF? frustum = null, DrawDelegate drawFunction = null) {
var draw = drawFunction ?? DefaultDraw; var draw = drawFunction ?? DefaultDraw;
var (minX, minY, maxX, maxY) = this.GetFrustum(frustum); var frust = frustum ?? new RectangleF(0, 0, float.MaxValue, float.MaxValue);
var minX = Math.Max(0, frust.Left / this.map.TileWidth).Floor();
var minY = Math.Max(0, frust.Top / this.map.TileHeight).Floor();
var maxX = Math.Min(this.map.Width, frust.Right / this.map.TileWidth).Ceil();
var maxY = Math.Min(this.map.Height, frust.Bottom / this.map.TileHeight).Ceil();
for (var x = minX; x < maxX; x++) { for (var x = minX; x < maxX; x++) {
for (var y = minY; y < maxY; y++) { for (var y = minY; y < maxY; y++) {
var info = this.drawInfos[layerIndex, x, y]; var info = this.drawInfos[layerIndex, x, y];
@ -127,26 +116,6 @@ namespace MLEM.Extended.Tiled {
} }
} }
/// <summary>
/// Adds the given layer of this individual tiled map renderer to the given <see cref="StaticSpriteBatch"/>.
/// Optionally, a frustum can be supplied that determines which positions, in pixel space, are visible at this time. <see cref="Camera"/> provides <see cref="Camera.GetVisibleRectangle"/> for this purpose.
/// </summary>
/// <param name="batch">The static sprite batch to use for drawing.</param>
/// <param name="layerIndex">The index of the layer in <see cref="TiledMap.TileLayers"/>.</param>
/// <param name="frustum">The area that is visible, in pixel space.</param>
/// <param name="addFunction">The add function to use, or null to use <see cref="DefaultAdd"/>.</param>
public void AddLayer(StaticSpriteBatch batch, int layerIndex, RectangleF? frustum = null, AddDelegate addFunction = null) {
var add = addFunction ?? DefaultAdd;
var (minX, minY, maxX, maxY) = this.GetFrustum(frustum);
for (var x = minX; x < maxX; x++) {
for (var y = minY; y < maxY; y++) {
var info = this.drawInfos[layerIndex, x, y];
if (info != null)
add(batch, info);
}
}
}
/// <summary> /// <summary>
/// Update all of the animated tiles in this individual tiled map renderer /// Update all of the animated tiles in this individual tiled map renderer
/// </summary> /// </summary>
@ -156,17 +125,8 @@ namespace MLEM.Extended.Tiled {
animation.Update(time); animation.Update(time);
} }
private (int MinX, int MinY, int MaxX, int MaxY) GetFrustum(RectangleF? frustum) {
var frust = frustum ?? new RectangleF(0, 0, float.MaxValue, float.MaxValue);
var minX = Math.Max(0, frust.Left / this.map.TileWidth).Floor();
var minY = Math.Max(0, frust.Top / this.map.TileHeight).Floor();
var maxX = Math.Min(this.map.Width, frust.Right / this.map.TileWidth).Ceil();
var maxY = Math.Min(this.map.Height, frust.Bottom / this.map.TileHeight).Ceil();
return (minX, minY, maxX, maxY);
}
/// <summary> /// <summary>
/// The default implementation of <see cref="DrawDelegate"/> that is used by <see cref="Draw"/> if no custom draw function is passed. /// The default implementation of <see cref="DrawDelegate"/> that is used by <see cref="SetMap"/> if no custom draw function is passed
/// </summary> /// </summary>
/// <param name="batch">The sprite batch to use for drawing</param> /// <param name="batch">The sprite batch to use for drawing</param>
/// <param name="info">The <see cref="TileDrawInfo"/> to draw</param> /// <param name="info">The <see cref="TileDrawInfo"/> to draw</param>
@ -177,18 +137,6 @@ namespace MLEM.Extended.Tiled {
batch.Draw(info.Tileset.Texture, drawPos, region, Color.White, 0, Vector2.Zero, 1, effects, info.Depth); batch.Draw(info.Tileset.Texture, drawPos, region, Color.White, 0, Vector2.Zero, 1, effects, info.Depth);
} }
/// <summary>
/// The default implementation of <see cref="AddDelegate"/> that is used by <see cref="Add"/> if no custom add function is passed.
/// </summary>
/// <param name="batch">The static sprite batch to use for drawing.</param>
/// <param name="info">The <see cref="TileDrawInfo"/> to add.</param>
public static void DefaultAdd(StaticSpriteBatch batch, TileDrawInfo info) {
var region = info.Tileset.GetTextureRegion(info.TilesetTile);
var effects = info.Tile.GetSpriteEffects();
var drawPos = new Vector2(info.Position.X * info.Renderer.map.TileWidth, info.Position.Y * info.Renderer.map.TileHeight);
batch.Add(info.Tileset.Texture, drawPos, region, Color.White, 0, Vector2.Zero, 1, effects, info.Depth);
}
/// <summary> /// <summary>
/// A delegate method used for <see cref="IndividualTiledMapRenderer.depthFunction"/>. /// A delegate method used for <see cref="IndividualTiledMapRenderer.depthFunction"/>.
/// The idea is to return a depth (between 0 and 1) for the given tile that determines where in the sprite batch it should be rendererd. /// The idea is to return a depth (between 0 and 1) for the given tile that determines where in the sprite batch it should be rendererd.
@ -207,13 +155,6 @@ namespace MLEM.Extended.Tiled {
/// <param name="info">The <see cref="TileDrawInfo"/> to draw</param> /// <param name="info">The <see cref="TileDrawInfo"/> to draw</param>
public delegate void DrawDelegate(SpriteBatch batch, TileDrawInfo info); public delegate void DrawDelegate(SpriteBatch batch, TileDrawInfo info);
/// <summary>
/// A delegate method used for adding an <see cref="IndividualTiledMapRenderer"/> to a <see cref="StaticSpriteBatch"/>.
/// </summary>
/// <param name="batch">The static sprite batch to use for drawing.</param>
/// <param name="info">The <see cref="TileDrawInfo"/> to add.</param>
public delegate void AddDelegate(StaticSpriteBatch batch, TileDrawInfo info);
/// <summary> /// <summary>
/// A tile draw info contains information about a tile at a given map location. /// A tile draw info contains information about a tile at a given map location.
/// It caches a lot of data that is required for drawing a tile efficiently. /// It caches a lot of data that is required for drawing a tile efficiently.

View file

@ -4,8 +4,7 @@ using MonoGame.Extended.Tiled;
namespace MLEM.Extended.Tiled { namespace MLEM.Extended.Tiled {
/// <summary> /// <summary>
/// A struct that represents a position on a <see cref="TiledMap"/> with multiple layers, where the <see cref="X"/> and <see cref="Y"/> coordinates are 32-bit integer numbers. /// A struct that represents a position on a <see cref="TiledMap"/> with multiple layers.
/// See <see cref="LayerPositionF"/> for a floating point position.
/// </summary> /// </summary>
[DataContract] [DataContract]
public struct LayerPosition : IEquatable<LayerPosition> { public struct LayerPosition : IEquatable<LayerPosition> {
@ -118,14 +117,5 @@ namespace MLEM.Extended.Tiled {
return Add(left, -right); return Add(left, -right);
} }
/// <summary>
/// Implicitly converts a <see cref="LayerPosition"/> to a <see cref="LayerPositionF"/>.
/// </summary>
/// <param name="position">The position to convert.</param>
/// <returns>The converted position.</returns>
public static implicit operator LayerPositionF(LayerPosition position) {
return new LayerPositionF(position.Layer, position.X, position.Y);
}
} }
} }

View file

@ -1,132 +0,0 @@
using System;
using System.Runtime.Serialization;
using MonoGame.Extended.Tiled;
namespace MLEM.Extended.Tiled {
/// <summary>
/// A struct that represents a position on a <see cref="TiledMap"/> with multiple layers, where the <see cref="X"/> and <see cref="Y"/> coordinates are 32-bit floating point numbers.
/// See <see cref="LayerPosition"/> for an integer position.
/// </summary>
[DataContract]
public struct LayerPositionF : IEquatable<LayerPositionF> {
/// <summary>
/// The name of the layer that this position is on
/// </summary>
[DataMember]
public string Layer;
/// <summary>
/// The x coordinate of this position
/// </summary>
[DataMember]
public float X;
/// <summary>
/// The y coordinate of this position
/// </summary>
[DataMember]
public float Y;
/// <summary>
/// Creates a new layer position with the given settings
/// </summary>
/// <param name="layerName">The layer name</param>
/// <param name="x">The x coordinate</param>
/// <param name="y">The y coordinate</param>
public LayerPositionF(string layerName, float x, float y) {
this.Layer = layerName;
this.X = x;
this.Y = y;
}
/// <inheritdoc cref="Equals(object)"/>
public bool Equals(LayerPositionF other) {
return this.Layer == other.Layer && this.X == other.X && this.Y == other.Y;
}
/// <summary>Indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param>
/// <returns>true if <paramref name="obj">obj</paramref> and this instance are the same type and represent the same value; otherwise, false.</returns>
public override bool Equals(object obj) {
return obj is LayerPositionF other && this.Equals(other);
}
/// <summary>Returns the hash code for this instance.</summary>
/// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
public override int GetHashCode() {
var hashCode = this.Layer.GetHashCode();
hashCode = (hashCode * 397) ^ this.X.GetHashCode();
hashCode = (hashCode * 397) ^ this.Y.GetHashCode();
return hashCode;
}
/// <summary>Returns the fully qualified type name of this instance.</summary>
/// <returns>The fully qualified type name.</returns>
public override string ToString() {
return $"{nameof(this.Layer)}: {this.Layer}, {nameof(this.X)}: {this.X}, {nameof(this.Y)}: {this.Y}";
}
/// <summary>
/// Adds the given layer positions together, returning a new layer position with the sum of their coordinates.
/// If the two layer positions' <see cref="Layer"/> differ, an <see cref="ArgumentException"/> is thrown.
/// </summary>
/// <param name="left">The left position.</param>
/// <param name="right">The right position.</param>
/// <returns>The sum of the positions.</returns>
/// <exception cref="ArgumentException">Thrown if the two positions' <see cref="Layer"/> are not the same.</exception>
public static LayerPositionF Add(LayerPositionF left, LayerPositionF right) {
if (left.Layer != right.Layer)
throw new ArgumentException("Cannot add layer positions on different layers");
return new LayerPositionF(left.Layer, left.X + right.X, left.Y + right.Y);
}
/// <inheritdoc cref="Equals(LayerPositionF)"/>
public static bool operator ==(LayerPositionF left, LayerPositionF right) {
return left.Equals(right);
}
/// <inheritdoc cref="Equals(LayerPositionF)"/>
public static bool operator !=(LayerPositionF left, LayerPositionF right) {
return !left.Equals(right);
}
/// <summary>
/// Returns the negative of the given layer position.
/// </summary>
/// <param name="position">The position to negate.</param>
/// <returns>The negative position.</returns>
public static LayerPositionF operator -(LayerPositionF position) {
return new LayerPositionF(position.Layer, -position.X, -position.Y);
}
/// <summary>
/// Returns the sum of the two layer positions using <see cref="Add"/>.
/// </summary>
/// <param name="left">The left position.</param>
/// <param name="right">The right position.</param>
/// <returns>The sum of the positions.</returns>
public static LayerPositionF operator +(LayerPositionF left, LayerPositionF right) {
return Add(left, right);
}
/// <summary>
/// Subtracts the second from the first position using <see cref="Add"/>.
/// </summary>
/// <param name="left">The left position.</param>
/// <param name="right">The right position.</param>
/// <returns>The difference of the positions.</returns>
public static LayerPositionF operator -(LayerPositionF left, LayerPositionF right) {
return Add(left, -right);
}
/// <summary>
/// Implicitly converts a <see cref="LayerPositionF"/> to a <see cref="LayerPosition"/>.
/// The coordinates are typecast to 32-bit integers in the process.
/// </summary>
/// <param name="position">The position to convert.</param>
/// <returns>The converted position.</returns>
public static implicit operator LayerPosition(LayerPositionF position) {
return new LayerPosition(position.Layer, (int) position.X, (int) position.Y);
}
}
}