I have bit of a silly problem.

Is is possible to use SpriteBatch.Draw() method to draw to the centre of screen using coords from the beginning of window (So resolution 800x600 would use 400;300) instead of coords from the beginning of the Game coordinates? Right now I'm doing calculations to keep the object in the centre of screen but I'm wondering if theres easier method how to solve this.

I tried googling, but either this hasn't been discussed anywhere or I'm using wrong keywords in search.

Thank you.

  • \$\begingroup\$ Can you expound upon what you mean? Are you looking to draw in the center of the physical screen (monitor center), or the center of an application's window with a Game running within it? Or, is it something entirely different? \$\endgroup\$ – Zachary Lewis Mar 14 '12 at 19:57

I find your question very confusing but I believe that this will do what you want. Assuming you want the center of the object to be draw to the center of the screen irregardless of resolution.

var screenCenter = new Vector2(
    GraphicsDevice.Viewport.Bounds.Width / 2,
    graphicsDevice.Viewport.Bounds.Height / 2);
var textureCenter = new Vector2(
    Texture2D.Width / 2,
    Texture2D.Height / 2);
SpriteBatch.Draw(Texture2D, screenCenter, null, Color.White, 0f, textureCenter, 1f, SpriteEffects.None, 1f)

The best way to do this would be to use the GameWindow class found in Window member of your Game class.

Vector2 ScreenCenter = new Vector2(
    Game1.Window.ClientBounds.Width / 2, 
    Game1.Window.ClientBounds.Height / 2);

That should get you the center point for the SpriteBatch object.


You can get the width and height of the window from the Graphics device manager. Assign the values to some variables and draw based off of that.

 width = graphics.PreferredBackBufferWidth;
 height = graphics.PreferredBackBufferHeight;

And then to draw it you would just base it off those values:

spriteBatch.Draw(texture, new Vector2(width / 2, height / 2), Color.White);
  • \$\begingroup\$ PreferredBackBufferWidth/Height is not necessarily the actual width and height, thus the preferred part. Use the GraphicsDevice.Viewport instead. \$\endgroup\$ – ClassicThunder Mar 14 '12 at 18:57
  • 1
    \$\begingroup\$ I think the asker tries to find the center of the screen, not the center of the Window. This is not possible with the XNA API alone but with some native calls you can probably find the position of the window on the screen and the size of the screen. \$\endgroup\$ – Roy T. Mar 14 '12 at 19:40
  • \$\begingroup\$ Ahh thank you for clarifying. I'll fix my answer after I get off work. \$\endgroup\$ – ClassicThunder Mar 14 '12 at 19:51

If you want to draw your object in the very center, u can use (pseudocode, see above for implementations)

(screenWidth - objectWidth) / 2;
(screenHeight - objectHeight) / 2;

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.