Switch to full screen mode without stretching in an XNA game

I have a 2D game I'm working on, it's 4: 3 aspect ratio. When I switch it to full screen mode on my widescreen monitor, it stretches. I tried using two viewports to give a black background where the game should not stretch, but this left the game in the same size as before. I could not get it to fill the viewing area, which was supposed to contain the entire game.

How can I make it work in full screen mode without stretching and without me to change each position and make an expression in the game? The code I use for viewports is below.

// set the viewport to the whole screen GraphicsDevice.Viewport = new Viewport { X = 0, Y = 0, Width = GraphicsDevice.PresentationParameters.BackBufferWidth, Height = GraphicsDevice.PresentationParameters.BackBufferHeight, MinDepth = 0, MaxDepth = 1 }; // clear whole screen to black GraphicsDevice.Clear(Color.Black); // figure out the largest area that fits in this resolution at the desired aspect ratio int width = GraphicsDevice.PresentationParameters.BackBufferWidth; int height = (int)(width / targetAspectRatio + .5f); if (height > GraphicsDevice.PresentationParameters.BackBufferHeight) { height = GraphicsDevice.PresentationParameters.BackBufferHeight; width = (int)(height * targetAspectRatio + .5f); } //Console.WriteLine("Back: Width: {0}, Height: {0}", GraphicsDevice.PresentationParameters.BackBufferWidth, GraphicsDevice.PresentationParameters.BackBufferHeight); //Console.WriteLine("Front: Width: {0}, Height: {1}", width, height); // set up the new viewport centered in the backbuffer GraphicsDevice.Viewport = new Viewport { X = GraphicsDevice.PresentationParameters.BackBufferWidth / 2 - width / 2, Y = GraphicsDevice.PresentationParameters.BackBufferHeight / 2 - height / 2, Width = width, Height = height, MinDepth = 0, MaxDepth = 1 }; GraphicsDevice.Clear(Color.CornflowerBlue); 

The following illustration shows what the screen looks like. Black on the sides is what I want (and from the first viewport), and the second viewport is the game and the blue is cornflower blue. I want to get the scale of the game to fill the blue area of โ€‹โ€‹the cornflower.

Not correctly scaling

+6
c # xna
source share
4 answers

As in commercial games, you must give the user the ability to switch between aspect 4: 3 and aspect 16: 9. You should be able to simply change the cameraโ€™s viewing ratio.

EDIT:

As far as Iโ€™ve seen, there are no games that "automatically determine" the appropriate aspect ratio to use.

As already noted, there are ways to make a good guess about the correct aspect ratio. If XNA allows you to get the current Windows user screen settings, you can determine the aspect ratio based on the resolution of the monitor.

Once you have determined the resolution of the monitor for the user, you can better decide how to deal with it. At first, the best option would be to simply place the black bars on the left / right side of the screen to provide a full-screen mode with a 16: 9 aspect ratio, which essentially still uses 4: 3 artwork.

In the end, you can change the game so that it changes the size of the viewing port when the aspect ratio is 16: 9. This would not require changing any artistic assets as they look.

0
source share

First of all, I assume that you are talking about XNA 4.0, in which AFAIK breaks the changes between XNA 3.x and XNA 4.0.

I am relatively new to XNA, but it seems to me that your assets do not fit the window size. Say your game is 320x240, and your window is larger, for example. 640x480.

So you can specify a PreferredBuffer to extend the application. So, tell XNA that you are going to use 320x240 by setting the following values:

 _graphics.PreferredBackBufferWidth = 320; _graphics.PreferredBackBufferHeight = 240; 

In addition, you can start full-screen mode by setting:

 _graphics.IsFullScreen = true; 

In addition, you must manually handle how the elements must resize after the window is resized.

Make your choice. https://github.com/hmadrigal/xnawp7/tree/master/XNASample02

(BTW, you can press F11 to switch between full screen and normal view)

Regards, Herber

0
source share

I'm not sure if you can really scale your viewport. I understand what you are trying to do, but for this you will need to do the following.

  • Set the width and height of the screen buffer to a resolution of 16: 9.
  • The program is moving so that objects do not draw within these boundaries.

Nowadays, all the main games, if you play them on a 16: 9 monitor and choose a resolution of 4: 3, will stretch to fit the screen. This is not something you usually want to overcome. You either support many resolutions in your game, or you get stretching when the user uses the wrong resolution for their screen.

Usually, each person customizes their game, and their textures work based on the relative sizes of the current viewport or the width and height of the buffer. Thus, regardless of the resolution entered, the game is scaled to work with this width / height ratio.

This is a bit more work, but in the end, makes your game much more polished and compatible with a wide range of systems.

The only time this may not be done is when the application runs in a window (not in full screen mode).

0
source share

All Articles