LoadingScreen.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //-----------------------------------------------------------------------------
  2. // LoadingScreen.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Threading;
  9. using Microsoft.Xna.Framework;
  10. using Microsoft.Xna.Framework.GamerServices;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Input;
  13. using Microsoft.Xna.Framework.Storage;
  14. namespace AlienGameSample
  15. {
  16. /// <summary>
  17. /// This just loads all content we're going to use on a background thread. It doesn't
  18. /// draw anything because it's supposed to be invoked in front of the background screen and right
  19. /// before the main menu (i.e. noone will notice). This saves us from spinning up the drive later
  20. /// and stalling between menu and gameplay.
  21. /// </summary>
  22. class LoadingScreen : GameScreen
  23. {
  24. private Thread backgroundThread;
  25. /// <summary>
  26. /// The constructor is private: loading screens should
  27. /// be activated via the static Load method instead.
  28. /// </summary>
  29. public LoadingScreen()
  30. {
  31. TransitionOnTime = TimeSpan.FromSeconds(0.0);
  32. TransitionOffTime = TimeSpan.FromSeconds(0.0);
  33. }
  34. public override void LoadContent()
  35. {
  36. // Create the thread here (instead of the ctor), so we can guarantee that loading content will work
  37. if (backgroundThread == null)
  38. {
  39. backgroundThread = new Thread(BackgroundLoadContent);
  40. backgroundThread.Start();
  41. }
  42. base.LoadContent();
  43. }
  44. void BackgroundLoadContent()
  45. {
  46. // First thing we need to do is get a valid storage device for loading and saving
  47. // high scores. Since this is a background thread, we can block on it; rendering the guide
  48. // for Xbox 360 will happen on the main thread.
  49. StorageDevice device = StorageDevice.ShowStorageDeviceGuide();
  50. ScreenManager.Game.Services.AddService(typeof(StorageDevice), device);
  51. /*ScreenManager.Game.Content.Load<object>("Alien_Hit");
  52. ScreenManager.Game.Content.Load<object>("alien1");
  53. ScreenManager.Game.Content.Load<object>("background");
  54. ScreenManager.Game.Content.Load<object>("badguy_blue");
  55. ScreenManager.Game.Content.Load<object>("badguy_green");
  56. ScreenManager.Game.Content.Load<object>("badguy_orange");
  57. ScreenManager.Game.Content.Load<object>("badguy_red");
  58. ScreenManager.Game.Content.Load<object>("bullet");
  59. ScreenManager.Game.Content.Load<object>("cloud1");
  60. ScreenManager.Game.Content.Load<object>("cloud2");
  61. ScreenManager.Game.Content.Load<object>("fire");
  62. ScreenManager.Game.Content.Load<object>("gamefont");
  63. ScreenManager.Game.Content.Load<object>("ground");
  64. ScreenManager.Game.Content.Load<object>("hills");
  65. ScreenManager.Game.Content.Load<object>("laser");
  66. ScreenManager.Game.Content.Load<object>("menufont");
  67. ScreenManager.Game.Content.Load<object>("moon");
  68. ScreenManager.Game.Content.Load<object>("mountains_blurred");
  69. ScreenManager.Game.Content.Load<object>("Player_Hit");
  70. ScreenManager.Game.Content.Load<object>("scorefont");
  71. ScreenManager.Game.Content.Load<object>("smoke");
  72. ScreenManager.Game.Content.Load<object>("sun");
  73. ScreenManager.Game.Content.Load<object>("tank");
  74. ScreenManager.Game.Content.Load<object>("tank_fire");
  75. ScreenManager.Game.Content.Load<object>("tank_tire");
  76. ScreenManager.Game.Content.Load<object>("tank_top");
  77. ScreenManager.Game.Content.Load<object>("title");
  78. ScreenManager.Game.Content.Load<object>("titlefont");*/
  79. }
  80. /// <summary>
  81. /// Updates the loading screen.
  82. /// </summary>
  83. public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  84. bool coveredByOtherScreen)
  85. {
  86. // See if the loading has completed...
  87. if (backgroundThread != null && backgroundThread.Join(10))
  88. {
  89. backgroundThread = null;
  90. this.ExitScreen();
  91. ScreenManager.AddScreen(new MainMenuScreen());
  92. ScreenManager.Game.ResetElapsedTime();
  93. }
  94. base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
  95. }
  96. }
  97. }