0
\$\begingroup\$

I'm still new in game developpement, and I'm trying to complete my 1st game. Its a simple 2d game, but i'm trying to add lots of differents techniques in it.

It's all good when you take a single tutorial (I followed some tutorials for the screen managers, for diffuse lightning, for camera, HUD, particles, spritesheet animations, etc. ) It's all quite simple to get it to works and run in a simple game.cs file.

the problem I have is when its time to all mix it toghether. I cant find any good pointers on how I should handle the spritebatch orderings. Should I use different spritebatch, or reuse the same over and over ? I was told I need to utilise as little spritebatch as possible ...

** I KNOW i could test it and start to wonder about it only if I have a problem ... but instead I'd prefer to learn to do it right the 1st time I try it. If I get into a bigger project and this way of doing it only works on easy small 2d game ... its not much better than saying I've never done anything.

More specificly, I'm wondering if I keep my main Draw set up like this :

 graphicsDevice.Clear(Color.Black);            

 spriteBat.Begin(paramteres set up for my camera); 
   for each screen in screenmanager {
     screen.draw(spritebatch);
   }
 spriteBat.End(); 


  spriteBat.Begin(paramteres set up for my HUD); 
     //draw hud stuff
  spriteBat.End();

....

and in my screens draw method ... i'll end up with :

void Draw(spritebat Spritebatch)
{
     //draw screen "normal" item affected by camera 
  spriteBat.End(); 

  spriteBat.Begin(paramteres set up for particle); 
    for each particlelist in particlemanager {
      for each particle in particleList
        particle.draw(spritebatch);
      }
    }
   spriteBat.End(); 


 spriteBat.Begin(paramteres set up for my light FX); 
    //draw diffuse light and shadows stuff
 spriteBat.End();

 spriteBat.Begin(paramteres set up for my camera); 
}

is this correct ?

\$\endgroup\$
1
  • \$\begingroup\$ should I move this into a code review oriented site ? its bordeline on "best practice" questions that will often ended up closed. \$\endgroup\$ Jul 4, 2014 at 18:16

1 Answer 1

1
\$\begingroup\$

Yeah, this is how spritebatch is meant to be used. It takes a view matrix in the begin parameter and uses that throughout the begin/end pair. You could argue that spritebatch would be more flexible with matrices if you could pass a view matrix in for each .draw call, but evidently the XNA team decided against this for one reason or another. To answer your question, having two seperate begin/ends won't make THAT much difference overall. Tutorials often make a note of using as little begin/end as possible to make the reader knows that it should not be called once for every draw call or nested somewhere in a class' .draw method.

The biggest problem I've found this leading to is when you have a .draw method in another class that relies on a spritebatch being begun. If that is the case, this method can only draw with one view matrix. This can be a bit of a problem, but shouldn't be too hard to get around.

Spritebatch .begin changes a lot of the state with the GPU. This is pretty expensive, but two calls per frame is very reasonable and shouldn't be a problem. If your 2D application is lagging on a decent modern computer, you should look for issues elsewhere.

\$\endgroup\$
10
  • \$\begingroup\$ Well, technically it could be alot more than 2, since each screens will open and close the spritebatch. I could have up to 3 or 4 screens at the same time \$\endgroup\$ Jul 5, 2014 at 23:24
  • \$\begingroup\$ Screens? You mean different viewports? \$\endgroup\$
    – Ben
    Jul 6, 2014 at 1:48
  • \$\begingroup\$ screens are just an object that holds what you see on the screen .. like "windows". I have a mini-map "screen", my hud "screen", world map "screen", battle-map "screen", combat "screen", inventory "screen", etc ... \$\endgroup\$ Jul 7, 2014 at 12:02
  • \$\begingroup\$ They shouldn't be drawn if they're not visible. They aren't ALL visible at the same time, right? \$\endgroup\$
    – Ben
    Jul 7, 2014 at 12:04
  • \$\begingroup\$ they could be. You can have a battle map, inventory, hud, et maybe an alert/acheivement message on at the same time. ex : Achievement is a small, a 200x200 "windows". \$\endgroup\$ Jul 7, 2014 at 13:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .