RelativeSprite.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // RelativeSprite.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 Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework;
  13. #endregion
  14. namespace Marblets
  15. {
  16. /// <summary>
  17. /// This class provides helper functions for making sprites, which are pixel based
  18. /// get drawn in correct positions and sizes when a window is resized.
  19. /// For the purposes of this code 1280x720 HD resolution is considered the base
  20. /// size. Everything is sized based on its relative value to that.
  21. /// </summary>
  22. public class RelativeSpriteBatch : SpriteBatch
  23. {
  24. private const int fullHeight = 480;
  25. private const int fullWidth = 320;
  26. private static float scale;
  27. private static Vector2 offset;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="RelativeSpriteBatch"/> class.
  30. /// </summary>
  31. /// <param name="graphicsDevice">The graphics device where sprites will be
  32. /// drawn.</param>
  33. public RelativeSpriteBatch(GraphicsDevice graphicsDevice)
  34. : base(graphicsDevice)
  35. {
  36. Resize();
  37. }
  38. /// <summary>
  39. /// Adjust the scale and location of the drawn graphics so that everything
  40. /// fits on the screen and is centered.
  41. /// </summary>
  42. public void Resize()
  43. {
  44. // Scale is used to stretch or shrink the drawn images so that everything
  45. // is visible on screen.
  46. scale =
  47. Math.Min((float)GraphicsDevice.Viewport.Height / (float)fullHeight,
  48. (float)GraphicsDevice.Viewport.Width / (float)fullWidth);
  49. // The offset used to center the drawn images on the screen
  50. offset =
  51. new Vector2((GraphicsDevice.Viewport.Width - fullWidth * scale) / 2,
  52. (GraphicsDevice.Viewport.Height - fullHeight * scale) / 2);
  53. }
  54. /// <summary>
  55. /// Draws a sprite at a particular position
  56. /// </summary>
  57. /// <param name="texture">The texture to take the sprite from. For this overload
  58. /// it will draw the entire texture.</param>
  59. /// <param name="position">The position in 1280x720 screen space. It will
  60. /// actually be drawn in the correct relative position and size</param>
  61. /// <param name="color">The color to tint the sprite</param>
  62. public new void Draw(Texture2D texture, Vector2 position, Color color)
  63. {
  64. this.Draw(texture, position, null, color);
  65. }
  66. /// <summary>
  67. /// Draws a sprite at a particular position
  68. /// </summary>
  69. /// <param name="texture">The texture to take the sprite from. For this overload
  70. /// it will draw the entire texture.</param>
  71. /// <param name="position">The position in 1280x720 screen space. It will
  72. /// actually be drawn in the correct relative position and size</param>
  73. /// <param name="source">The part of the texture to draw</param>
  74. /// <param name="color">The color to tint the sprite</param>
  75. public new void Draw(Texture2D texture, Vector2 position, Rectangle? source,
  76. Color color)
  77. {
  78. //Scale and move the sprite based on current screen size
  79. base.Draw(texture, (position * scale) + offset, source, color, 0,
  80. Vector2.Zero, scale, SpriteEffects.None, 0);
  81. }
  82. }
  83. }