TilemapReader.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Microsoft.Xna.Framework;
  18. using Microsoft.Xna.Framework.Content;
  19. using Microsoft.Xna.Framework.Graphics;
  20. using tainicom.Aether.Graphics;
  21. using tainicom.Aether.Shaders;
  22. namespace tainicom.Aether.Graphics.Content
  23. {
  24. public class TilemapReader : ContentTypeReader<Tilemap>
  25. {
  26. protected override Tilemap Read(ContentReader input, Tilemap existingInstance)
  27. {
  28. IGraphicsDeviceService graphicsDeviceService = (IGraphicsDeviceService)input.ContentManager.ServiceProvider.GetService(typeof(IGraphicsDeviceService));
  29. var device = graphicsDeviceService.GraphicsDevice;
  30. Tilemap output = existingInstance ?? new Tilemap();
  31. // read standard Texture2D
  32. output.TextureAtlas = ReadTexture2D(input, output.TextureAtlas);
  33. output.TextureMap = ReadTexture2D(input, output.TextureMap);
  34. // read Sprites
  35. var count = input.ReadInt32();
  36. for (int i = 0; i < count; i++)
  37. {
  38. var name = input.ReadString();
  39. var bounds = new Rectangle(input.ReadInt32(), input.ReadInt32(), input.ReadInt32(), input.ReadInt32());
  40. output.Sprites[name] = new Tile(output.TextureAtlas, bounds);
  41. }
  42. var tilemapEffect = new TilemapEffect(device);
  43. tilemapEffect.Alpha = 1f;
  44. tilemapEffect.VertexColorEnabled = true;
  45. tilemapEffect.DiffuseColor = new Vector3(1f, 1f, 1f);
  46. tilemapEffect.World = Matrix.Identity;
  47. tilemapEffect.View = Matrix.Identity;
  48. tilemapEffect.Projection = Matrix.Identity;
  49. tilemapEffect.Texture = output.TextureMap;
  50. tilemapEffect.TextureAtlas = output.TextureAtlas;
  51. tilemapEffect.MapSize = new Vector2(output.TextureMap.Width, output.TextureMap.Height);
  52. tilemapEffect.AtlasSize = new Vector2(
  53. output.TextureAtlas.Width / output.Sprites["null"].Bounds.Width,
  54. output.TextureAtlas.Height / output.Sprites["null"].Bounds.Height);
  55. output.Effect = tilemapEffect;
  56. return output;
  57. }
  58. private Texture2D ReadTexture2D(ContentReader input, Texture2D existingInstance)
  59. {
  60. Texture2D output = null;
  61. try
  62. {
  63. output = input.ReadRawObject<Texture2D>(existingInstance);
  64. }
  65. catch(NotSupportedException)
  66. {
  67. var assembly = typeof(Microsoft.Xna.Framework.Content.ContentTypeReader).Assembly;
  68. var texture2DReaderType = assembly.GetType("Microsoft.Xna.Framework.Content.Texture2DReader");
  69. var texture2DReader = (ContentTypeReader)Activator.CreateInstance(texture2DReaderType, true);
  70. output = input.ReadRawObject<Texture2D>(texture2DReader, existingInstance);
  71. }
  72. return output;
  73. }
  74. }
  75. }