2
0

TextureSequence.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // TextureSequence.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using RobotGameData.Helper;
  17. using RobotGameData.Resource;
  18. #endregion
  19. namespace RobotGameData.ParticleSystem
  20. {
  21. /// <summary>
  22. /// a structure for the animation of consecutive textures.
  23. /// </summary>
  24. [Serializable]
  25. public class TextureSequence
  26. {
  27. #region Style
  28. [Flags]
  29. public enum Styles
  30. {
  31. None = 0x00000000,
  32. Repeat = 0x00000001,
  33. StaticTime = 0x00000002,
  34. Random = 0x00000004,
  35. FixedFrame = 0x00000008,
  36. }
  37. #endregion
  38. #region Fields
  39. // Persistent Members
  40. public string TextureFileName = String.Empty;
  41. public bool IsUseStaticTime = true;
  42. public bool IsRepeat = false;
  43. public bool IsRandomMode = false;
  44. public bool IsFixedFrameMode = false;
  45. public float FrameWidth = 0.0f; // Width rate (maximum is 1.0)
  46. public float FrameHeight = 0.0f; // Height rate (maximum is 1.0)
  47. public float StaticInterval = 0.016666666666666666666666666666667f; // 60 FPS
  48. public uint Count = 0;
  49. public uint StartIndex = 0;
  50. public List<float> TimeTable = null;
  51. // Volatile Members
  52. uint style = 0;
  53. int lineCount = 0;
  54. float frameWidthPixel = 0.0f;
  55. float frameHeightPixel = 0.0f;
  56. float rInterval = 0.0f;
  57. float widthFactor = 0.0f;
  58. float heightFactor = 0.0f;
  59. float duration = 0.0f;
  60. Texture2D texture = null;
  61. #endregion
  62. #region Properties
  63. public Texture2D Texture
  64. {
  65. get { return texture; }
  66. }
  67. public bool UseStaticTime
  68. {
  69. get { return IsUseStaticTime; }
  70. set
  71. {
  72. IsUseStaticTime = value;
  73. if (value)
  74. {
  75. style |= (uint)Styles.StaticTime;
  76. if (TimeTable == null)
  77. TimeTable = new List<float>();
  78. }
  79. else
  80. {
  81. style &= ~(uint)Styles.StaticTime;
  82. if (TimeTable != null)
  83. {
  84. TimeTable.Clear();
  85. TimeTable = null;
  86. }
  87. }
  88. }
  89. }
  90. #endregion
  91. /// <summary>
  92. /// Loads a texture.
  93. /// </summary>
  94. /// <param name="resourcePath">texture file name</param>
  95. public void LoadTexture(string resourcePath)
  96. {
  97. string resourceFullPath = Path.Combine(resourcePath, TextureFileName);
  98. GameResourceTexture2D resource =
  99. FrameworkCore.ResourceManager.LoadTexture(resourceFullPath);
  100. // Set to texture
  101. SetTexture( resource.Texture2D);
  102. }
  103. /// <summary>
  104. /// Set the texture.
  105. /// </summary>
  106. /// <param name="tex">texture</param>
  107. public void SetTexture(Texture2D tex)
  108. {
  109. texture = tex;
  110. CalcParams();
  111. }
  112. /// <summary>
  113. /// Initialize parameters.
  114. /// </summary>
  115. public void CalcParams()
  116. {
  117. if (texture == null)
  118. return;
  119. if (texture.Width == 0 || texture.Height == 0)
  120. return;
  121. if (StaticInterval == 0.0f)
  122. return;
  123. duration = StaticInterval * Count;
  124. rInterval = 1.0f / StaticInterval;
  125. frameWidthPixel = (float)texture.Width * FrameWidth;
  126. frameHeightPixel = (float)texture.Height * FrameHeight;
  127. lineCount = (int)((float)texture.Width / frameWidthPixel);
  128. widthFactor = frameWidthPixel / (float)texture.Width;
  129. heightFactor = frameHeightPixel / (float)texture.Height;
  130. bool val = IsUseStaticTime;
  131. UseStaticTime = val;
  132. }
  133. /// <summary>
  134. /// gets texture coordinates by the time flow.
  135. /// </summary>
  136. /// <param name="time">defined time</param>
  137. /// <param name="uv1">texture coordinates 1</param>
  138. /// <param name="uv2">texture coordinates 2</param>
  139. public void GetUV(float time, out Vector2 uv1, out Vector2 uv2)
  140. {
  141. uint x = 0, y = 0, idx = 0;
  142. float u = 0.0f, v = 0.0f;
  143. if (IsRepeat)
  144. time = HelperMath.CalculateModulo(time, duration);
  145. if (IsRandomMode)
  146. idx = (uint)HelperMath.Randomi() % Count;
  147. else
  148. {
  149. if (IsUseStaticTime)
  150. {
  151. // Calculate index by static interval time
  152. idx = (uint)(time * rInterval);
  153. if (idx >= Count)
  154. idx = Count - 1;
  155. }
  156. else
  157. {
  158. uint start = 0;
  159. uint end = Count - 1;
  160. // Calculate index by dynamic interval time
  161. do
  162. {
  163. idx = (start + end) / 2;
  164. if (TimeTable[(int)idx] > time)
  165. end = idx - 1;
  166. else
  167. start = idx + 1;
  168. } while (start < end);
  169. }
  170. }
  171. idx += StartIndex;
  172. y = idx / (uint)lineCount;
  173. v = y * heightFactor;
  174. x = idx % (uint)lineCount;
  175. u = x * widthFactor;
  176. uv1 = new Vector2(u, v);
  177. uv2 = new Vector2(u + widthFactor, v + heightFactor);
  178. }
  179. }
  180. }