From 1ed6b019bb5f1b793422c5bce9b94423e64a85fd Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 3 Dec 2021 20:59:56 +0100 Subject: [PATCH] separate background and overlay regions in DrawExtendedAutoTile --- Demos/AutoTilingDemo.cs | 5 ++++- MLEM/Graphics/AutoTiling.cs | 33 ++++++++++++++++++--------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/Demos/AutoTilingDemo.cs b/Demos/AutoTilingDemo.cs index 59d8a71..bbe3a0c 100644 --- a/Demos/AutoTilingDemo.cs +++ b/Demos/AutoTilingDemo.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MLEM.Extensions; using MLEM.Graphics; using MLEM.Startup; @@ -61,7 +62,9 @@ namespace Demos { AutoTiling.DrawAutoTile(this.SpriteBatch, new Vector2(x + 1, y + 1) * TileSize, this.texture, new Rectangle(0, 0, TileSize, TileSize), ConnectsTo, Color.White); // when drawing extended auto-tiles, the same rules apply, but the source texture layout is different - AutoTiling.DrawExtendedAutoTile(this.SpriteBatch, new Vector2(x + 8, y + 1) * TileSize, this.texture, new Rectangle(0, TileSize * 2, TileSize, TileSize), ConnectsTo, Color.White, Color.White); + var background = new Rectangle(0, TileSize * 2, TileSize, TileSize); + var overlay = background.OffsetCopy(new Point(TileSize, 0)); + AutoTiling.DrawExtendedAutoTile(this.SpriteBatch, new Vector2(x + 8, y + 1) * TileSize, this.texture, background, overlay, ConnectsTo, Color.White, Color.White); } } this.SpriteBatch.End(); diff --git a/MLEM/Graphics/AutoTiling.cs b/MLEM/Graphics/AutoTiling.cs index 7f86c4f..4b53450 100644 --- a/MLEM/Graphics/AutoTiling.cs +++ b/MLEM/Graphics/AutoTiling.cs @@ -63,10 +63,9 @@ namespace MLEM.Graphics { /// This method allows for a tiled texture to be drawn in an auto-tiling mode. /// This allows, for example, a grass patch on a tilemap to have nice looking edges that transfer over into a path without any hard edges between tiles. /// - /// This method is a more complex version of that overlays separate border textures on a base texture, which also allows for non-rectangular texture areas to be used easily. - /// For auto-tiling in this way to work, the tiles have to be laid out as follows: 17 tiles aligned horizontally within the texture file, with the following information: + /// This method is a more complex version of that overlays separate border textures on a background texture region, which also allows for non-rectangular texture areas to be used easily. + /// For auto-tiling in this way to work, the overlay sections have to be laid out as follows: 16 sections aligned horizontally within the texture file, with the following information: /// - /// The texture used for filling big areas /// The texture used for straight, horizontal borders, with the borders facing away from the center, split up into four parts: top left, then top right, then bottom left, then bottom right /// The texture used for outer corners, with the corners facing away from the center, split up into four parts: top left, then top right, then bottom left, then bottom right /// The texture used for straight, vertical borders, with the borders facing away from the center, split up into four parts: top left, then top right, then bottom left, then bottom right @@ -77,7 +76,8 @@ namespace MLEM.Graphics { /// The sprite batch to use for drawing. /// The position to draw at. /// The texture to use for drawing. - /// The location of the first texture region, as described in the summary. + /// The location of the background region, or null to skip drawing a background. + /// The location of the first overlay region, as described in the summary. /// A function that determines whether two positions should connect. /// The color to draw the texture used for filling big areas with. /// The color to draw border and corner textures with. @@ -85,12 +85,13 @@ namespace MLEM.Graphics { /// The scale to draw with. /// The layer depth to draw with. /// An optional depth offset from that the overlay should be drawn with - public static void DrawExtendedAutoTile(SpriteBatch batch, Vector2 pos, Texture2D texture, Rectangle textureRegion, ConnectsTo connectsTo, Color backgroundColor, Color overlayColor, Vector2? origin = null, Vector2? scale = null, float layerDepth = 0, float overlayDepthOffset = 0) { + public static void DrawExtendedAutoTile(SpriteBatch batch, Vector2 pos, Texture2D texture, Rectangle? backgroundRegion, Rectangle overlayRegion, ConnectsTo connectsTo, Color backgroundColor, Color overlayColor, Vector2? origin = null, Vector2? scale = null, float layerDepth = 0, float overlayDepthOffset = 0) { var orig = origin ?? Vector2.Zero; var sc = scale ?? Vector2.One; var od = layerDepth + overlayDepthOffset; - var (r1, r2, r3, r4) = CalculateExtendedAutoTile(pos, textureRegion, connectsTo, sc); - batch.Draw(texture, pos, textureRegion, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth); + var (r1, r2, r3, r4) = CalculateExtendedAutoTile(pos, overlayRegion, connectsTo, sc); + if (backgroundRegion != null) + batch.Draw(texture, pos, backgroundRegion, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth); if (r1 != Rectangle.Empty) batch.Draw(texture, pos, r1, overlayColor, 0, orig, sc, SpriteEffects.None, od); if (r2 != Rectangle.Empty) @@ -102,13 +103,15 @@ namespace MLEM.Graphics { } /// - public static void AddExtendedAutoTile(StaticSpriteBatch batch, Vector2 pos, Texture2D texture, Rectangle textureRegion, ConnectsTo connectsTo, Color backgroundColor, Color overlayColor, Vector2? origin = null, Vector2? scale = null, float layerDepth = 0, float overlayDepthOffset = 0, ICollection items = null) { + public static void AddExtendedAutoTile(StaticSpriteBatch batch, Vector2 pos, Texture2D texture, Rectangle? backgroundRegion, Rectangle overlayRegion, ConnectsTo connectsTo, Color backgroundColor, Color overlayColor, Vector2? origin = null, Vector2? scale = null, float layerDepth = 0, float overlayDepthOffset = 0, ICollection items = null) { var orig = origin ?? Vector2.Zero; var sc = scale ?? Vector2.One; var od = layerDepth + overlayDepthOffset; - var (r1, r2, r3, r4) = CalculateExtendedAutoTile(pos, textureRegion, connectsTo, sc); - var background = batch.Add(texture, pos, textureRegion, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth); - items?.Add(background); + var (r1, r2, r3, r4) = CalculateExtendedAutoTile(pos, overlayRegion, connectsTo, sc); + if (backgroundRegion != null) { + var background = batch.Add(texture, pos, backgroundRegion, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth); + items?.Add(background); + } if (r1 != Rectangle.Empty) { var o1 = batch.Add(texture, pos, r1, overlayColor, 0, orig, sc, SpriteEffects.None, od); items?.Add(o1); @@ -150,10 +153,10 @@ namespace MLEM.Graphics { var down = connectsTo(0, 1); var left = connectsTo(-1, 0); var right = connectsTo(1, 0); - var xUl = up && left ? connectsTo(-1, -1) ? -1 : 13 : left ? 1 : up ? 9 : 5; - var xUr = up && right ? connectsTo(1, -1) ? -1 : 14 : right ? 2 : up ? 10 : 6; - var xDl = down && left ? connectsTo(-1, 1) ? -1 : 15 : left ? 3 : down ? 11 : 7; - var xDr = down && right ? connectsTo(1, 1) ? -1 : 16 : right ? 4 : down ? 12 : 8; + var xUl = up && left ? connectsTo(-1, -1) ? -1 : 12 : left ? 0 : up ? 8 : 4; + var xUr = up && right ? connectsTo(1, -1) ? -1 : 13 : right ? 1 : up ? 9 : 5; + var xDl = down && left ? connectsTo(-1, 1) ? -1 : 14 : left ? 2 : down ? 10 : 6; + var xDr = down && right ? connectsTo(1, 1) ? -1 : 15 : right ? 3 : down ? 11 : 7; var (w, h) = textureRegion.Size; return ( xUl < 0 ? Rectangle.Empty : new Rectangle(textureRegion.X + xUl * w, textureRegion.Y, w, h),