render order complete

This commit is contained in:
Dylan Wilson
2015-08-07 21:50:21 +10:00
parent 2d478b5313
commit f317596b0d
8 changed files with 114 additions and 22 deletions
@@ -54,6 +54,7 @@
<Compile Include="Tiled\TiledMap.cs" />
<Compile Include="Tiled\TiledMapReader.cs" />
<Compile Include="Tiled\TiledMapRenderOrder.cs" />
<Compile Include="Tiled\TiledTile.cs" />
<Compile Include="Tiled\TiledTileSet.cs" />
<Compile Include="Graphics\ViewportAdapters\BoxingViewportAdapter.cs" />
<Compile Include="Graphics\ViewportAdapters\DefaultViewportAdapter.cs" />
+91 -16
View File
@@ -1,4 +1,6 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.Graphics;
@@ -11,10 +13,10 @@ namespace MonoGame.Extended.Tiled
Name = name;
Width = width;
Height = height;
_tiledMap = tiledMap;
_data = data;
_spriteBatch = new SpriteBatch(graphicsDevice);
_tiles = CreateTiles(data);
}
public string Name { get; private set; }
@@ -22,33 +24,106 @@ namespace MonoGame.Extended.Tiled
public int Height { get; private set; }
private readonly TiledMap _tiledMap;
private readonly int[] _data;
private readonly TiledTile[] _tiles;
private readonly SpriteBatch _spriteBatch;
public void Draw(Camera2D camera)
private TiledTile[] CreateTiles(int[] data)
{
_spriteBatch.Begin(sortMode: SpriteSortMode.Immediate, blendState: BlendState.AlphaBlend,
samplerState: SamplerState.PointClamp, transformMatrix: camera.GetViewMatrix());
var tiles = new TiledTile[data.Length];
var index = 0;
for (var y = 0; y < Height; y++)
{
for (var x = 0; x < Width; x++)
{
var id = _data[x + y * Width];
var region = _tiledMap.GetTileRegion(id);
tiles[x + y * Width] = new TiledTile(data[index], x, y);
index++;
}
}
if (region != null)
{
// not exactly sure why we need to compensate 1 pixel here. Could be a bug in MonoGame?
var tx = x * (_tiledMap.TileWidth - 1);
var ty = y * (_tiledMap.TileHeight - 1);
return tiles;
}
public void Draw(Camera2D camera)
{
var renderOrderFunction = GetRenderOrderFunction();
_spriteBatch.Begin(sortMode: SpriteSortMode.Immediate, blendState: BlendState.AlphaBlend,
samplerState: SamplerState.PointClamp, transformMatrix: camera.GetViewMatrix());
foreach (var tile in renderOrderFunction())
{
var region = _tiledMap.GetTileRegion(tile.Id);
if (region != null)
{
// not exactly sure why we need to compensate 1 pixel here. Could be a bug in MonoGame?
var tx = tile.X * (_tiledMap.TileWidth - 1);
var ty = tile.Y * (_tiledMap.TileHeight - 1);
_spriteBatch.Draw(region, new Rectangle(tx, ty, region.Width, region.Height), Color.White);
}
_spriteBatch.Draw(region, new Rectangle(tx, ty, region.Width, region.Height), Color.White);
}
}
_spriteBatch.End();
}
public TiledTile GetTile(int x, int y)
{
return _tiles[x + y * Width];
}
private Func<IEnumerable<TiledTile>> GetRenderOrderFunction()
{
switch (_tiledMap.RenderOrder)
{
case TiledMapRenderOrder.LeftDown:
return GetTilesLeftDown;
case TiledMapRenderOrder.LeftUp:
return GetTilesLeftUp;
case TiledMapRenderOrder.RightDown:
return GetTilesRightDown;
case TiledMapRenderOrder.RightUp:
return GetTilesRightUp;
}
throw new NotSupportedException(string.Format("{0} is not supported", _tiledMap.RenderOrder));
}
private IEnumerable<TiledTile> GetTilesRightDown()
{
for (var y = 0; y < Height; y++)
{
for (var x = 0; x < Width; x++)
yield return GetTile(x, y);
}
}
private IEnumerable<TiledTile> GetTilesRightUp()
{
for (var y = Height - 1; y >= 0; y--)
{
for (var x = 0; x < Width; x++)
yield return GetTile(x, y);
}
}
private IEnumerable<TiledTile> GetTilesLeftDown()
{
for (var y = 0; y < Height; y++)
{
for (var x = Width - 1; x >= 0; x--)
yield return GetTile(x, y);
}
}
private IEnumerable<TiledTile> GetTilesLeftUp()
{
for (var y = Height - 1; y >= 0; y--)
{
for (var x = Width - 1; x >= 0; x--)
yield return GetTile(x, y);
}
}
}
}
@@ -0,0 +1,16 @@
namespace MonoGame.Extended.Tiled
{
public class TiledTile
{
public TiledTile (int id, int x, int y)
{
Id = id;
X = x;
Y = y;
}
public int Id { get; private set; }
public int X { get; private set; }
public int Y { get; private set; }
}
}
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="left-down" width="3" height="3" tilewidth="25" tileheight="25" backgroundcolor="#a6527c" nextobjectid="1">
<tileset firstgid="1" name="test-tileset" tilewidth="32" tileheight="32" spacing="2" margin="2">
<image source="../../../../../Temp/test-tileset/test-tileset.png" width="104" height="104"/>
<image source="test-tileset.png" width="104" height="104"/>
</tileset>
<layer name="Tile Layer 1" width="3" height="3">
<data>
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="left-up" width="3" height="3" tilewidth="25" tileheight="25" backgroundcolor="#a6527c" nextobjectid="1">
<tileset firstgid="1" name="test-tileset" tilewidth="32" tileheight="32" spacing="2" margin="2">
<image source="../../../../../Temp/test-tileset/test-tileset.png" width="104" height="104"/>
<image source="test-tileset.png" width="104" height="104"/>
</tileset>
<layer name="Tile Layer 1" width="3" height="3">
<data>
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="3" height="3" tilewidth="25" tileheight="25" backgroundcolor="#a6527c" nextobjectid="1">
<tileset firstgid="1" name="test-tileset" tilewidth="32" tileheight="32" spacing="2" margin="2">
<image source="../../../../../Temp/test-tileset/test-tileset.png" width="104" height="104"/>
<image source="test-tileset.png" width="104" height="104"/>
</tileset>
<layer name="Tile Layer 1" width="3" height="3">
<data>
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-up" width="3" height="3" tilewidth="25" tileheight="25" backgroundcolor="#a6527c" nextobjectid="1">
<tileset firstgid="1" name="test-tileset" tilewidth="32" tileheight="32" spacing="2" margin="2">
<image source="../../../../../Temp/test-tileset/test-tileset.png" width="104" height="104"/>
<image source="test-tileset.png" width="104" height="104"/>
</tileset>
<layer name="Tile Layer 1" width="3" height="3">
<data>
+2 -2
View File
@@ -44,7 +44,7 @@ namespace Sandbox
_camera = new Camera2D(_viewportAdapter)
{
Zoom = 1.5f,
//Position = new Vector2(900, 650)
Position = new Vector2(-200, -200)
};
Window.AllowUserResizing = true;
@@ -58,7 +58,7 @@ namespace Sandbox
_spriteBatch = new SpriteBatch(GraphicsDevice);
_backgroundTexture = Content.Load<Texture2D>("hills");
_bitmapFont = Content.Load<BitmapFont>("courier-new-32");
_tiledMap = Content.Load<TiledMap>("test-tileset-left-down");
_tiledMap = Content.Load<TiledMap>("test-tileset-right-down");
}
protected override void UnloadContent()