I have this code that is supposed to draw two volume icons to the window, but it is not working. Here is the relevant code:
Texture2D vol_max;
Vector2 vol_max_vect;
Texture2D vol_min;
Vector2 vol_min_vect;
...
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
vol_max = Content.Load<Texture2D>("vol_max@16");
vol_min = Content.Load<Texture2D>("vol_min@16");
}
protected override void Update(GameTime gameTime)
{
thisKeyboard = Keyboard.GetState(PlayerIndex.One);
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
thisKeyboard.IsKeyDown(Keys.Escape))
{
this.Exit();
}
// Update window vectors
vol_max_vect = new Vector2(
(float)(Window.ClientBounds.Right - 20),
(float)(Window.ClientBounds.Bottom - 20));
vol_min_vect = new Vector2(
(float)(Window.ClientBounds.Right - 140),
(float)(Window.ClientBounds.Bottom - 20));
prevKeyboard = thisKeyboard;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(
vol_max,
vol_max_vect,
Color.White);
spriteBatch.Draw(
vol_min,
vol_min_vect,
Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
The issue is that the ClientBounds.Right/Bottom is in Windows screen coordinates (where [0,0] is the top left of your screen, and the bottom right is your resolution, eg. [1024, 768]).
What you really want is to draw them at the bottom right of your own window. XNA’s SpriteBatch draws in viewport coordinates, where [0, 0] is the top left of your viewport, and the bottom right is your application’s resolution eg. [800, 480]. To get that width, you can simply use Window.ClientBounds.Width instead of Window.ClientBounds.Right, and Window.ClientBounds.Height instead of Window.ClientBounds.Bottom.
Hopefully that helps!