mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-25 08:22:15 +00:00
one tiny tweak to rule them all
This commit is contained in:
@@ -62,19 +62,32 @@ namespace MonoGame.Extended.Tiled.Graphics
|
||||
var vertices = new List<VertexPositionTexture>();
|
||||
var indices = new List<ushort>();
|
||||
|
||||
foreach (var tile in tileLayer.Tiles)
|
||||
foreach (var tile in tileLayer.Tiles.Where(t => tileset.ContainsGlobalIdentifier(t.GlobalIdentifier)))
|
||||
{
|
||||
var tileGid = tile.GlobalIdentifier;
|
||||
var localTileIdentifier = tileGid - tileset.FirstGlobalIdentifier;
|
||||
var position = GetTilePosition(_map, tile);
|
||||
var sourceRectangle = TiledMapHelper.GetTileSourceRectangle(localTileIdentifier, tileset.TileWidth, tileset.TileHeight, tileset.Columns, tileset.Margin, tileset.Spacing);
|
||||
var tilesetColumns = tileset.Columns == 0 ? 1 : tileset.Columns; // fixes a problem (what problem exactly?)
|
||||
var sourceRectangle = TiledMapHelper.GetTileSourceRectangle(localTileIdentifier, tileset.TileWidth, tileset.TileHeight, tilesetColumns, tileset.Margin, tileset.Spacing);
|
||||
var flipFlags = tile.Flags;
|
||||
|
||||
AddTileIndices(indices, vertices.Count);
|
||||
AddTileVertices(vertices, texture, position, sourceRectangle, flipFlags);
|
||||
indices.AddRange(CreateTileIndices(vertices.Count));
|
||||
Debug.Assert(indices.Count <= TiledMapHelper.MaximumIndicesPerModel);
|
||||
|
||||
vertices.AddRange(CreateVertices(texture, position, sourceRectangle, flipFlags));
|
||||
Debug.Assert(vertices.Count <= TiledMapHelper.MaximumVerticesPerModel);
|
||||
|
||||
// check if the current model is full
|
||||
if (vertices.Count + TiledMapHelper.VerticesPerTile >= TiledMapHelper.MaximumVerticesPerModel)
|
||||
{
|
||||
layerModels.Add(new TiledMapLayerModelNew(_graphicsDevice, texture, vertices.ToArray(), indices.ToArray()));
|
||||
vertices = new List<VertexPositionTexture>();
|
||||
indices = new List<ushort>();
|
||||
}
|
||||
}
|
||||
|
||||
layerModels.Add(new TiledMapLayerModelNew(_graphicsDevice, texture, vertices.ToArray(), indices.ToArray()));
|
||||
if (vertices.Any())
|
||||
layerModels.Add(new TiledMapLayerModelNew(_graphicsDevice, texture, vertices.ToArray(), indices.ToArray()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,7 +109,7 @@ namespace MonoGame.Extended.Tiled.Graphics
|
||||
}
|
||||
}
|
||||
|
||||
public void AddTileVertices(List<VertexPositionTexture> vertices, Texture2D texture, Point2 position, Rectangle sourceRectangle, TiledMapTileFlipFlags flags = TiledMapTileFlipFlags.None)
|
||||
private static IEnumerable<VertexPositionTexture> CreateVertices(Texture2D texture, Point2 position, Rectangle sourceRectangle, TiledMapTileFlipFlags flags = TiledMapTileFlipFlags.None)
|
||||
{
|
||||
var reciprocalWidth = 1f / texture.Width;
|
||||
var reciprocalHeight = 1f / texture.Height;
|
||||
@@ -162,28 +175,20 @@ namespace MonoGame.Extended.Tiled.Graphics
|
||||
}
|
||||
}
|
||||
|
||||
vertices.Add(vertexTopLeft);
|
||||
vertices.Add(vertexTopRight);
|
||||
vertices.Add(vertexBottomLeft);
|
||||
vertices.Add(vertexBottomRight);
|
||||
|
||||
Debug.Assert(vertices.Count <= TiledMapHelper.MaximumVerticesPerModel);
|
||||
yield return vertexTopLeft;
|
||||
yield return vertexTopRight;
|
||||
yield return vertexBottomLeft;
|
||||
yield return vertexBottomRight;
|
||||
}
|
||||
|
||||
public void AddTileIndices(List<ushort> indices, int indexOffset)
|
||||
private static IEnumerable<ushort> CreateTileIndices(int indexOffset)
|
||||
{
|
||||
//var indexOffset = Vertices.Count;
|
||||
|
||||
Debug.Assert(3 + indexOffset <= TiledMapHelper.MaximumVerticesPerModel);
|
||||
|
||||
indices.Add((ushort)(0 + indexOffset));
|
||||
indices.Add((ushort)(1 + indexOffset));
|
||||
indices.Add((ushort)(2 + indexOffset));
|
||||
indices.Add((ushort)(1 + indexOffset));
|
||||
indices.Add((ushort)(3 + indexOffset));
|
||||
indices.Add((ushort)(2 + indexOffset));
|
||||
|
||||
Debug.Assert(indices.Count <= TiledMapHelper.MaximumIndicesPerModel);
|
||||
yield return (ushort)(0 + indexOffset);
|
||||
yield return (ushort)(1 + indexOffset);
|
||||
yield return (ushort)(2 + indexOffset);
|
||||
yield return (ushort)(1 + indexOffset);
|
||||
yield return (ushort)(3 + indexOffset);
|
||||
yield return (ushort)(2 + indexOffset);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended.Tiled
|
||||
{
|
||||
public sealed class TiledMap : IDisposable
|
||||
public sealed class TiledMap //: IDisposable
|
||||
{
|
||||
private readonly List<TiledMapImageLayer> _imageLayers = new List<TiledMapImageLayer>();
|
||||
private readonly List<TiledMapLayer> _layers = new List<TiledMapLayer>();
|
||||
@@ -55,11 +55,11 @@ namespace MonoGame.Extended.Tiled
|
||||
BackgroundColor = backgroundColor;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var layer in _layers)
|
||||
layer.Dispose();
|
||||
}
|
||||
//public void Dispose()
|
||||
//{
|
||||
// foreach (var layer in _layers)
|
||||
// layer.Dispose();
|
||||
//}
|
||||
|
||||
internal void AddTileset(TiledMapTileset tileset)
|
||||
{
|
||||
|
||||
@@ -18,8 +18,7 @@ namespace MonoGame.Extended.Tiled
|
||||
// these optimal maximum numbers of course are not considering texture bindings which would practically lower the actual number of tiles per vertex / index buffer
|
||||
// thus, the reason why it is a good to have ONE giant tileset (at least per layer)
|
||||
|
||||
internal static Rectangle GetTileSourceRectangle(int localTileIdentifier, int tileWidth, int tileHeight,
|
||||
int columns, int margin, int spacing)
|
||||
internal static Rectangle GetTileSourceRectangle(int localTileIdentifier, int tileWidth, int tileHeight, int columns, int margin, int spacing)
|
||||
{
|
||||
var x = margin + localTileIdentifier % columns * (tileWidth + spacing);
|
||||
var y = margin + localTileIdentifier / columns * (tileHeight + spacing);
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using MonoGame.Extended.Tiled.Graphics;
|
||||
|
||||
namespace MonoGame.Extended.Tiled
|
||||
{
|
||||
public abstract class TiledMapLayer : IDisposable
|
||||
public abstract class TiledMapLayer// : IDisposable
|
||||
{
|
||||
internal TiledMapLayerModel[] Models;
|
||||
//internal TiledMapLayerModel[] Models;
|
||||
//internal TiledMapLayerAnimatedModel[] AnimatedModels;
|
||||
|
||||
public string Name { get; }
|
||||
@@ -24,22 +22,22 @@ namespace MonoGame.Extended.Tiled
|
||||
Properties = new TiledMapProperties();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
//public void Dispose()
|
||||
//{
|
||||
// Dispose(true);
|
||||
// GC.SuppressFinalize(this);
|
||||
//}
|
||||
|
||||
protected virtual void Dispose(bool diposing)
|
||||
{
|
||||
if (!diposing)
|
||||
return;
|
||||
//protected virtual void Dispose(bool diposing)
|
||||
//{
|
||||
// if (!diposing)
|
||||
// return;
|
||||
|
||||
if (Models == null)
|
||||
return;
|
||||
// if (Models == null)
|
||||
// return;
|
||||
|
||||
foreach (var model in Models)
|
||||
model.Dispose();
|
||||
}
|
||||
// foreach (var model in Models)
|
||||
// model.Dispose();
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
{
|
||||
public struct TiledMapTile
|
||||
{
|
||||
public ushort X { get; }
|
||||
public ushort Y { get; }
|
||||
internal readonly uint GlobalTileIdentifierWithFlags;
|
||||
public readonly ushort X;
|
||||
public readonly ushort Y;
|
||||
public readonly uint GlobalTileIdentifierWithFlags;
|
||||
|
||||
public int GlobalIdentifier => (int)(GlobalTileIdentifierWithFlags & ~(uint)TiledMapTileFlipFlags.All);
|
||||
public bool IsFlippedHorizontally => (GlobalTileIdentifierWithFlags & (uint)TiledMapTileFlipFlags.FlipHorizontally) != 0;
|
||||
@@ -13,7 +13,7 @@
|
||||
public bool IsBlank => GlobalIdentifier == 0;
|
||||
public TiledMapTileFlipFlags Flags => (TiledMapTileFlipFlags)(GlobalTileIdentifierWithFlags & (uint)TiledMapTileFlipFlags.All);
|
||||
|
||||
internal TiledMapTile(uint globalTileIdentifierWithFlags, ushort x, ushort y)
|
||||
public TiledMapTile(uint globalTileIdentifierWithFlags, ushort x, ushort y)
|
||||
{
|
||||
GlobalTileIdentifierWithFlags = globalTileIdentifierWithFlags;
|
||||
X = x;
|
||||
|
||||
Reference in New Issue
Block a user