Game Development Reference
In-Depth Information
else
{
Dealwithfullscreenmode
}
graphics.ApplyChanges();
...
}
There are a few interesting things happening in this method. First, if you look at the
parameter, you see that it is assigned a value. This is a feature in the C# program-
ming language allowing us to define default values for parameters. In this case, the
default value for the parameter is true . This means that we could call the method as
this .SetFullScreen(); , which would be equivalent to this .SetFullScreen( true ); . Default
values can come in handy sometimes, since you can avoid defining a lot of parame-
ters that you do not care about, making your code easier to read.
In the if instruction, we deal with the two cases: windowed mode, or fullscreen
mode. After that, we apply the changes to the graphics device. Also, we store the
actual screen resolution by accessing the width and height of the viewport. We need
to get this information from the viewport instead of simply storing the resolution,
because in some cases, the two can be different, especially when we are in fullscreen
mode. Also in some other cases, the graphics device manager may choose to use an-
other resolution than the one we specify because of limitation of the graphics device.
That is why a property used to set the resolution are called PreferredBackBufferWidth
and not simply BackBufferWidth .
When we want to determine if we have to scale anything, we first have to find
out what the current screen resolution is. For that, we can use the GraphicsAdapter
class that is provided by XNA. For example, we can get the current width using
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width . We can use this to calculate
with what factor we should scale the x game screen width to match the actual total
width of the screen:
float scalex = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width /
( float )screen.X;
The value that we calculate here is either less than 1 (so, we need to scale down in
order to fit the desired size on the screen), equal to one (the screen size matches the
desired size), or greater than 1 (we need to scale up the image in order to fit on the
screen). Similarly, we calculate this value for the y direction:
float scaley = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height /
( float )screen.Y;
If we want to maintain the correct aspect ratio, we need to choose one of these scales
and use it to scale the image. Let us store this scale in a local variable finalscale ,
which will initially be one (no scaling):
Search WWH ::




Custom Search