Screen.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Screen.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;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Audio;
  14. using Microsoft.Xna.Framework.Input;
  15. #endregion
  16. namespace Marblets
  17. {
  18. /// <summary>
  19. /// Screen represents a unit of rendering for the game, generally transitional point
  20. /// such as splash screens, selection screens and the actual game levels.
  21. /// </summary>
  22. public class Screen : DrawableGameComponent
  23. {
  24. private bool isMusicPlaying;
  25. private SoundEffectInstance cue;
  26. private SoundEntry backgroundMusic;
  27. private Texture2D backgroundTexture;
  28. private string backgroundImage;
  29. private RelativeSpriteBatch batch;
  30. /// <summary>
  31. /// Gets the sprite batch used for this screen
  32. /// </summary>
  33. /// <value>The sprite batch for this screen</value>
  34. public RelativeSpriteBatch SpriteBatch
  35. {
  36. get
  37. {
  38. return batch;
  39. }
  40. }
  41. /// <summary>
  42. /// Default constructor
  43. /// </summary>
  44. /// <param name="game">The game object</param>
  45. /// <param name="backgroundImage">The background image to use when this is
  46. /// visible</param>
  47. /// <param name="backgroundMusic">The background music to play when this is
  48. /// visible</param>
  49. public Screen(Game game, string backgroundImage, SoundEntry backgroundMusic)
  50. : base(game)
  51. {
  52. this.backgroundImage = backgroundImage;
  53. this.backgroundMusic = backgroundMusic;
  54. }
  55. /// <summary>
  56. /// Initializes the component. Override to load any non-graphics resources and
  57. /// query for any required services.
  58. /// </summary>
  59. public override void Initialize()
  60. {
  61. base.Initialize();
  62. }
  63. /// <summary>
  64. /// Called when the DrawableGameComponent.Visible property changes. Raises the
  65. /// DrawableGameComponent.VisibleChanged event.
  66. /// </summary>
  67. /// <param name="sender">The DrawableGameComponent.</param>
  68. /// <param name="args">Arguments to the DrawableGameComponent.VisibleChanged
  69. /// event.</param>
  70. protected override void OnVisibleChanged(object sender, EventArgs args)
  71. {
  72. base.OnVisibleChanged(sender, args);
  73. if(!Visible)
  74. {
  75. ShutdownMusic();
  76. }
  77. else
  78. {
  79. StartMusic();
  80. }
  81. }
  82. private void ShutdownMusic()
  83. {
  84. if(isMusicPlaying)
  85. {
  86. Sound.StopMusic(cue);
  87. isMusicPlaying = false;
  88. }
  89. }
  90. private void StartMusic()
  91. {
  92. // TODO cue = Sound.PlayMusic(backgroundMusic);
  93. isMusicPlaying = true;
  94. }
  95. /// <summary>
  96. /// Tidies up the scene.
  97. /// </summary>
  98. public virtual void Shutdown()
  99. {
  100. ShutdownMusic();
  101. if(batch != null)
  102. {
  103. batch.Dispose();
  104. batch = null;
  105. }
  106. }
  107. /// <summary>
  108. /// Load your graphics content.
  109. /// </summary>
  110. protected override void LoadContent()
  111. {
  112. //Re-Create the Sprite Batch!
  113. IGraphicsDeviceService graphicsService =
  114. Game.Services.GetService(typeof(IGraphicsDeviceService))
  115. as IGraphicsDeviceService;
  116. batch = new RelativeSpriteBatch(graphicsService.GraphicsDevice);
  117. //Load content for any sub components
  118. if(!String.IsNullOrEmpty(backgroundImage))
  119. {
  120. backgroundTexture =
  121. MarbletsGame.Content.Load<Texture2D>(backgroundImage);
  122. }
  123. }
  124. protected bool Clicked { get; private set; }
  125. private bool buttonWasDown = false;
  126. public override void Update(GameTime gameTime)
  127. {
  128. bool buttonDown = (Mouse.GetState().LeftButton == ButtonState.Pressed);
  129. Clicked = (buttonWasDown && !buttonDown);
  130. buttonWasDown = buttonDown;
  131. base.Update(gameTime);
  132. if(!isMusicPlaying)
  133. StartMusic();
  134. }
  135. /// <summary>
  136. /// Renders the screen.
  137. /// </summary>
  138. public override void Draw(GameTime gameTime)
  139. {
  140. base.Draw(gameTime);
  141. if(!String.IsNullOrEmpty(backgroundImage))
  142. {
  143. SpriteBatch.Begin();
  144. SpriteBatch.Draw(backgroundTexture, Vector2.Zero, Color.White);
  145. SpriteBatch.End();
  146. }
  147. }
  148. }
  149. }