TilemapContent.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #region License
  2. // Copyright 2021 Kastellanos Nikolaos
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Collections.Generic;
  18. using Microsoft.Xna.Framework;
  19. using Microsoft.Xna.Framework.Content.Pipeline;
  20. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  21. namespace nkast.Aether.Content.Pipeline
  22. {
  23. public class TilemapContent : Texture2DContent
  24. {
  25. public Texture2DContent TextureAtlas { get { return this; } }
  26. public Texture2DContent TextureMap;
  27. public readonly Dictionary<string, TileContent> Tiles = new Dictionary<string, TileContent>();
  28. public TilesetContent Tileset;
  29. internal readonly List<TileContent> DestinationTiles = new List<TileContent>();
  30. internal int MapColumns, MapRows;
  31. internal int TileWidth, TileHeight;
  32. internal int Width, Height;
  33. internal string Renderorder;
  34. internal int Firstgid;
  35. internal int LayerColumns, LayerRows;
  36. internal int[] MapData;
  37. internal static void PackTiles(TilemapContent output, int tileWidth, int tileHeight)
  38. {
  39. IList<TileContent> dstTiles = TilePacker.ArrangeGlyphs(output.Tileset.SourceTiles, tileWidth, tileHeight, true, true);
  40. foreach (TileContent dstTile in dstTiles)
  41. {
  42. output.DestinationTiles.Add(dstTile);
  43. string name = dstTile.SrcTexture.Name;
  44. if (output.Tiles.ContainsKey(name))
  45. name = name + dstTile.Id;
  46. output.Tiles.Add(name, dstTile);
  47. }
  48. }
  49. internal static void RenderAtlas(TilemapContent output)
  50. {
  51. Rectangle s = new Rectangle(0,0,1,1);
  52. foreach (TileContent dstTile in output.DestinationTiles)
  53. {
  54. s = Rectangle.Union(s,dstTile.DstBounds);
  55. }
  56. var outputBmp = new PixelBitmapContent<Color>(s.Width, s.Height);
  57. foreach (TileContent dstTile in output.DestinationTiles)
  58. {
  59. Rectangle srcBounds = dstTile.SrcBounds;
  60. Rectangle dstBounds = new Rectangle(dstTile.DstBounds.X, dstTile.DstBounds.Y, srcBounds.Width, srcBounds.Height);
  61. int offsetX = 0;
  62. int offsetY = dstTile.DstBounds.Height - srcBounds.Height;
  63. dstBounds.X += offsetX;
  64. dstBounds.Y += offsetY;
  65. BitmapContent srcBmp = dstTile.SrcTexture.Faces[0][0];
  66. BitmapContent.Copy(srcBmp, srcBounds, outputBmp, dstBounds);
  67. }
  68. MipmapChain mipmapChain = new MipmapChain(outputBmp);
  69. output.TextureAtlas.Mipmaps = mipmapChain;
  70. }
  71. internal static void RenderMap(TilemapContent output)
  72. {
  73. byte[] mp = new byte[output.MapData.Length * 4];
  74. for (int i = 0; i < output.MapData.Length; i++)
  75. {
  76. int id = output.MapData[i] - output.Firstgid;
  77. TileContent tile = null;
  78. foreach (TileContent t in output.DestinationTiles)
  79. {
  80. if (t.Id == id)
  81. {
  82. tile = t;
  83. break;
  84. }
  85. }
  86. byte x = (byte)(tile.DstBounds.X / output.TileWidth);
  87. byte y = (byte)(tile.DstBounds.Y / output.TileHeight);
  88. mp[i * 4 + 0] = x;
  89. mp[i * 4 + 1] = y;
  90. mp[i * 4 + 2] = 0;
  91. mp[i * 4 + 3] = 255;
  92. }
  93. BitmapContent bm = new PixelBitmapContent<Color>(output.MapColumns, output.MapRows);
  94. bm.SetPixelData(mp);
  95. Texture2DContent mt = new Texture2DContent();
  96. mt.Faces[0].Add(bm);
  97. output.TextureMap = mt;
  98. }
  99. }
  100. }