简体   繁体   中英

XNA scale resolution while keeping aspect ratio?

Okay so we've been having this problem with a project we're working on.

We're working with a base resolution of 500 by 1000 pixels, and everything is placed and moved by that resolution. What we want to achieve is to have the base resolution be scaled (while maintaining the aspect ratio) to the actual resolution, which might be anything. What I mean by this is that all objects are scaled (including the mouse). It should also work in fullscreen.

We've tried using Matrix.CreateScale() in the SpriteBatch.Begin() method, but it did not preserve the aspect ratio and didn't work in fullscreen.

We're just really confused what to set the PreferredBackBufferWidth/Height and Viewport.X/Y/Width/Height values to and when.

If someone could point us in the right direction or provide some examples we'd appreciate it.

Thanks!

Assuming you truly want to just scale everything to the new resolution, you can just draw everything in 500x1000 to a RenderTarget2D, and then draw this to the actual graphics device at whatever resolution you want.

Note however that this will stretch the image, and quite possibly make it look very weird if you scale it up too large. But regardless, this is how to do it.

Now to also have Mouse coordinates translated properly:

var mouseState = Mouse.GetState();
var translatedX = mouseState.X * (game.Width / screen.Width)
var translatedY = mouseState.Y * (game.Height / screen.Height)
Vector2 gameMouseCoordinates = new Vector2(translatedX, translatedY);

in this case, game Width/Height is 500x1000, and screen Width/Height would be the actual resolution (for example 1920x1080).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM