Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually there is a very simple solution to this. As Pinckerman did point out, you need to know the aspect ratio (screen ratio), but you will be using it in the SpriteBatch.Begin method, where you will pass the Vector2 scale through a Matrix.CreateScale(Vector3 Here, where Z is 1), and use it as final argument in the SpriteBatch.Begin method.</p> <p>Most other arguments in this case can be left Null.</p> <p>Basically what it does is, it tells the spritebatch to draw everything with an offset (the scale) applied to both position and size. This means that when, in my case, I place something on 640x400, no matter what resolution I run in the screen, it still starts off dead center of the screen. Quite useful if you ask me.</p> <p>Example from my current project:</p> <pre><code>/// &lt;summary&gt; /// Resolution /// &lt;/summary&gt; public static class Resolution { private static Vector3 ScalingFactor; private static int _preferredBackBufferWidth; private static int _preferredBackBufferHeight; /// &lt;summary&gt; /// The virtual screen size. Default is 1280x800. See the non-existent documentation on how this works. /// &lt;/summary&gt; public static Vector2 VirtualScreen = new Vector2(1280, 800); /// &lt;summary&gt; /// The screen scale /// &lt;/summary&gt; public static Vector2 ScreenAspectRatio = new Vector2(1, 1); /// &lt;summary&gt; /// The scale used for beginning the SpriteBatch. /// &lt;/summary&gt; public static Matrix Scale; /// &lt;summary&gt; /// The scale result of merging VirtualScreen with WindowScreen. /// &lt;/summary&gt; public static Vector2 ScreenScale; /// &lt;summary&gt; /// Updates the specified graphics device to use the configured resolution. /// &lt;/summary&gt; /// &lt;param name="device"&gt;The device.&lt;/param&gt; /// &lt;exception cref="System.ArgumentNullException"&gt;device&lt;/exception&gt; public static void Update(GraphicsDeviceManager device) { if (device == null) throw new ArgumentNullException("device"); //Calculate ScalingFactor _preferredBackBufferWidth = device.PreferredBackBufferWidth; float widthScale = _preferredBackBufferWidth / VirtualScreen.X; _preferredBackBufferHeight = device.PreferredBackBufferHeight; float heightScale = _preferredBackBufferHeight / VirtualScreen.Y; ScreenScale = new Vector2(widthScale, heightScale); ScreenAspectRatio = new Vector2(widthScale / heightScale); ScalingFactor = new Vector3(widthScale, heightScale, 1); Scale = Matrix.CreateScale(ScalingFactor); device.ApplyChanges(); } /// &lt;summary&gt; /// &lt;para&gt;Determines the draw scaling.&lt;/para&gt; /// &lt;para&gt;Used to make the mouse scale correctly according to the virtual resolution, /// no matter the actual resolution.&lt;/para&gt; /// &lt;para&gt;Example: 1920x1080 applied to 1280x800: new Vector2(1.5f, 1,35f)&lt;/para&gt; /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public static Vector2 DetermineDrawScaling() { var x = _preferredBackBufferWidth / VirtualScreen.X; var y = _preferredBackBufferHeight / VirtualScreen.Y; return new Vector2(x, y); } } </code></pre> <p>And usage:</p> <pre><code> /// &lt;summary&gt; /// Draws the game objects to the screen. Calls Root.Draw. /// &lt;/summary&gt; /// &lt;param name="gameTime"&gt;The game time.&lt;/param&gt; protected override void Draw(GameTime gameTime) { // TODO: Camera SpriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, Resolution.Scale); Root.Draw(SpriteBatch, gameTime); SpriteBatch.End(); base.Draw(gameTime); } </code></pre> <p>Hope this example code is helpful.</p> <p>NOTE: Doing it the way Pinckerman suggested isn't a wrong way, however I'm led to believe that apart from the obvious (initially more code, but in the long run way less), what Pinckerman suggested may also take up more performance. This is only a hunch, and it's merely based on the GPU probably being more efficient in calculations related to screen placement than the CPU.</p> <p>But don't hang me up on it.</p> <p>@Edit:</p> <p>To get Mouse coordinates in relation to objects on the screen:</p> <pre><code>public Vector2 GetMouseCoords() { var screenCoords = Mouse.GetPosition(); // Or whatever it's called. var sceneCoords = screenCoords / Resolution.ScreenScale; return sceneCoords; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload