Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Setting backbuffer and creating camera on initialisation
- var metrics = new Android.Util.DisplayMetrics();
- Activity.WindowManager.DefaultDisplay.GetMetrics(metrics);
- graphics.IsFullScreen = true;
- graphics.PreferredBackBufferWidth = (int)(metrics.WidthPixels / metrics.Density);
- graphics.PreferredBackBufferHeight = (int)(metrics.HeightPixels / metrics.Density);
- graphics.SupportedOrientations = DisplayOrientation.Portrait;
- graphics.ApplyChanges();
- scaler = new ResolutionIndependentScaler(this, graphics);
- scaler.SetVirtualResolution(GAME_WIDTH, GAME_HEIGHT); // (224, 368);
- Components.Add(scaler);
- // ***********************************************************************************
- // Drawing code
- spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, null, null, null, scaler.GetTransform());
- // draw stuff yadayadayada
- spriteBatch.End();
- // ************************************************************************************
- // 'Camera' code:
- // not really a camera in the typical sense, it's given a virtual resolution which is
- // upscaled to the target resolution
- public class ResolutionIndependentScaler : GameComponent
- {
- private bool isResizing;
- private GraphicsDeviceManager graphics;
- private int gameWidth, gameHeight;
- private bool isDirty;
- private Vector2 position;
- private float scale { get; set; }
- private Matrix transform { get; set; }
- public ResolutionIndependentScaler(Game game, GraphicsDeviceManager graphics) : base(game)
- {
- game.Window.ClientSizeChanged += Window_ClientSizeChanged;
- this.graphics = graphics;
- }
- public void SetVirtualResolution(int width, int height)
- {
- gameWidth = width;
- gameHeight = height;
- float scaleWidth = (float)(Game.GraphicsDevice.Viewport.Width / (float)width);
- float scaleHeight = (float)(Game.GraphicsDevice.Viewport.Height / (float)height);
- scale = Math.Min(scaleWidth, scaleHeight);
- }
- private void Window_ClientSizeChanged(object sender, EventArgs e)
- {
- // force a recalculation of the scaling
- isResizing = true;
- }
- public void SetPosition(Vector2 position)
- {
- if (position != this.position)
- {
- isDirty = true;
- this.position = position;
- }
- }
- public override void Update(GameTime gameTime)
- {
- if (isResizing)
- {
- isResizing = false;
- graphics.PreferredBackBufferWidth = Game.Window.ClientBounds.Width;
- graphics.PreferredBackBufferHeight = Game.Window.ClientBounds.Height;
- graphics.ApplyChanges();
- SetVirtualResolution(gameWidth, gameHeight);
- }
- if (isDirty)
- {
- isDirty = false;
- transform = Matrix.CreateTranslation(new Vector3(-position.X, -position.Y, 0)) *
- Matrix.CreateRotationZ(0) *
- Matrix.CreateScale(new Vector3(scale, scale, 1)) *
- Matrix.CreateTranslation(new Vector3(Game.GraphicsDevice.Viewport.Width * 0.5f, Game.GraphicsDevice.Viewport.Height * 0.5f, 0));
- }
- base.Update(gameTime);
- }
- public Matrix GetTransform()
- {
- return transform;
- }
- public Vector2 WorldToScreen(Vector2 position)
- {
- return Vector2.Transform(position, GetTransform());
- }
- public Vector2 ScreenToWorld(Vector2 position)
- {
- return Vector2.Transform(position, Matrix.Invert(GetTransform()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment