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]):

   new Vector2(
   (Window.ClientBounds.Width / 2) - (texture.Width / 2),
   (Window.ClientBounds.Height / 2) - (texture.Height / 2)),

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,
         - frameSize.Y));

      speed = new Vector2(((Game1)Game).rnd.Next(
         enemyMaxSpeed), 0);


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.

  • \$\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\$ Jun 17 '12 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\$ Jun 17 '12 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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