title: Scaling a Texture2D in XNA
body: <p>I have a 160 x 80 button in a window with resolution of 1280 x 720. I draw it as followed(I paraphrase a bit here):</p>
  
  <pre><code>currentwindowwidth = 1280;
  // The scale in this instance would be 1.0f
  scale = currentwindowwidth / 1280f;
  b.position = new Vector2(640, 650) * scale;
  b.origin = new Vector2(80, 40) * scale;
  spriteBatch.Draw(b.texture, b.pos, null, Color.White, 0, b.ori, scale, SpriteEffects.None, 0);
  </code></pre>
  
  <p>When I draw the window at 1280 x 720, the button is drawn in the correct location and the button detection:</p>
  
  <pre><code>if (mousepos.X &gt;= b.pos.X - b.ori.X &amp;&amp; mousepos.X &lt;= b.pos.X + b.ori.X)
  {
      if (mousepos.Y &gt;= b.pos.Y - b.ori.Y &amp;&amp; mousepos.Y &lt;= b.pos.Y + b.ori.Y)
      {
          HandleButton(b);
      }
  }
  </code></pre>
  
  <p>works 100%. The button detects my mouse clicks to pixel perfect precision, and the texture2d is drawn faithfully to the detection box.</p>
  
  <p>However, when I scale up to 1920 x 1080 (same aspect ratio), the button is drawn in the incorrect location:</p>
  
  <pre><code>currentwindowwidth = 1920;
  // The scale would be 1.5f here
  scale = currentwindowwidth / 1280f;
  b.position = new Vector2(640, 650) * scale;
  b.origin = new Vector2(80, 40) * scale;
  spriteBatch.Draw(b.texture, b.pos, null, Color.White, 0, b.ori, scale, SpriteEffects.None, 0);
  </code></pre>
  
  <p>This will now give me a position vector of (960, 975) and an origin of (120, 60)(&lt;-- which I find in the "Locals" tab in visual studio). After doing some calculations these are the correct values, and the button detection works exactly where I want it to work. HOWEVER the sprite is not drawn in the correct location, it is drawn (-60, -30) pixels offset from where it should be. When scaled up the button detection works in the correct location(840-1080, 915-1035) but the texture2d is drawn in the wrong place. </p>
  
  <p>This happens as well when I scale the screen to another 16:9 resolution such as 1366 x 768, except not (-60, -30) off but proportionally (-8.0625, -4.03125) and (-30, -15) at 1600 x 900. </p>
  
  <p>This effect is also witnessed when I use resolutions smaller than 1280 x 720 (854 x 480), but in the opposite direction (a positive offset vector instead of a negative one). </p>
  
  <p>In every 16:9 resolution the button detection works as calculated, but when I scale the texture2d it is drawn in the incorrect place. My only guess is that the scale scale's the texture2d in ways I am not aware of.</p>
  
  <p>If anybody else has any experience on the issue I would much appreciate it,</p>
  
  <p>Ken</p>