Letterboxing and scaling in XNA on PC

Is there a way that I could in principle develop my XNA game at 1080p (or 720p) as the default resolution, and then depending on the resolution you set, just scale everything in the game to the right size without setting a scale factor in each Sprite method Draw ()?

I think that I could develop all the graphics, adjust the coordinates, etc. based on 1080p resolution, but then for XBOX just set res to 720p and zoom out (so that XBOX sees everything as 720 and therefore processes all the resolutions mentioned in the developer docs) and on the PC scale, to any resolution you need or aspect ratios by automatically assigning a view to a mailbox for permissions that are not 16: 9.

I already had the game setup, so that spritebatch.begin () and end () were called at the highest level, around all the other Draw calls, so I could technically just go through the scaling matrix there, but whenever I do this so that he does something strange, like making a view from the center or occupying only a quarter of the screen.

Is there a better way to achieve this?

+1
source share
1 answer

If you set the scaling matrix to SpriteBatch.Begin , then it will scale the sizes and positions of each individual sprite you draw with that SpriteBatch , until you name End .

SpriteBatch uses the client space, where zero is the upper left corner of the Viewport , and one element in this space is equivalent to one pixel in the viewport.

When you give the SpriteBatch transform, the sprites you draw will have the transform applied to them for you before they are drawn. Thus, you can (and should) use the same technique to translate your scene (for example, to focus it on your player).

For instance:

Your game is developed at 720p and you use SpriteBatch without conversion. You have a sprite centered in the lower right corner. Let's say its texture is (32, 32) pixels, and the origin sprite is (16, 16) (the origin is indicated in the texture space, so this is the center of the sprite). The sprite position is (1280, 720). The scale sprite is 1, which leads to its resulting size (32, 32). You will see the upper left quarter of the sprite in the lower right corner of the screen.

Now you are switching to a 1080p screen (1.5 times larger than 720p). If you do not add a scaling matrix to SpriteBatch , you can see the entire sprite with its two-thirds of the screen to the right and down.

But you want to enlarge your entire scene so that at 1080p it looks the same as at 720p. So you add the Matrix.CreateScale(1.5f, 1.5f, 1f) matrix Matrix.CreateScale(1.5f, 1.5f, 1f) (note using 1 for the Z axis, because it's 2D is not 3D) for your SpriteBatch.Begin and don't do anything.

Now your sprite scene will be increased 1.5 times more. Without any changes to the actual Draw call, your sprite will be drawn at (1920, 1080) (in the lower right corner of the screen), its size will be (48, 48) (1.5 times larger) and its origin will still be will be the center. You will see the upper left quadrant of the sprite in the lower right corner of the screen, as with 720p, and with the same relative size.

+4
source

All Articles