2

I'm trying to draw a quick loading sprite to the screen when the game gets reset but spritebatch isn't drawing. I've put a breakpoint on game.spritebatch.begin(); and when I step through nothing is drawn. Any ideas?

Here is my reset function

public void Reset()
{
    loadingScreen = game.Content.Load<Texture2D>("Textures\\loadingScreen");
    game.spriteBatch.Begin();
    game.spriteBatch.Draw(loadingScreen, Vector2.Zero, null, Color.Black, 0.0f, Vector2.Zero, 0.5f, SpriteEffects.None, 0f);
    game.spriteBatch.End();
    cameraPosition = original_cameraPosition;
    players.Clear();
    furthest = 0;
    enemies.Clear();
    LoadEnemies();
    ActivatePlayers();
}
jmack20093
  • 63
  • 1
  • 4
  • 2
    Have you noticed that your loading texture is only going to be displayed in the one frame when `Reset()` is begin called? – neeKo Dec 05 '12 at 14:43
  • Niko is right - does your loading take long enough that your loading screen will stay displayed for long enough for you to actually see it? Before it gets replaced by drawing from the game loop? If you call `SupressDraw` in your `Update` method, you can prevent the game loop from drawing anything itself - which will allow you to diagnose whether this is happening. – Andrew Russell Dec 07 '12 at 10:40
  • After added the game.GraphicsDevice.Present() it draws a purple screen and it does last long enough to show the screen for a second. Also if I put a break point after spritebatch.End() before I added present it didn't show anything(meaning it wasn't drawing). Now it shows purple. – jmack20093 Dec 07 '12 at 22:01

3 Answers3

2

You are probably not polling it in the correct place. The texture reset( loadingScreen = game.Content.Load<Texture2D>("Textures\\loadingScreen")should only be carried out once, otherwise it is a massive waste of resoruces.

The rendering code on the other hand:

    game.spriteBatch.Begin();
    game.spriteBatch.Draw(loadingScreen, Vector2.Zero, null, Color.Black, 0.0f, Vector2.Zero, 0.5f, SpriteEffects.None, 0f);
    game.spriteBatch.End();

This needs to be continually polled otherwise it will only render for a few ms after which you you will not be able to see it. Put the rendering code in the rendering method and it should work ok.

const_ref
  • 4,016
  • 3
  • 23
  • 38
  • 2
    Actually subsequent calls to `Content.Load` will return the same instance that was loaded on the original call. So it's not actually that bad. – Andrew Russell Dec 05 '12 at 14:26
  • 1
    Even if that is the case, its poor practice. Doing something similar in other languages is a sure fire way to eat resources, so its best not to do it at all as there is certainly no need. – const_ref Dec 05 '12 at 14:34
  • 2
    That is certainly true. Although in this *particular* case - where it's just a loading screen (and knowing that XNA's content manager does caching) - I'd give it a pass on the basis that it saves tracking the texture elsewhere. – Andrew Russell Dec 05 '12 at 14:38
  • 1
    @AndrewRussell Thanks ill keep that in mind for the future. It was just a sidenote to the probable problem the person faces, which he can likely fix using either of our solutions :) – const_ref Dec 05 '12 at 14:42
  • 1
    No other draws are being called until after the loading is done. That is why I am only calling it this one time. – jmack20093 Dec 05 '12 at 17:52
  • Draw it to its own rendertarget2d that you can save in memory, then in a new spritebatch, draw that rendertarget2d... cuts down on loading... – Jared Mark Nov 11 '13 at 18:55
2

Have you tried using Color.White?

neeKo
  • 4,280
  • 23
  • 31
1

In the default configuration, XNA uses double-buffering. All your draw commands are happening - but the result is being drawn to the backbuffer. You need to swap it with the frontbuffer, so that the result appears on screen.

Normally this is done for you. The game will call Update, BeginDraw, Draw, EndDraw in a loop (these are methods from Game that you can override in your own game class). The default implementation of EndDraw will call GraphicsDevice.Present, which will swap the buffers.

I get the impression that, being a loading screen, you are drawing this somewhere outside of the main game loop. You can call GraphicsDevice.Present yourself in order to flip the buffers.

A few other tips:

  • You can do essentially the opposite - and prevent XNA from drawing automatically - by overriding BeginDraw and returning false. Or calling SupressDraw. (Or you can prevent the swap by overriding EndDraw and not calling the base method.)

  • SpriteBatch, by default, will buffer all drawing until End is called. Put your Present call after that.

Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
  • This works to get something to draw to the screen but it doesn't draw my texture. It just shows a purple screen. – jmack20093 Dec 06 '12 at 18:40
  • @jmack20093 The purple screen is the "freshly allocated buffer" colour (indicating nothing has been drawn to it). Not really sure why that is happening to you. Render targets sometimes trip people up - but you're not using those, right? Maybe something wrong with your sprite? (I suppose it could be completely transparent.) You could try `GraphicsDevice.Clear` to try and identify if drawing is actually happening when you expect. `Clear` followed by `Present` *should* let you change the colour of the screen - a good starting point for diagnosing the problem. – Andrew Russell Dec 07 '12 at 10:35
  • Using clear did change the color. Could it be that something is called in `Draw()` behind the scenes that could be causing the problem? The game is 3D so I'm changing between 2D and 3D drawing often but the UI draws just fine in `Draw()` using the same method as `Reset()`. – jmack20093 Dec 07 '12 at 22:10
  • @jmack20093 `SpriteBatch` can cause problems for 3D drawing. But not the other way around. The default `SpriteBatch.Begin` should reset the settings required for 2D. I've done some testing - and the code you've posted - with `Present` after `End` *should* work. So something else is going on here. Can you reproduce the problem using a fresh XNA project? – Andrew Russell Dec 08 '12 at 10:19