Game Development Reference
In-Depth Information
float finalscale = 1;
Now we have to handle the two cases (fullscreen or not). If we are not in
fullscreen mode, then we want to scale down if the window does not entirely fit
on the screen. If the desired x screen size is larger than the display screen, the scalex
variable will contain a value smaller than 1. And the same goes for the y screen
size. In other words, if either the scalex or scaley variable is smaller than 1, we want
to scale down the size of the window so that it fits on the screen. And to ensure the
window always fits on the screen, we would have to choose the smallest scale in that
case:
if (!fullscreen)
{
if (scalex < 1f || scaley < 1f)
finalscale = Math.Min(scalex, scaley);
}
In case we are in fullscreen mode, we will either scale up or down (or not scale
at all in very few cases). We want to scale the image as little as possible to avoid too
much distortion, so we want to choose the scale that is closest to 1. We can do that
as follows:
float finalscale = scalex;
if (Math.Abs(1
scaley) < Math.Abs(1 scalex))
finalscale = scaley;
Here we use the Math.Abs method which returns the absolute value of a numerical
type value passed as a parameter. So, in this if instruction we check if the difference
between the y scale and 1 is smaller than the difference between the x scale and 1.
If so, we choose the y scale as the final scaling value. Now that we have calculated
which scale we are going to use, we apply this scale to calculate what the new width
and height should be, and set that as the desired screen size:
graphics.PreferredBackBufferWidth = ( int )(finalscale
screen.X);
screen.Y);
graphics.PreferredBackBufferHeight = ( int )(finalscale
Then, we set the mode to either fullscreen or not, depending on the value of the
variable passed as a parameter, and we apply the changes to the graphics device:
graphics.IsFullScreen = fullscreen;
graphics.ApplyChanges();
Search WWH ::




Custom Search