Game Development Reference
In-Depth Information
13.4 Correcting the Mouse Position
Now that we have changed the resolution of the game, we need to solve a couple of
things. One thing now is that the mouse position will no longer always be correct,
since we will be scaling the sprites later on. Therefore, we also need to scale the
mouse position. We are going to extend the InputHelper class for that. First, we add a
Vector2 member variable in that class to store the current scale:
protected Vector2 scale;
We also define a property Scale to be able to read and write the member variable:
public Vector2 Scale
{
get { return scale; }
set { scale = value ;}
}
Then, we have to modify the MousePosition property to take this scale into account:
public Vector2 MousePosition
{
get { return new Vector2(currentMouseState.X, currentMouseState.Y) / scale; }
}
In the SetFullScreen method, we then set the properties to the right values:
inputHelper.Scale = new Vector2(( float )GraphicsDevice.Viewport.Width / screen.X,
( float )GraphicsDevice.Viewport.Height / screen.Y);
Note that we recalculate what the scale is using the GraphicsDevice property. We
do this to ensure that we get the right scale values. Remember that setting the
PreferredBackBufferWidth property does not guarantee that the game will indeed get
this particular resolution. The game engine may decide that the final screen size is
slightly different depending on the limitations of the graphics output device.
13.5 Scaling the Sprites
Now that we set the right resolution, we will need to scale the sprites when we
draw them so that they correspond to the scaled resolution. If we did not do this, our
viewport resolution would be correct, but the sprites would be drawn at their original
size. Scaling the sprites sounds a lot more complicated than it is. XNA provides a
very simple way to scale everything up or down, using the Matrix class (this refers to
the mathematical construction, not the movie). First, we declare a member variable
spriteScale and then we use the CreateScale method to create a scaling matrix:
Search WWH ::




Custom Search