123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- #region File Description
- //-----------------------------------------------------------------------------
- // LoadingAndInstructionScreen.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.Threading;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input.Touch;
- using Microsoft.Xna.Framework.Input;
- #endregion
- namespace HoneycombRush
- {
- class LoadingAndInstructionScreen : GameScreen
- {
- #region Fields
- SpriteFont font;
- bool isLoading;
- GameplayScreen gameplayScreen;
- System.Threading.Thread thread;
- bool assetsLoaded = false;
- #endregion
- #region Initialization
- public LoadingAndInstructionScreen()
- {
- TransitionOnTime = TimeSpan.FromSeconds(0);
- TransitionOffTime = TimeSpan.FromSeconds(0.5);
- EnabledGestures = GestureType.Tap;
- }
-
- /// <summary>
- /// Load the screen resources
- /// </summary>
- public override void LoadContent()
- {
- font = Load<SpriteFont>(@"Fonts\MenuFont");
- // Create a new instance of the gameplay screen
- gameplayScreen = new GameplayScreen(DifficultyMode.Easy);
- gameplayScreen.ScreenManager = ScreenManager;
- }
- #endregion
- #region Update
- /// <summary>
- /// Exit the screen after a tap gesture
- /// </summary>
- /// <param name="input"></param>
- public override void HandleInput(GameTime gameTime, InputState input)
- {
- if (!isLoading)
- {
- PlayerIndex player;
- // Handle touch input
- if (input.Gestures.Count > 0)
- {
- if (input.Gestures[0].GestureType == GestureType.Tap)
- {
- LoadResources();
- }
- }
- else if (input.IsNewKeyPress(Keys.Escape, ControllingPlayer, out player))
- {
- foreach (GameScreen screen in ScreenManager.GetScreens())
- {
- screen.ExitScreen();
- }
- ScreenManager.AddScreen(new BackgroundScreen("titleScreen"), null);
- ScreenManager.AddScreen(new MainMenuScreen(), PlayerIndex.One);
- }
- else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
- input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player) ||
- input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player))
- {
- LoadResources();
- }
- }
- base.HandleInput(gameTime, input);
- }
- /// <summary>
- /// Screen update logic
- /// </summary>
- /// <param name="gameTime"></param>
- /// <param name="otherScreenHasFocus"></param>
- /// <param name="coveredByOtherScreen"></param>
- public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
- {
- // If necessary, load the highscore. Remember that calling LoadHighscores multiple times does not have
- // and adverse effect. Highscore will only be loaded once.
- if (!HighScoreScreen.HighscoreLoaded)
- {
- HighScoreScreen.LoadHighscores();
- }
- // If additional thread is running, do nothing
- else if (null != thread)
- {
- // If additional thread finished loading and the screen is not exiting
- if (thread.ThreadState == ThreadState.Stopped && !IsExiting)
- {
- // Move on to the game play screen once highscore data is loaded
- foreach (GameScreen screen in ScreenManager.GetScreens())
- {
- screen.ExitScreen();
- }
- ScreenManager.AddScreen(gameplayScreen, null);
- }
- }
- else if (assetsLoaded)
- {
- // Screen is not exiting
- if ( !IsExiting)
- {
- // Move on to the game play screen once highscore data is loaded
- foreach (GameScreen screen in ScreenManager.GetScreens())
- {
- screen.ExitScreen();
- }
- ScreenManager.AddScreen(gameplayScreen, null);
- }
- }
- base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
- }
- #endregion
- #region Render
- /// <summary>
- /// Render screen
- /// </summary>
- /// <param name="gameTime"></param>
- public override void Draw(GameTime gameTime)
- {
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- spriteBatch.Begin();
- // If loading game play screen resource in the
- // background, show "Loading..." text
- if (isLoading)
- {
- string text = "Loading...";
- Vector2 size = font.MeasureString(text);
- Vector2 position = new Vector2((ScreenManager.GraphicsDevice.Viewport.Width - size.X) / 2,
- (ScreenManager.GraphicsDevice.Viewport.Height - size.Y) / 2);
- spriteBatch.DrawString(font, text, position, Color.White);
- }
- spriteBatch.End();
- }
- #endregion
- #region Private functionality
- private void LoadResources()
- {
- #if MONOMAC
- // Start loading the resources on main thread
- // If not then all sorts of errors happen for
- // AutoReleasPools and OpenGL does not handle
- // multiple thread to well when using Thread
- MonoMac.AppKit.NSApplication.SharedApplication.BeginInvokeOnMainThread(delegate {
- gameplayScreen.LoadAssets ();
- isLoading = false;
- assetsLoaded = true;
- });
- #else
- // Start loading the resources in an additional thread
- thread = new Thread(new ThreadStart(gameplayScreen.LoadAssets));
- thread.Start();
- isLoading = true;
- #endif
- }
- #endregion
- }
- }
|