mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-25 08:22:15 +00:00
Depth buffer issue really solved, code cleanup and some fixes
This commit is contained in:
@@ -156,7 +156,7 @@ namespace Demo.SpriteSheetAnimations
|
||||
GraphicsDevice.Clear(Color.CornflowerBlue);
|
||||
|
||||
_spriteBatch.Begin(transformMatrix: _camera.GetViewMatrix());
|
||||
_tiledMap.Draw(_spriteBatch, _camera);
|
||||
_tiledMap.Draw(_camera);
|
||||
_zombie.Draw(_spriteBatch);
|
||||
_spriteBatch.Draw(_fireballSprite);
|
||||
_spriteBatch.End();
|
||||
|
||||
@@ -1648,7 +1648,7 @@
|
||||
<imagelayer name="Camada de Imagem 2">
|
||||
<image source="montserrat-32_0.png"/>
|
||||
</imagelayer>
|
||||
<objectgroup name="Objects Layer 1" visible="0">
|
||||
<objectgroup name="Objects Layer 1">
|
||||
<properties>
|
||||
<property name="objLayerProp" value="true"/>
|
||||
</properties>
|
||||
|
||||
@@ -98,10 +98,8 @@ namespace Demo.TiledMaps
|
||||
{
|
||||
GraphicsDevice.Clear(_tiledMap.BackgroundColor ?? Color.Black);
|
||||
|
||||
_spriteBatch.Begin(transformMatrix: _camera.GetViewMatrix());
|
||||
|
||||
// you can draw the whole map all at once
|
||||
_spriteBatch.Draw(_tiledMap, _camera, gameTime: gameTime);
|
||||
_tiledMap.Draw(_camera, gameTime: gameTime);
|
||||
|
||||
// or you can have more control over drawing each individual layer
|
||||
//foreach (var layer in _tiledMap.Layers)
|
||||
@@ -110,8 +108,6 @@ namespace Demo.TiledMaps
|
||||
// _spriteBatch.Draw(layer, _camera);
|
||||
//}
|
||||
|
||||
_spriteBatch.End();
|
||||
|
||||
var textColor = Color.Black;
|
||||
_spriteBatch.Begin(samplerState: SamplerState.PointClamp, blendState: BlendState.AlphaBlend);
|
||||
_spriteBatch.DrawString(_bitmapFont, "WASD/Arrows: move", new Vector2(5, 32), textColor);
|
||||
|
||||
@@ -6,19 +6,6 @@ namespace MonoGame.Extended.Maps.Tiled
|
||||
{
|
||||
public static class SpriteBatchExtensions
|
||||
{
|
||||
public static void Draw(this SpriteBatch spriteBatch, TiledMap tiledMap, Camera2D camera, GameTime gameTime = null)
|
||||
{
|
||||
tiledMap.Draw(spriteBatch, camera, gameTime);
|
||||
}
|
||||
|
||||
public static void Draw(this SpriteBatch spriteBatch, TiledLayer layer, Rectangle? visibleRectangle = null, GameTime gameTime = null)
|
||||
{
|
||||
layer.Draw(spriteBatch, visibleRectangle, gameTime: gameTime);
|
||||
}
|
||||
|
||||
public static void Draw(this SpriteBatch spriteBatch, TiledLayer layer, Camera2D camera, GameTime gameTime = null)
|
||||
{
|
||||
layer.Draw(spriteBatch, camera.GetBoundingRectangle().ToRectangle(), gameTime: gameTime);
|
||||
}
|
||||
// The file remains here for future uses
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Extended.Shapes;
|
||||
using MonoGame.Extended.TextureAtlases;
|
||||
using MonoGame.Extended.Graphics;
|
||||
|
||||
namespace MonoGame.Extended.Maps.Tiled
|
||||
{
|
||||
@@ -45,8 +46,6 @@ namespace MonoGame.Extended.Maps.Tiled
|
||||
private IndexBuffer _tilesIndexBuffer;
|
||||
private VertexPositionTexture[] _tilesVertices;
|
||||
private short[] _tilesIndexes;
|
||||
private int _tilesPrimitivesCount;
|
||||
private int _tilesVerticesSoFar;
|
||||
|
||||
private readonly DepthStencilState _depthBufferState;
|
||||
private Matrix _worldMatrix;
|
||||
@@ -105,7 +104,7 @@ namespace MonoGame.Extended.Maps.Tiled
|
||||
var tileVertices = new List<VertexPositionTexture>();
|
||||
var tileIndexes = new List<short>();
|
||||
var tileLayers = _layers.OfType<TiledTileLayer>();
|
||||
var index = 0;
|
||||
var indexOffset = 0;
|
||||
foreach (var layer in tileLayers)
|
||||
{
|
||||
tileVertices.AddRange(layer.RenderVertices());
|
||||
@@ -113,22 +112,21 @@ namespace MonoGame.Extended.Maps.Tiled
|
||||
for (var i = 0; i < tilesCount; i++)
|
||||
{
|
||||
var thisTileIndexes = new short[6];
|
||||
thisTileIndexes[0] = (short)(4 * index);
|
||||
thisTileIndexes[1] = (short)(4 * index + 1);
|
||||
thisTileIndexes[2] = (short)(4 * index + 2);
|
||||
thisTileIndexes[3] = (short)(4 * index + 1);
|
||||
thisTileIndexes[4] = (short)(4 * index + 3);
|
||||
thisTileIndexes[5] = (short)(4 * index + 2);
|
||||
thisTileIndexes[0] = (short)(4 * indexOffset);
|
||||
thisTileIndexes[1] = (short)(4 * indexOffset + 1);
|
||||
thisTileIndexes[2] = (short)(4 * indexOffset + 2);
|
||||
thisTileIndexes[3] = (short)(4 * indexOffset + 1);
|
||||
thisTileIndexes[4] = (short)(4 * indexOffset + 3);
|
||||
thisTileIndexes[5] = (short)(4 * indexOffset + 2);
|
||||
tileIndexes.AddRange(thisTileIndexes);
|
||||
_tilesPrimitivesCount += 2;
|
||||
index++;
|
||||
indexOffset++;
|
||||
}
|
||||
}
|
||||
|
||||
_tilesVertices = tileVertices.ToArray();
|
||||
_tilesIndexes = tileIndexes.ToArray();
|
||||
_tilesVertexBuffer = new VertexBuffer(_graphicsDevice, typeof(VertexPositionTexture), tileVertices.Count, BufferUsage.WriteOnly);
|
||||
_tilesIndexBuffer = new IndexBuffer(_graphicsDevice, typeof(short), tileIndexes.Count, BufferUsage.WriteOnly);
|
||||
_tilesVertexBuffer = new VertexBuffer(_graphicsDevice, typeof(VertexPositionTexture), _tilesVertices.Length, BufferUsage.WriteOnly);
|
||||
_tilesIndexBuffer = new IndexBuffer(_graphicsDevice, typeof(short), _tilesIndexes.Length, BufferUsage.WriteOnly);
|
||||
_tilesVertexBuffer.SetData(_tilesVertices);
|
||||
_tilesIndexBuffer.SetData(_tilesIndexes);
|
||||
|
||||
@@ -178,7 +176,7 @@ namespace MonoGame.Extended.Maps.Tiled
|
||||
return _objectGroups.FirstOrDefault(i => i.Name == name);
|
||||
}
|
||||
|
||||
public void Draw(SpriteBatch spriteBatch, Camera2D camera, GameTime gameTime = null)
|
||||
public void Draw(Camera2D camera, GameTime gameTime = null)
|
||||
{
|
||||
_basicEffect.World = camera.GetViewMatrix();
|
||||
|
||||
@@ -187,26 +185,35 @@ namespace MonoGame.Extended.Maps.Tiled
|
||||
_graphicsDevice.DepthStencilState = _depthBufferState;
|
||||
_graphicsDevice.BlendState = BlendState.AlphaBlend;
|
||||
_graphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
|
||||
|
||||
// Images draw
|
||||
|
||||
var tilesIndexesSoFar = 0;
|
||||
|
||||
foreach (var pass in _basicEffect.CurrentTechnique.Passes)
|
||||
{
|
||||
foreach (var layer in _layers)
|
||||
{
|
||||
if (layer is TiledTileLayer)
|
||||
{
|
||||
_basicEffect.Texture = _tilesets[0].Texture;
|
||||
pass.Apply();
|
||||
|
||||
_graphicsDevice.DrawIndexedPrimitives(
|
||||
PrimitiveType.TriangleList,
|
||||
0,
|
||||
0,
|
||||
_tilesPrimitivesCount
|
||||
);
|
||||
var tileLayer = (TiledTileLayer)layer;
|
||||
var indexCount = tileLayer.NotBlankTilesCount * 6;
|
||||
var primitivesCount = tileLayer.NotBlankTilesCount * 2;
|
||||
if (tileLayer.IsVisible)
|
||||
{
|
||||
_basicEffect.Texture = _tilesets[0].Texture;
|
||||
pass.Apply();
|
||||
_graphicsDevice.DrawIndexedPrimitives(
|
||||
PrimitiveType.TriangleList,
|
||||
0,
|
||||
tilesIndexesSoFar,
|
||||
primitivesCount
|
||||
);
|
||||
}
|
||||
tilesIndexesSoFar += indexCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!layer.IsVisible)
|
||||
continue;
|
||||
var imageLayer = (TiledImageLayer)layer;
|
||||
_basicEffect.Texture = imageLayer.Texture;
|
||||
pass.Apply();
|
||||
|
||||
@@ -38,6 +38,8 @@ namespace MonoGame.Extended.Maps.Tiled
|
||||
|
||||
public VertexPositionTexture[] VerticesList => _verticesList;
|
||||
private VertexPositionTexture[] _verticesList;
|
||||
public int NotBlankTilesCount => _notBlankTilesCount;
|
||||
private int _notBlankTilesCount;
|
||||
|
||||
public IEnumerable<TiledTile> Tiles => _tiles;
|
||||
public int TileWidth => _map.TileWidth;
|
||||
@@ -57,6 +59,7 @@ namespace MonoGame.Extended.Maps.Tiled
|
||||
}
|
||||
}
|
||||
|
||||
_notBlankTilesCount = tiles.Where(x => x.Id != 0).Count();
|
||||
return tiles;
|
||||
}
|
||||
|
||||
@@ -80,19 +83,19 @@ namespace MonoGame.Extended.Maps.Tiled
|
||||
}
|
||||
var vertices = new VertexPositionTexture[4];
|
||||
vertices[0] = new VertexPositionTexture(
|
||||
new Vector3(tile.X * TileWidth - 1, tile.Y * TileHeight, Z),
|
||||
new Vector3(tile.X * TileWidth - 0.9f, tile.Y * TileHeight, Z),
|
||||
textureCoordinateTopLeft
|
||||
);
|
||||
vertices[1] = new VertexPositionTexture(
|
||||
new Vector3(tile.X * TileWidth + TileWidth + 1, tile.Y * TileHeight, Z),
|
||||
new Vector3(tile.X * TileWidth + TileWidth + 0.9f, tile.Y * TileHeight, Z),
|
||||
new Vector2(textureCoordinateBottomRight.X, textureCoordinateTopLeft.Y)
|
||||
);
|
||||
vertices[2] = new VertexPositionTexture(
|
||||
new Vector3(tile.X * TileWidth - 1, tile.Y * TileHeight + TileHeight + 1, Z),
|
||||
new Vector3(tile.X * TileWidth - 0.9f, tile.Y * TileHeight + TileHeight + 0.9f, Z),
|
||||
new Vector2(textureCoordinateTopLeft.X, textureCoordinateBottomRight.Y)
|
||||
);
|
||||
vertices[3] = new VertexPositionTexture(
|
||||
new Vector3(tile.X * TileWidth + TileWidth + 1, tile.Y * TileHeight + TileHeight + 1, Z),
|
||||
new Vector3(tile.X * TileWidth + TileWidth + 0.9f, tile.Y * TileHeight + TileHeight + 0.9f, Z),
|
||||
textureCoordinateBottomRight
|
||||
);
|
||||
verticesList.AddRange(vertices);
|
||||
|
||||
Reference in New Issue
Block a user