ImageComponent.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. namespace RockRain.Core
  4. {
  5. /// <summary>
  6. /// This is a game component that draw a image.
  7. /// </summary>
  8. public class ImageComponent : DrawableGameComponent
  9. {
  10. public enum DrawMode
  11. {
  12. Center = 1,
  13. Stretch,
  14. } ;
  15. // Texture to draw
  16. protected readonly Texture2D texture;
  17. // Draw Mode
  18. protected readonly DrawMode drawMode;
  19. // Spritebatch
  20. protected SpriteBatch spriteBatch = null;
  21. // Image Rectangle
  22. protected Rectangle imageRect;
  23. /// <summary>
  24. /// Default constructor
  25. /// </summary>
  26. /// <param name="game">The game object</param>
  27. /// <param name="texture">Texture to Draw</param>
  28. /// <param name="drawMode">Draw Mode</param>
  29. public ImageComponent(Game game, Texture2D texture, DrawMode drawMode)
  30. : base(game)
  31. {
  32. this.texture = texture;
  33. this.drawMode = drawMode;
  34. // Get the current spritebatch
  35. spriteBatch = (SpriteBatch)
  36. Game.Services.GetService(typeof (SpriteBatch));
  37. // Create a rectangle with the size and position of the image
  38. switch (drawMode)
  39. {
  40. case DrawMode.Center:
  41. imageRect = new Rectangle((Game.Window.ClientBounds.Width -
  42. texture.Width)/2,(Game.Window.ClientBounds.Height -
  43. texture.Height)/2,texture.Width, texture.Height);
  44. break;
  45. case DrawMode.Stretch:
  46. imageRect = new Rectangle(0, 0, Game.Window.ClientBounds.Width,
  47. Game.Window.ClientBounds.Height);
  48. break;
  49. }
  50. }
  51. /// <summary>
  52. /// Allows the game component to draw itself.
  53. /// </summary>
  54. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  55. public override void Draw(GameTime gameTime)
  56. {
  57. spriteBatch.Draw(texture, imageRect, Color.White);
  58. base.Draw(gameTime);
  59. }
  60. }
  61. }