| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- //-----------------------------------------------------------------------------
- // LoadingScreen.cs
- //
- // Display a screen to inform the player that their game is still responding,
- // while loading content in the background.
- //-----------------------------------------------------------------------------
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- using RacingGame.Graphics;
- using System.Threading.Tasks;
- using RacingGame.Helpers;
- namespace RacingGame.GameScreens
- {
- /// <summary>
- /// Loading screen
- /// </summary>
- class LoadingScreen : IGameScreen
- {
- private const string loadingText = "Loading...";
- private int loadingTextWidth = TextureFont.GetTextWidth(loadingText);
- private string loadingStatus = "";
- public LoadingScreen()
- {
- //Setup the handler before we start the thread
- RacingGameManager.LoadEvent += new EventHandler<EventArgs>(LoadEvent);
- }
- /// <summary>
- /// Gather input on the loading screen and update it if progress has
- /// changed in loading the game.
- /// </summary>
- public void Update(GameTime gameTime)
- {
- if (RacingGameManager.LoadingTask == null)
- {
- RacingGameManager.LoadingTask = Task.Run(() =>
- RacingGameManager.LoadResources());
- }
- if (RacingGameManager.LoadingTask.IsCompleted)
- {
- if (RacingGameManager.LoadingTask.IsFaulted)
- {
- Log.Write("Content loading failed: " +
- RacingGameManager.LoadingTask.Exception);
- }
- // Proceed to next screen
- }
- }
- public void LoadEvent(object sender, EventArgs e)
- {
- loadingStatus = (string)sender;
- }
- /// <summary>
- /// Render loading screen
- /// </summary>
- public bool Render()
- {
- SpriteBatch textBatch = new SpriteBatch(BaseGame.Device);
- Vector2 position = new Vector2((BaseGame.Width / 2) - 50, (BaseGame.Height / 2) - 20);
- for (int i = 0; i < loadingText.Length; i++)
- {
- string charStr = new string(loadingText[i], 1);
- int charHeight = (int)(position.Y + 7 * Math.Abs(Math.Sin((i / 4f) + (-BaseGame.TotalTime * 3))));
- TextureFont.WriteText((int)position.X, charHeight, charStr, Color.Red);
- position.X += TextureFont.GetTextWidth(charStr);
- }
- TextureFont.WriteTextCentered(BaseGame.Width / 2, (int)position.Y + 40, loadingStatus);
- return RacingGameManager.ContentLoaded;
- }
- }
- }
|