Guest User

Untitled

a guest
Jan 5th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. // Setting backbuffer and creating camera on initialisation
  2. var metrics = new Android.Util.DisplayMetrics();
  3. Activity.WindowManager.DefaultDisplay.GetMetrics(metrics);
  4.  
  5. graphics.IsFullScreen = true;
  6. graphics.PreferredBackBufferWidth = (int)(metrics.WidthPixels / metrics.Density);
  7. graphics.PreferredBackBufferHeight = (int)(metrics.HeightPixels / metrics.Density);
  8. graphics.SupportedOrientations = DisplayOrientation.Portrait;
  9. graphics.ApplyChanges();
  10.  
  11. scaler = new ResolutionIndependentScaler(this, graphics);
  12. scaler.SetVirtualResolution(GAME_WIDTH, GAME_HEIGHT); // (224, 368);
  13. Components.Add(scaler);
  14.  
  15. // ***********************************************************************************
  16. // Drawing code
  17. spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, null, null, null, scaler.GetTransform());
  18. // draw stuff yadayadayada
  19. spriteBatch.End();
  20.  
  21. // ************************************************************************************
  22. // 'Camera' code:
  23. // not really a camera in the typical sense, it's given a virtual resolution which is
  24. // upscaled to the target resolution
  25.     public class ResolutionIndependentScaler : GameComponent
  26.     {
  27.         private bool isResizing;
  28.         private GraphicsDeviceManager graphics;
  29.  
  30.         private int gameWidth, gameHeight;
  31.  
  32.         private bool isDirty;
  33.         private Vector2 position;
  34.  
  35.         private float scale { get; set; }
  36.  
  37.         private Matrix transform { get; set; }
  38.  
  39.         public ResolutionIndependentScaler(Game game, GraphicsDeviceManager graphics) : base(game)
  40.         {
  41.             game.Window.ClientSizeChanged += Window_ClientSizeChanged;
  42.             this.graphics = graphics;
  43.         }
  44.  
  45.         public void SetVirtualResolution(int width, int height)
  46.         {
  47.             gameWidth = width;
  48.             gameHeight = height;
  49.             float scaleWidth = (float)(Game.GraphicsDevice.Viewport.Width / (float)width);
  50.             float scaleHeight = (float)(Game.GraphicsDevice.Viewport.Height / (float)height);
  51.             scale = Math.Min(scaleWidth, scaleHeight);
  52.         }
  53.  
  54.         private void Window_ClientSizeChanged(object sender, EventArgs e)
  55.         {
  56.             // force a recalculation of the scaling
  57.             isResizing = true;
  58.         }
  59.  
  60.         public void SetPosition(Vector2 position)
  61.         {
  62.             if (position != this.position)
  63.             {
  64.                 isDirty = true;
  65.                 this.position = position;
  66.             }
  67.         }
  68.  
  69.         public override void Update(GameTime gameTime)
  70.         {
  71.             if (isResizing)
  72.             {
  73.                 isResizing = false;
  74.  
  75.                 graphics.PreferredBackBufferWidth = Game.Window.ClientBounds.Width;
  76.                 graphics.PreferredBackBufferHeight = Game.Window.ClientBounds.Height;
  77.                 graphics.ApplyChanges();
  78.  
  79.                 SetVirtualResolution(gameWidth, gameHeight);
  80.             }
  81.  
  82.             if (isDirty)
  83.             {
  84.                 isDirty = false;
  85.  
  86.                 transform = Matrix.CreateTranslation(new Vector3(-position.X, -position.Y, 0)) *
  87.                     Matrix.CreateRotationZ(0) *
  88.                     Matrix.CreateScale(new Vector3(scale, scale, 1)) *
  89.                     Matrix.CreateTranslation(new Vector3(Game.GraphicsDevice.Viewport.Width * 0.5f, Game.GraphicsDevice.Viewport.Height * 0.5f, 0));
  90.             }
  91.  
  92.             base.Update(gameTime);
  93.         }
  94.  
  95.         public Matrix GetTransform()
  96.         {
  97.             return transform;
  98.         }
  99.  
  100.         public Vector2 WorldToScreen(Vector2 position)
  101.         {
  102.             return Vector2.Transform(position, GetTransform());
  103.         }
  104.  
  105.         public Vector2 ScreenToWorld(Vector2 position)
  106.         {
  107.             return Vector2.Transform(position, Matrix.Invert(GetTransform()));
  108.         }
  109.     }
Advertisement
Add Comment
Please, Sign In to add comment