TilemapReader.cs 3.5 KB

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