Texture.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //-----------------------------------------------------------------------------
  2. // Texture.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using System;
  10. using System.IO;
  11. using RacingGame.Helpers;
  12. namespace RacingGame.Graphics
  13. {
  14. /// <summary>
  15. /// Texture class helping you with using DirectX Textures and handling
  16. /// possible errors that can happen while loading (stupid DirectX
  17. /// error messages telling you absolutely nothing, so a lot of pre-checks
  18. /// help you determinate the error before even calling DirectX methods).
  19. /// </summary>
  20. public class Texture : IDisposable
  21. {
  22. public static SpriteBatch alphaSprite;
  23. public static SpriteBatch additiveSprite;
  24. /// <summary>
  25. /// Texture filename
  26. /// </summary>
  27. protected string texFilename = "";
  28. /// <summary>
  29. /// Get filename of texture.
  30. /// </summary>
  31. public string Filename
  32. {
  33. get { return texFilename; }
  34. }
  35. /// <summary>
  36. /// Size of texture
  37. /// </summary>
  38. protected int texWidth, texHeight;
  39. /// <summary>
  40. /// Width of texture
  41. /// </summary>
  42. public int Width
  43. {
  44. get
  45. {
  46. return texWidth;
  47. }
  48. }
  49. /// <summary>
  50. /// Height of texture
  51. /// </summary>
  52. public int Height
  53. {
  54. get
  55. {
  56. return texHeight;
  57. }
  58. }
  59. /// <summary>
  60. /// Gfx rectangle
  61. /// </summary>
  62. /// <returns>Rectangle</returns>
  63. public Rectangle GfxRectangle
  64. {
  65. get
  66. {
  67. return new Rectangle(0, 0, texWidth, texHeight);
  68. }
  69. }
  70. /// <summary>
  71. /// Size of half a pixel, will be calculated when size is set.
  72. /// </summary>
  73. private Vector2 precaledHalfPixelSize = Vector2.Zero;
  74. /// <summary>
  75. /// Get the size of half a pixel, used to correct texture
  76. /// coordinates when rendering on screen, see Texture.RenderOnScreen.
  77. /// </summary>
  78. public Vector2 HalfPixelSize
  79. {
  80. get
  81. {
  82. return precaledHalfPixelSize;
  83. }
  84. }
  85. /// <summary>
  86. /// Calc half pixel size
  87. /// </summary>
  88. protected void CalcHalfPixelSize()
  89. {
  90. precaledHalfPixelSize = new Vector2(
  91. (1.0f / texWidth) / 2.0f,
  92. (1.0f / texHeight) / 2.0f);
  93. }
  94. /// <summary>
  95. /// XNA Framework Graphic Texture
  96. /// </summary>
  97. protected Texture2D internalXnaTexture;
  98. /// <summary>
  99. /// XNA Framework texture
  100. /// </summary>
  101. public virtual Texture2D XnaTexture
  102. {
  103. get
  104. {
  105. return internalXnaTexture;
  106. }
  107. }
  108. /// <summary>
  109. /// Loading succeeded?
  110. /// </summary>
  111. protected bool loaded = true;
  112. /// <summary>
  113. /// Error?
  114. /// </summary>
  115. protected string error = "";
  116. /// <summary>
  117. /// Is texture valid? Will be false if loading failed.
  118. /// </summary>
  119. public virtual bool Valid
  120. {
  121. get
  122. {
  123. return loaded &&
  124. internalXnaTexture != null;
  125. }
  126. }
  127. /// <summary>
  128. /// Has alpha?
  129. /// </summary>
  130. private bool hasAlpha;
  131. /// <summary>
  132. /// Has texture alpha information?
  133. /// </summary>
  134. public bool HasAlphaPixels
  135. {
  136. get
  137. {
  138. return hasAlpha;
  139. }
  140. }
  141. /// <summary>
  142. /// Create texture from a given filename.
  143. /// </summary>
  144. /// <param name="setFilename">Set filename, must be relative and be a
  145. /// valid file in the Textures directory.</param>
  146. public Texture(string setFilename)
  147. {
  148. if (alphaSprite == null)
  149. alphaSprite = new SpriteBatch(BaseGame.Device);
  150. if (additiveSprite == null)
  151. additiveSprite = new SpriteBatch(BaseGame.Device);
  152. if (String.IsNullOrEmpty(setFilename))
  153. throw new ArgumentNullException(nameof(setFilename),
  154. "Unable to create texture without valid filename.");
  155. // Set content name (cut off extension!)
  156. texFilename = Path.GetFileNameWithoutExtension(setFilename);
  157. string fullFilename =
  158. Path.Combine(Directories.ContentDirectory, "Textures", texFilename);
  159. // Try loading as 2d texture
  160. internalXnaTexture = BaseGame.Content.Load<Texture2D>(fullFilename);
  161. // Get info from the texture directly.
  162. texWidth = internalXnaTexture.Width;
  163. texHeight = internalXnaTexture.Height;
  164. // We will use alpha for Dxt3 and Dxt5 textures.
  165. hasAlpha = (internalXnaTexture.Format == SurfaceFormat.Dxt5 ||
  166. internalXnaTexture.Format == SurfaceFormat.Dxt3);
  167. loaded = true;
  168. CalcHalfPixelSize();
  169. }
  170. /// <summary>
  171. /// Create texture, protected version for derived classes.
  172. /// </summary>
  173. protected Texture()
  174. {
  175. }
  176. /// <summary>
  177. /// Create texture by just assigning a Texture2D.
  178. /// </summary>
  179. /// <param name="tex">Tex</param>
  180. public Texture(Texture2D tex)
  181. {
  182. if (alphaSprite == null)
  183. alphaSprite = new SpriteBatch(BaseGame.Device);
  184. if (additiveSprite == null)
  185. additiveSprite = new SpriteBatch(BaseGame.Device);
  186. if (tex == null)
  187. throw new ArgumentNullException(nameof(tex));
  188. internalXnaTexture = tex;
  189. // Get info from the texture directly.
  190. texWidth = internalXnaTexture.Width;
  191. texHeight = internalXnaTexture.Height;
  192. loaded = true;
  193. // We will use alpha for Dxt3 and Dxt5 textures
  194. hasAlpha = (internalXnaTexture.Format == SurfaceFormat.Dxt5 ||
  195. internalXnaTexture.Format == SurfaceFormat.Dxt3);
  196. CalcHalfPixelSize();
  197. }
  198. /// <summary>
  199. /// Dispose
  200. /// </summary>
  201. public void Dispose()
  202. {
  203. Dispose(true);
  204. GC.SuppressFinalize(this);
  205. }
  206. /// <summary>
  207. /// Dispose
  208. /// </summary>
  209. /// <param name="disposing">Disposing</param>
  210. protected virtual void Dispose(bool disposing)
  211. {
  212. if (disposing)
  213. {
  214. if (internalXnaTexture != null)
  215. internalXnaTexture.Dispose();
  216. internalXnaTexture = null;
  217. }
  218. loaded = false;
  219. }
  220. /// <summary>
  221. /// Render texture at rect directly on screen using pixelRect.
  222. /// </summary>
  223. /// <param name="rect">Rectangle</param>
  224. /// <param name="pixelRect">Pixel rectangle</param>
  225. public void RenderOnScreen(Rectangle rect, Rectangle pixelRect)
  226. {
  227. alphaSprite.Draw(internalXnaTexture, rect, pixelRect, Color.White);
  228. //SpriteHelper.AddSpriteToRender(this, rect, pixelRect);
  229. }
  230. /// <summary>
  231. /// Render on screen
  232. /// </summary>
  233. /// <param name="rect">Rectangle</param>
  234. /// <param name="pixelX">Pixel x</param>
  235. /// <param name="pixelY">Pixel y</param>
  236. /// <param name="pixelWidth">Pixel width</param>
  237. /// <param name="pixelHeight">Pixel height</param>
  238. public void RenderOnScreen(Rectangle rect,
  239. int pixelX, int pixelY, int pixelWidth, int pixelHeight)
  240. {
  241. alphaSprite.Draw(internalXnaTexture, rect, new Rectangle(pixelX, pixelY,
  242. pixelWidth, pixelHeight), Color.White);
  243. }
  244. /// <summary>
  245. /// Render on screen
  246. /// </summary>
  247. /// <param name="pos">Position</param>
  248. public void RenderOnScreen(Point pos)
  249. {
  250. alphaSprite.Draw(internalXnaTexture,
  251. new Rectangle(pos.X, pos.Y, texWidth, texHeight),
  252. new Rectangle(0, 0, texWidth, texHeight), Color.White);
  253. }
  254. /// <summary>
  255. /// Render on screen
  256. /// </summary>
  257. /// <param name="renderRect">Render rectangle</param>
  258. public void RenderOnScreen(Rectangle renderRect)
  259. {
  260. alphaSprite.Draw(internalXnaTexture, renderRect, GfxRectangle, Color.White);
  261. //SpriteHelper.AddSpriteToRender(this,
  262. // renderRect, GfxRectangle);
  263. }
  264. /// <summary>
  265. /// Render on screen relative for 1024x640 (16:9) graphics.
  266. /// </summary>
  267. /// <param name="relX">Rel x</param>
  268. /// <param name="relY">Rel y</param>
  269. /// <param name="pixelRect">Pixel rectangle</param>
  270. public void RenderOnScreenRelative16To9(int relX, int relY,
  271. Rectangle pixelRect)
  272. {
  273. alphaSprite.Draw(internalXnaTexture, BaseGame.CalcRectangle(
  274. relX, relY, pixelRect.Width, pixelRect.Height),
  275. pixelRect, Color.White);
  276. //SpriteHelper.AddSpriteToRender(this,
  277. // BaseGame.CalcRectangle(
  278. // relX, relY, pixelRect.Width, pixelRect.Height),
  279. // pixelRect);
  280. }
  281. /// <summary>
  282. /// Render on screen relative 1024x786 (4:3)
  283. /// </summary>
  284. /// <param name="relX">Rel x</param>
  285. /// <param name="relY">Rel y</param>
  286. /// <param name="pixelRect">Pixel rectangle</param>
  287. public void RenderOnScreenRelative4To3(int relX, int relY,
  288. Rectangle pixelRect)
  289. {
  290. alphaSprite.Draw(internalXnaTexture, BaseGame.CalcRectangleKeep4To3(
  291. relX, relY, pixelRect.Width, pixelRect.Height),
  292. pixelRect, Color.White);
  293. //SpriteHelper.AddSpriteToRender(this,
  294. // BaseGame.CalcRectangleKeep4To3(
  295. // relX, relY, pixelRect.Width, pixelRect.Height),
  296. // pixelRect);
  297. }
  298. /// <summary>
  299. /// Render on screen relative for 1600px width graphics.
  300. /// </summary>
  301. /// <param name="relX">Rel x</param>
  302. /// <param name="relY">Rel y</param>
  303. /// <param name="pixelRect">Pixel rectangle</param>
  304. public void RenderOnScreenRelative1600(
  305. int relX, int relY, Rectangle pixelRect)
  306. {
  307. alphaSprite.Draw(internalXnaTexture, BaseGame.CalcRectangle1600(
  308. relX, relY, pixelRect.Width, pixelRect.Height),
  309. pixelRect, Color.White);
  310. //SpriteHelper.AddSpriteToRender(this,
  311. // BaseGame.CalcRectangle1600(
  312. // relX, relY, pixelRect.Width, pixelRect.Height),
  313. // pixelRect);
  314. }
  315. /// <summary>
  316. /// Render texture at rect directly on screen using texture cordinates.
  317. /// This method allows to render with specific color and alpha values.
  318. /// </summary>
  319. /// <param name="rect">Rectangle</param>
  320. /// <param name="pixelRect">Pixel rectangle</param>
  321. /// <param name="color">Color</param>
  322. public void RenderOnScreen(Rectangle rect, Rectangle pixelRect,
  323. Color color)
  324. {
  325. alphaSprite.Draw(internalXnaTexture, rect, pixelRect, color);
  326. //SpriteHelper.AddSpriteToRender(this, rect, pixelRect, color);
  327. }
  328. /// <summary>
  329. /// Render on screen
  330. /// </summary>
  331. /// <param name="rect">Rectangle</param>
  332. /// <param name="pixelRect">Pixel rectangle</param>
  333. /// <param name="color">Color</param>
  334. /// <param name="blendState">Blend mode</param>
  335. public void RenderOnScreen(Rectangle rect, Rectangle pixelRect,
  336. Color color, BlendState blendState)
  337. {
  338. if (blendState == BlendState.Additive)
  339. additiveSprite.Draw(internalXnaTexture, rect, pixelRect, color);
  340. else
  341. alphaSprite.Draw(internalXnaTexture, rect, pixelRect, color);
  342. //SpriteHelper.AddSpriteToRender(this, rect, pixelRect, color, blendState);
  343. }
  344. /// <summary>
  345. /// Render on screen with rotation
  346. /// </summary>
  347. /// <param name="rect"></param>
  348. /// <param name="pixelRect">Pixel rectangle</param>
  349. /// <param name="rotation">Rotation</param>
  350. /// <param name="rotationPoint"></param>
  351. public void RenderOnScreenWithRotation(
  352. Rectangle rect, Rectangle pixelRect,
  353. float rotation, Vector2 rotationPoint)
  354. {
  355. alphaSprite.Draw(internalXnaTexture, rect, pixelRect, Color.White, rotation,
  356. rotationPoint, SpriteEffects.None, 0);
  357. }
  358. /// <summary>
  359. /// To string
  360. /// </summary>
  361. public override string ToString()
  362. {
  363. return "Texture(filename=" + texFilename +
  364. ", width=" + texWidth +
  365. ", height=" + texHeight +
  366. ", xnaTexture=" + (internalXnaTexture != null ? "valid" : "null") + ")";
  367. }
  368. }
  369. }