Game1.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. #if ANDROID
  4. using Android.App;
  5. #endif
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Storage;
  11. using Microsoft.Xna.Framework.Audio;
  12. namespace Microsoft.Xna.Samples.Sound
  13. {
  14. /// <summary>
  15. /// This is the main type for your game
  16. /// </summary>
  17. public class Game1 : Microsoft.Xna.Framework.Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. KeyboardState oldSate;
  21. SoundEffect sound;
  22. SpriteBatch spriteBatch;
  23. SoundEffectInstance soundInst;
  24. SpriteFont font;
  25. #if ANDROID
  26. public Game1 (AndroidGameActivity activity) : base (activity)
  27. #else
  28. public Game1 ()
  29. #endif
  30. {
  31. graphics = new GraphicsDeviceManager (this);
  32. Content.RootDirectory = "Content";
  33. graphics.PreferMultiSampling = true;
  34. graphics.IsFullScreen = false;
  35. Window.AllowUserResizing = false;
  36. graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight | DisplayOrientation.PortraitUpsideDown;
  37. }
  38. /// <summary>
  39. /// Allows the game to perform any initialization it needs to before starting to run.
  40. /// This is where it can query for any required services and load any non-graphic
  41. /// related content. Calling base.Initialize will enumerate through any components
  42. /// and initialize them as well.
  43. /// </summary>
  44. protected override void Initialize ()
  45. {
  46. // TODO: Add your initialization logic here
  47. base.Initialize ();
  48. }
  49. /// <summary>
  50. /// LoadContent will be called once per game and is the place to load
  51. /// all of your content.
  52. /// </summary>
  53. protected override void LoadContent ()
  54. {
  55. // Create a new SpriteBatch, which can be used to draw textures.
  56. spriteBatch = new SpriteBatch(GraphicsDevice);
  57. sound = Content.Load<SoundEffect>("explosion1");
  58. soundInst = sound.CreateInstance();
  59. font = Content.Load<SpriteFont>("spriteFont1");
  60. }
  61. /// <summary>
  62. /// Allows the game to run logic such as updating the world,
  63. /// checking for collisions, gathering input, and playing audio.
  64. /// </summary>
  65. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  66. protected override void Update (GameTime gameTime)
  67. {
  68. KeyboardState ks = Keyboard.GetState();
  69. #if ANDROID
  70. if (soundInst.State != SoundState.Playing)
  71. {
  72. soundInst.Volume = 1f;
  73. soundInst.IsLooped = true;
  74. soundInst.Play();
  75. }
  76. #endif
  77. if (ks[Keys.Escape] == KeyState.Down)
  78. base.Exit();
  79. if (ks.IsKeyDown(Keys.A) && oldSate.IsKeyUp(Keys.A))
  80. soundInst.Play();
  81. if (ks.IsKeyDown(Keys.B) && oldSate.IsKeyUp(Keys.B))
  82. soundInst.Stop();
  83. if (ks.IsKeyDown(Keys.C) && oldSate.IsKeyUp(Keys.C))
  84. soundInst.Pause();
  85. if (ks.IsKeyDown(Keys.D) && oldSate.IsKeyUp(Keys.D))
  86. soundInst.IsLooped = !soundInst.IsLooped;
  87. if (ks.IsKeyDown(Keys.E) && oldSate.IsKeyUp(Keys.E))
  88. soundInst.Stop(true);
  89. if (ks.IsKeyDown(Keys.X))
  90. soundInst.Volume = MathHelper.Clamp(soundInst.Volume + 0.01f, 0f, 1f);
  91. else if (ks.IsKeyDown(Keys.Z))
  92. soundInst.Volume = MathHelper.Clamp(soundInst.Volume - 0.01f, 0f, 1f);;
  93. oldSate = ks;
  94. base.Update (gameTime);
  95. }
  96. /// <summary>
  97. /// This is called when the game should draw itself.
  98. /// </summary>
  99. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  100. protected override void Draw (GameTime gameTime)
  101. {
  102. graphics.GraphicsDevice.Clear (Color.CornflowerBlue);
  103. spriteBatch.Begin();
  104. spriteBatch.DrawString(font, "A: play\nB: stop\nC: pause\nD: toggle looping\nE: immediate stop\nX/Z volume\nStatus: " +
  105. soundInst.State.ToString() + "\nLooping: " +
  106. soundInst.IsLooped.ToString() + "\nVolume: " +
  107. soundInst.Volume.ToString()
  108. , Vector2.Zero, Color.White);
  109. base.Draw(gameTime);
  110. spriteBatch.End();
  111. }
  112. }
  113. }