ScaledSpriteBatch.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ScaledSpriteBatch.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.Text;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework;
  15. #endregion
  16. namespace HoneycombRush
  17. {
  18. /// <summary>
  19. /// Represents a spritebatch where all drawing operations are scaled by a specific non-uniform factor. If a draw
  20. /// operation specifies a specific scale factor, it will be multiplied by the default one.
  21. /// </summary>
  22. public class ScaledSpriteBatch : SpriteBatch
  23. {
  24. #region Properties
  25. /// <summary>
  26. /// Determines the scale factor for all drawing operations
  27. /// </summary>
  28. public Vector2 ScaleVector { get; set; }
  29. #endregion
  30. #region Initialization
  31. public ScaledSpriteBatch(GraphicsDevice graphicsDevice, Vector2 initialScale) : base(graphicsDevice)
  32. {
  33. ScaleVector = initialScale;
  34. }
  35. #endregion
  36. #region Draw overrides
  37. /// <summary>
  38. /// Adds a sprite to a batch of sprites for rendering using the specified texture,
  39. /// position and color. Reference page contains links to related code samples.
  40. /// </summary>
  41. /// <param name="texture">A texture.</param>
  42. /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
  43. /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
  44. /// <remarks>The drawing operation will be scaled according to the <see cref="ScaleVector"/>
  45. /// property.</remarks>
  46. public new void Draw(Texture2D texture, Vector2 position, Color color)
  47. {
  48. base.Draw(texture, position, null, color, 0, Vector2.Zero, ScaleVector, SpriteEffects.None, 0);
  49. }
  50. /// <summary>
  51. /// Adds a sprite to a batch of sprites for rendering using the specified texture,
  52. /// position, source rectangle, and color.
  53. /// </summary>
  54. /// <param name="texture">A texture.</param>
  55. /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
  56. /// <param name="sourceRectangle">A rectangle that specifies (in texels) the source texels from a texture.
  57. /// Use null to draw the entire texture.</param>
  58. /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
  59. /// <remarks>The drawing operation will be scaled according to the <see cref="ScaleVector"/>
  60. /// property.</remarks>
  61. public new void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color)
  62. {
  63. base.Draw(texture, position, sourceRectangle, color, 0, Vector2.Zero, ScaleVector, SpriteEffects.None, 0);
  64. }
  65. /// <summary>
  66. /// Adds a sprite to a batch of sprites for rendering using the specified texture,
  67. /// position, source rectangle, color, rotation, origin, scale, effects, and
  68. /// layer. Reference page contains links to related code samples.
  69. /// </summary>
  70. /// <param name="texture">A texture.</param>
  71. /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
  72. /// <param name="sourceRectangle">A rectangle that specifies (in texels) the source texels from a texture.
  73. /// Use null to draw the entire texture.</param>
  74. /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
  75. /// <param name="rotation">Specifies the angle (in radians) to rotate the sprite about its center.</param>
  76. /// <param name="origin">The sprite origin; the default is (0,0) which represents the
  77. /// upper-left corner.</param>
  78. /// <param name="scale">Scale factor.</param>
  79. /// <param name="effects">Effects to apply.</param>
  80. /// <param name="layerDepth">The depth of a layer. By default, 0 represents the front layer and 1 represents
  81. /// a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing.</param>
  82. /// <remarks>The drawing operation will be scaled according to the <see cref="ScaleVector"/>
  83. /// property.</remarks>
  84. public new void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color,
  85. float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
  86. {
  87. base.Draw(texture, position, sourceRectangle, color, rotation, origin, scale * ScaleVector, effects,
  88. layerDepth);
  89. }
  90. /// <summary>
  91. /// Adds a sprite to a batch of sprites for rendering using the specified texture,
  92. /// position, source rectangle, color, rotation, origin, scale, effects and layer.
  93. /// Reference page contains links to related code samples.
  94. /// </summary>
  95. /// <param name="texture">A texture.</param>
  96. /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
  97. /// <param name="sourceRectangle">A rectangle that specifies (in texels) the source texels from a texture.
  98. /// Use null to draw the entire texture.</param>
  99. /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
  100. /// <param name="rotation">Specifies the angle (in radians) to rotate the sprite about its center.</param>
  101. /// <param name="origin">The sprite origin; the default is (0,0) which represents the
  102. /// upper-left corner.</param>
  103. /// <param name="scale">Scale factor.</param>
  104. /// <param name="effects">Effects to apply.</param>
  105. /// <param name="layerDepth">The depth of a layer. By default, 0 represents the front layer and 1 represents
  106. /// a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing.</param>
  107. /// <remarks>The drawing operation will be scaled according to the <see cref="ScaleVector"/>
  108. /// property.</remarks>
  109. public new void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color,
  110. float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)
  111. {
  112. base.Draw(texture, position, sourceRectangle, color, rotation, origin, scale * ScaleVector, effects,
  113. layerDepth);
  114. }
  115. /// <summary>
  116. /// Adds a string to a batch of sprites for rendering using the specified font,
  117. /// text, position, and color.
  118. /// </summary>
  119. /// <param name="spriteFont">A font for diplaying text</param>
  120. /// <param name="text">A text string.</param>
  121. /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
  122. /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
  123. /// <remarks>The drawing operation will be scaled according to the <see cref="ScaleVector"/>
  124. /// property.</remarks>
  125. public new void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color)
  126. {
  127. base.DrawString(spriteFont, text, position, color, 0, Vector2.Zero, ScaleVector, SpriteEffects.None, 0);
  128. }
  129. /// <summary>
  130. /// Adds a string to a batch of sprites for rendering using the specified font,
  131. /// text, position, and color.
  132. /// </summary>
  133. /// <param name="spriteFont">A font for diplaying text.</param>
  134. /// <param name="text">A text string.</param>
  135. /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
  136. /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
  137. /// <remarks>The drawing operation will be scaled according to the <see cref="ScaleVector"/>
  138. /// property.</remarks>
  139. public new void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color)
  140. {
  141. base.DrawString(spriteFont, text, position, color, 0, Vector2.Zero, ScaleVector, SpriteEffects.None, 0);
  142. }
  143. /// <summary>
  144. /// Adds a string to a batch of sprites for rendering using the specified font,
  145. /// text, position, color, rotation, origin, scale, effects and layer.
  146. /// </summary>
  147. /// <param name="spriteFont">A font for diplaying text.</param>
  148. /// <param name="text">A text string.</param>
  149. /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
  150. /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
  151. /// <param name="rotation">Specifies the angle (in radians) to rotate the sprite about its center.</param>
  152. /// <param name="origin">The sprite origin; the default is (0,0) which represents the
  153. /// upper-left corner.</param>
  154. /// <param name="scale">Scale factor.</param>
  155. /// <param name="effects">Effects to apply.</param>
  156. /// <param name="layerDepth">The depth of a layer. By default, 0 represents the front layer and 1 represents
  157. /// a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing.</param>
  158. /// <remarks>The drawing operation will be scaled according to the <see cref="ScaleVector"/>
  159. /// property.</remarks>
  160. public new void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, float rotation,
  161. Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
  162. {
  163. base.DrawString(spriteFont, text, position, color, rotation, origin, scale * ScaleVector, effects,
  164. layerDepth);
  165. }
  166. /// <summary>
  167. /// Adds a string to a batch of sprites for rendering using the specified font,
  168. /// text, position, color, rotation, origin, scale, effects and layer.
  169. /// </summary>
  170. /// <param name="spriteFont">A font for diplaying text.</param>
  171. /// <param name="text">A text string.</param>
  172. /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
  173. /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
  174. /// <param name="rotation">Specifies the angle (in radians) to rotate the sprite about its center.</param>
  175. /// <param name="origin">The sprite origin; the default is (0,0) which represents the upper-left
  176. /// corner.</param>
  177. /// <param name="scale">Scale factor.</param>
  178. /// <param name="effects">Effects to apply.</param>
  179. /// <param name="layerDepth">The depth of a layer. By default, 0 represents the front layer and 1 represents
  180. /// a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing.</param>
  181. /// <remarks>The drawing operation will be scaled according to the <see cref="ScaleVector"/>
  182. /// property.</remarks>
  183. public new void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, float rotation,
  184. Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)
  185. {
  186. DrawString(spriteFont, text, position, color, rotation, origin, scale * ScaleVector, effects, layerDepth);
  187. }
  188. /// <summary>
  189. /// Adds a string to a batch of sprites for rendering using the specified font,
  190. /// text, position, color, rotation, origin, scale, effects and layer.
  191. /// </summary>
  192. /// <param name="spriteFont">A font for diplaying text.</param>
  193. /// <param name="text">Text string.</param>
  194. /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
  195. /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
  196. /// <param name="rotation">Specifies the angle (in radians) to rotate the sprite about its center.</param>
  197. /// <param name="origin">The sprite origin; the default is (0,0) which represents the upper-left
  198. /// corner.</param>
  199. /// <param name="scale">Scale factor.</param>
  200. /// <param name="effects">Effects to apply.</param>
  201. /// <param name="layerDepth">The depth of a layer. By default, 0 represents the front layer and 1 represents
  202. /// a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing.</param>
  203. /// <remarks>The drawing operation will be scaled according to the <see cref="ScaleVector"/>
  204. /// property.</remarks>
  205. public new void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color,
  206. float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
  207. {
  208. base.DrawString(spriteFont, text, position, color, rotation, origin, scale * ScaleVector, effects,
  209. layerDepth);
  210. }
  211. /// <summary>
  212. /// Adds a string to a batch of sprites for rendering using the specified font,
  213. /// text, position, color, rotation, origin, scale, effects and layer.
  214. /// </summary>
  215. /// <param name="spriteFont">A font for diplaying text.</param>
  216. /// <param name="text">Text string.</param>
  217. /// <param name="position">The location (in screen coordinates) to draw the sprite.</param>
  218. /// <param name="color">The color to tint a sprite. Use Color.White for full color with no tinting.</param>
  219. /// <param name="rotation">Specifies the angle (in radians) to rotate the sprite about its center.</param>
  220. /// <param name="origin">The sprite origin; the default is (0,0) which represents the upper-left
  221. /// corner.</param>
  222. /// <param name="scale">Scale factor.</param>
  223. /// <param name="effects">Effects to apply.</param>
  224. /// <param name="layerDepth">The depth of a layer. By default, 0 represents the front layer and 1 represents
  225. /// a back layer. Use SpriteSortMode if you want sprites to be sorted during drawing.</param>
  226. /// <remarks>The drawing operation will be scaled according to the <see cref="ScaleVector"/>
  227. /// property.</remarks>
  228. public new void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color,
  229. float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)
  230. {
  231. base.DrawString(spriteFont, text, position, color, rotation, origin, scale * ScaleVector,
  232. effects, layerDepth);
  233. }
  234. #endregion
  235. }
  236. }