1
\$\begingroup\$

I'm reading a book called "Learning XNA 4.0" written by Aaron Reed. Throughout most of the chapters, whenever he calculates the position of a sprite to use in his call to SpriteBatch.Draw, he uses Window.ClientBounds.Width and Window.ClientBounds.Height. But then all of a sudden, on page 108, he uses PresentationParameters.BackBufferWidth and PresentationParameters.BackBufferHeight instead.

I think I understand what the Back Buffer and the Client Bounds are and the difference between those two (or perhaps not?). But I'm mighty confused about when I should use one or the other when it comes to positioning sprites. The author uses for the most part Client Bounds both for checking whenever a moving sprite is of the screen and to find a spawn point for new sprites. However, he seems to make two exceptions from this pattern in his book. The first time is when he wants some animated sprites to "move in" and cross the screen from one side to another (page 108 as mentioned). The second and last time is when he positions a texture to work as a button in the lower right corner of a Windows Phone 7 screen (page 379).

Anyone got an idea?

I shall provide some context if it is of any help. Here's how he usually calls SpriteBatch.Draw (code example from where he positions a sprite in the middle of the screen [page 35]):

spriteBatch.Draw(texture,
   new Vector2(
   (Window.ClientBounds.Width / 2) - (texture.Width / 2),
   (Window.ClientBounds.Height / 2) - (texture.Height / 2)),
   null,
   Color.White,
   0,
   Vector2.Zero,
   1,
   SpriteEffects.None,
   0);

And here is the first case of four possible in a switch statement that will set the position of soon to be spawned moving sprites, this position will later be used in the SpriteBatch.Draw call (page 108):

// Randomly choose which side of the screen to place enemy,
// then randomly create a position along that side of the screen
// and randomly choose a speed for the enemy
switch (((Game1)Game).rnd.Next(4))
{
   case 0: // LEFT to RIGHT
      position = new Vector2(
         -frameSize.X, ((Game1)Game).rnd.Next(0,
         Game.GraphicsDevice.PresentationParameters.BackBufferHeight
         - frameSize.Y));

      speed = new Vector2(((Game1)Game).rnd.Next(
         enemyMinSpeed,
         enemyMaxSpeed), 0);
   break;
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Neither.

Use GraphicsDevice.Viewport, as that is the region that SpriteBatch actually uses to do its drawing.

See also this answer for when you should use each of the available options.

\$\endgroup\$
2
  • \$\begingroup\$ Thanx, although something bugs me. The docs say that the destinationrectangle is "a rectangle specifying, in screen coordinates, where the sprite will be drawn". The word screen is used. At another MSDN-page it is written like so: "Sprites are positioned on the screen by coordinates. The width and height of the screen is the same as the back buffer." The book XNA Game Studio 4.0 Programming do it like you and uses the viewport. \$\endgroup\$ Commented Jun 17, 2012 at 9:27
  • \$\begingroup\$ They seem to be using the term "screen coordinates" to name the coordinate system that I usually call "client coordinates". I think my terminology is more precise, but it doesn't really matter. However the documentation is (strictly speaking) incorrect where it says "back buffer". It should read "viewport". The reason for the inaccuracy is probably to simplify things for an introductory article. \$\endgroup\$ Commented Jun 17, 2012 at 11:20

You must log in to answer this question.

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