TextureBuffer.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. namespace OpenVIII
  9. {
  10. public class TextureBuffer : Texture_Base, ICloneable, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
  11. {
  12. #region Fields
  13. private Color[] colors;
  14. #endregion Fields
  15. #region Constructors
  16. public TextureBuffer(int width, int height, bool alert = true)
  17. {
  18. Height = height;
  19. Width = width;
  20. colors = new Color[height * width];
  21. Alert = alert;
  22. }
  23. #endregion Constructors
  24. #region Properties
  25. public bool Alert { get; set; }
  26. public Color[] Colors { get => colors; private set => colors = value; }
  27. public int Height { get; private set; }
  28. public int Length => colors?.Length ?? 0;
  29. public int Width { get; private set; }
  30. public override int GetClutCount => 0;
  31. public override byte GetBytesPerPixel => 4;
  32. public override int GetClutSize => 0;
  33. public override int GetColorsCountPerPalette => 0;
  34. public override int GetHeight => Height;
  35. public override int GetOrigX => 0;
  36. public override int GetOrigY => 0;
  37. public override int GetWidth => Width;
  38. public int Count => ((ICollection)colors).Count;
  39. public object SyncRoot => colors.SyncRoot;
  40. public bool IsSynchronized => colors.IsSynchronized;
  41. #endregion Properties
  42. #region Indexers
  43. public Color this[int i]
  44. {
  45. get => colors[i]; set
  46. {
  47. if (Alert && colors[i] != Color.TransparentBlack)
  48. throw new Exception("Color is set!");
  49. colors[i] = value;
  50. }
  51. }
  52. public Color this[int x, int y]
  53. {
  54. get => colors[x + (y * Width)]; set => this[x + (y * Width)] = value;
  55. }
  56. public Color[] this[Rectangle rectangle]
  57. {
  58. get => this[rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height];
  59. set => this[rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height] = value;
  60. }
  61. public Color[] this[int x, int y, int width, int height]
  62. {
  63. get
  64. {
  65. int pos = (x + y * Width);
  66. List<Color> r = new List<Color>(width * height);
  67. int row = 0;
  68. while (r.Count < width * height)
  69. {
  70. r.AddRange(colors.Skip(pos + row * Width).Take(width));
  71. row++;
  72. }
  73. return r.ToArray();
  74. }
  75. set
  76. {
  77. for (int _y = y; (_y - y) < height; _y++)
  78. for (int _x = x; (_x - x) < width; _x++)
  79. {
  80. int pos = (_x + _y * Width);
  81. colors[pos] = value[(_x - x) + (_y - y) * width];
  82. }
  83. }
  84. }
  85. #endregion Indexers
  86. #region Methods
  87. public static explicit operator Texture2D(TextureBuffer @in)
  88. {
  89. if (Memory.graphics?.GraphicsDevice != null)
  90. {
  91. Texture2D tex = new Texture2D(Memory.graphics.GraphicsDevice, @in.Width, @in.Height);
  92. @in.SetData(tex);
  93. return tex;
  94. }
  95. return null;
  96. }
  97. public static explicit operator Texture2DWrapper(TextureBuffer @in)
  98. {
  99. return new Texture2DWrapper(@in.GetTexture());
  100. }
  101. public static explicit operator TextureBuffer(Texture2D @in)
  102. {
  103. TextureBuffer texture = new TextureBuffer(@in.Width, @in.Height);
  104. @in.GetData(texture.colors);
  105. return texture;
  106. }
  107. public static explicit operator TextureBuffer(Texture2DWrapper @in)
  108. {
  109. TextureBuffer texture = new TextureBuffer(@in.GetWidth, @in.GetHeight);
  110. var tex = @in.GetTexture();
  111. tex.GetData(texture.colors);
  112. return texture;
  113. }
  114. public static implicit operator Color[] (TextureBuffer @in) => @in.colors;
  115. public void SetData(Texture2D tex) => tex.SetData(colors);
  116. public override void ForceSetClutColors(ushort newNumOfColours)
  117. {
  118. }
  119. public override void ForceSetClutCount(ushort newClut)
  120. {
  121. }
  122. public override Color[] GetClutColors(ushort clut) => null;
  123. public override Texture2D GetTexture() => (Texture2D)this;
  124. public override Texture2D GetTexture(Color[] colors = null) => (Texture2D)this;
  125. public override Texture2D GetTexture(ushort? clut = null) => (Texture2D)this;
  126. public override void Save(string path)
  127. {
  128. using (Texture2D tex = GetTexture())
  129. using (FileStream fs = File.Create(path))
  130. tex.SaveAsPng(fs, tex.Width, tex.Height);
  131. }
  132. public override void SaveCLUT(string path)
  133. {
  134. }
  135. public override void Load(byte[] buffer, uint offset = 0) => throw new NotImplementedException();
  136. public bool Equals(object other, IEqualityComparer comparer) => ((IStructuralEquatable)colors).Equals(other, comparer);
  137. public int GetHashCode(IEqualityComparer comparer) => ((IStructuralEquatable)colors).GetHashCode(comparer);
  138. public int CompareTo(object other, IComparer comparer) => ((IStructuralComparable)colors).CompareTo(other, comparer);
  139. public IEnumerator GetEnumerator() => colors.GetEnumerator();
  140. public void CopyTo(Array array, int index) => colors.CopyTo(array, index);
  141. public object Clone() => colors.Clone();
  142. #endregion Methods
  143. }
  144. }