OK, I added a loading screen to my game, and in order for the game able to update and show a loading screen and load stuff in the background I came to the concludsion that I needed to host the loading function on a thread. When I did this, the loading times doubled, if not tripled in length, can anyone thing why this is so?
I set the thread to the highest priority but that hasn't seemed to help (note, this is my first time ever using threads):
gameScreen = new GameScreen(content);
loadingThread = new Thread(gameScreen.LoadContent);
loadingThread.Priority = ThreadPriority.Highest;
loadingThread.Start();
gameScreen.ScreenState = ScreenState.Loading;
EDIT:
Loading Screen Class:
Thread loadingThread;
Screen gameScreen;
public LoadingScreen(ContentManager content)
: base(content)
{
}
public override void LoadContent()
{
Texture2D background = content.Load<Texture2D>("Screens\\Loading\\Textures\\background");
Texture2D dropshadow = content.Load<Texture2D>("Screens\\Loading\\Textures\\dropshadow");
Texture2D logo = content.Load<Texture2D>("Screens\\Loading\\Textures\\logo");
Sprite background_sprite = new Sprite(background, new Vector2(0, 0));
int tiles = Variables.SCREEN_WIDTH / dropshadow.Width;
Sprite dropshadow_sprite_top = new Sprite(dropshadow, new Vector2(0, 0));
dropshadow_sprite_top.ScaleY(65);
tiles = Variables.SCREEN_WIDTH / dropshadow_sprite_top.ScaledWidth + (Variables.SCREEN_WIDTH % dropshadow_sprite_top.ScaledWidth == 0 ? 0 : 1);
dropshadow_sprite_top.TileX(tiles);
Sprite dropshadow_sprite_bottom = new Sprite(dropshadow, new Vector2(0, Variables.SCREEN_HEIGHT - dropshadow.Height));
dropshadow_sprite_bottom.ScaleY(65);
dropshadow_sprite_bottom.Position = new Vector2(0, Variables.SCREEN_HEIGHT - dropshadow_sprite_bottom.ScaledHeight);
dropshadow_sprite_bottom.TileX(tiles);
dropshadow_sprite_bottom.FlipY();
Sprite logo_sprite = new Sprite(logo, new Vector2(Variables.SCREEN_WIDTH - logo.Width, Variables.SCREEN_HEIGHT - logo.Height));
logo_sprite.ScaleX(45);
logo_sprite.Position = new Vector2(Variables.SCREEN_WIDTH - logo_sprite.ScaledWidth - 10, Variables.SCREEN_HEIGHT - logo_sprite.ScaledHeight - 10);
sprites.Add(background_sprite);
sprites.Add(dropshadow_sprite_top);
sprites.Add(dropshadow_sprite_bottom);
sprites.Add(logo_sprite);
gameScreen = new GameScreen(content);
loadingThread = new Thread(gameScreen.LoadContent);
loadingThread.Priority = ThreadPriority.Highest;
loadingThread.Start();
gameScreen.ScreenState = ScreenState.Loading;
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override Screen RequestedScreen()
{
if (gameScreen.ScreenState == ScreenState.Idle)
{
return gameScreen;
}
return this;
}
public override void Draw(SpriteBatch spriteBatch)
{
base.Draw(spriteBatch);
}
...
In the GameScreen class:
public override void LoadContent()
{
world.LoadContent(content);
playerRender.LoadContent(content);
skyRenderer.LoadContent(world, content);
screenState = ScreenState.Idle;
}
...
In the game1.cs
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Variables.TILE_ALAIS = new GUI.SpriteSheet(Content.Load<Texture2D>("Textures\\Sheets\\terrain"), new Vector2(16, 16));
Variables.BLOCKSELECTOR_TEXTURE = Content.Load<Texture2D>("Textures\\Sprites\\blockselector");
OutputBuffer.BUFFER_FONT = Content.Load<SpriteFont>("Fonts\\statusfont");
Variables.STATUS_FONT = OutputBuffer.BUFFER_FONT;
screen = new MainScreen(Content);
screen.LoadContent();
frameRateCounter.LoadContent(Content);
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (screen != screen.RequestedScreen())
{
screen = screen.RequestedScreen();
screen.LoadContent();
}
screen.Update(gameTime);
frameRateCounter.Update(gameTime);
ProcessInput();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
spriteBatch.Begin();
screen.Draw(spriteBatch);
//frameRateCounter.Draw(gameTime, spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
The requested screen function asks the current screen if it should change. The MainScreen.RequestedScreen() just returns LoadingScreen, when then if it has finished building the world and etc. It returns a instance of the game screen class whoose LoadContent method has been threaded.
Any ideas?