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 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.

share|improve this question
    
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? –  Zachary Lewis Mar 14 '12 at 19:57
add comment

4 Answers

up vote 5 down vote accepted

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)
share|improve this answer
add comment

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.

share|improve this answer
add comment

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);
share|improve this answer
    
PreferredBackBufferWidth/Height is not necessarily the actual width and height, thus the preferred part. Use the GraphicsDevice.Viewport instead. –  ClassicThunder Mar 14 '12 at 18:57
1  
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. –  Roy T. Mar 14 '12 at 19:40
    
Ahh thank you for clarifying. I'll fix my answer after I get off work. –  ClassicThunder Mar 14 '12 at 19:51
add comment

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;
share|improve this answer
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.