Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've faced with a problem when i tried to draw 2d game with SpriteBatch and GraphicsDevice.DrawUserPrimitives. I suppose that what is drawing with SpriteBatch has higher priority, because i can not see a result of its rendering. GraphiscDevice.DrawUserPrimitives renders only if i comment a line of rendering background. Here is my method Draw. Could you check the order of rendering. What is the reason?

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.BlanchedAlmond);    

    basicEffect.CurrentTechnique.Passes[0].Apply();

    spriteBatch.Begin();
    spriteBatch.Draw(background, new Rectangle { X = 0, Y = 0, Height = Bounds.Height, Width = Bounds.Width }, Color.White);

    DrawCell();
    spriteBatch.DrawString(defaultFont, "fps: " + FPS, new Vector2(10, 75), Color.DarkBlue);

    spriteBatch.End();

    base.Draw(gameTime);
}

private void DrawCell()
{
    basicEffect.Texture = cellBody;
    graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, cell.Triangles, 0, cell.Triangles.Length / 3);
}

protected override void Initialize()
{
    basicEffect = new BasicEffect(graphics.GraphicsDevice);
    basicEffect.TextureEnabled = true;
    basicEffect.Texture = bg_menu;
    basicEffect.World = Matrix.Identity;
    basicEffect.Projection = Matrix.CreateOrthographicOffCenter
  (0, graphics.GraphicsDevice.Viewport.Width,
  graphics.GraphicsDevice.Viewport.Height, 0,
  0, 1);
...
}
share|improve this question
add comment

2 Answers

The spritebatch items don't actually draw onto the back buffer until spritebatch.End() is called. This is so it knows which sprites to 'batch' into 1 call to the GPU.

So your code basically:

  1. informs the spritbatch that background will be one item in the batch
  2. draws Drawcell directly to the back buffer (because you call it explicitly)
  3. informs the spritebatch that "fps: " will be another item in the batch
  4. End() is call so it batches and draws background and "fps: " to the back buffer overwriting the DrawCell stuff

It is generally recommended to End() the spritebatch before explicitly calling directly from the graphics device.

share|improve this answer
    
i have tried to call SpriteBatch.End(),but the notification about mistake appears An exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.Graphics.ni.dll but was not handled in user code –  Ilya Jan 15 '13 at 15:21
    
Are you saying that SpriteBatch.Draw uses the GPU out of the box? Really? –  ashes999 Jan 15 '13 at 15:23
    
I don't know. I've tried to run code as you've told, but i am not succeed, that's why i'll really appreciate your help. –  Ilya Jan 15 '13 at 15:39
add comment

I have found this issue in my game recently, however I found the way to get over it is to call end before your draw primatives and begin after it:

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.BlanchedAlmond);    

basicEffect.CurrentTechnique.Passes[0].Apply();

spriteBatch.Begin();
spriteBatch.Draw(background, new Rectangle { X = 0, Y = 0, Height = Bounds.Height, Width = Bounds.Width }, Color.White);

spriteBatch.End(); //end the spritebatch here 
DrawCell();
spriteBatch.Begin(); // start it again here
spriteBatch.DrawString(defaultFont, "fps: " + FPS, new Vector2(10, 75), Color.DarkBlue);

spriteBatch.End();

base.Draw(gameTime);
}
share|improve this answer
    
Yes, guys. I've tryied to do it in such ways also, but it doesn't help –  Ilya Jan 16 '13 at 10:15
    
do you mind putting in the code for the definition od cell and cellbody. Also BasicEffect is only ever assigned to and never used, is it used somewhere else not in your demo code? I am thinking that there is something else going wrong here. –  Jastill Jan 16 '13 at 22:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.