When you have to display a series of visual components (sprites) within the context of a game each taking a literal height and width that needs to be relative to the height & width of the Viewport (not necessarily aspect ratio) of the target device:
- Is there a scaling class to help come up with scaling ratio in a dynamic fashion based on current device viewport size?
- Will I need to roll my own scaling ratio algorithm?
- Any cross platform issues I should be aware of?
This is not a question relating to the loading of assets based on target device nor is it a question of how to perform the scaling of the sprite (which is described here: http://msdn.microsoft.com/en-us/library/bb194913.aspx), rather a question of how to determine the scale of sprites based on view port size.
You can always create your own implementation of scaling.
For example, the default target viewport dimensions are:
And your current screen dimensions are
800×600, which gives you a (let’s use aVector2instead of two floats):This gives you a
{0.625; 0.83333}. You can now use this in a handySpriteBatch.Draw()overload that takes aVector2scaling variable:Alternatively, you can draw all your stuff to a
RenderTarget2Dand copy the resulting image from there to a stretched texture on the main screen, but that will still require the aboveSpriteBatch.Draw()overload, though it might save you time if you have lots of draw calls.