Advertisement
Guest User

DisplayManager

a guest
Mar 25th, 2021
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.86 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6.  
  7. namespace Runic
  8. {
  9.     public class DisplayManager
  10.     {
  11.         private Viewport                m_Viewport;
  12.         private GraphicsDeviceManager   m_GraphicsManager;
  13.         private RenderTarget2D          m_VirtualTarget;
  14.         private int                     m_ScreenWidth;
  15.         private int                     m_ScreenHeight;
  16.         private int                     m_ReferenceWidth;
  17.         private int                     m_ReferenceHeight;
  18.         private int                     m_VirtualWidth;
  19.         private int                     m_VirtualHeight;
  20.         private bool                    m_IsFullscreen;
  21.         private bool                    m_UpscaleTarget;
  22.         private bool                    m_Dirty = false;
  23.  
  24.         public DisplayManager(GraphicsDeviceManager manager, int width, int height, int refWidth, int refHeight, bool Upscale, bool fullScreen)
  25.         {
  26.             m_GraphicsManager   = manager;
  27.             m_ScreenWidth       = width;
  28.             m_ScreenHeight      = height;
  29.             m_ReferenceWidth    = refWidth;
  30.             m_ReferenceHeight   = refHeight;
  31.             m_IsFullscreen      = fullScreen;
  32.             m_UpscaleTarget     = Upscale;
  33.             ApplySettings();
  34.         }
  35.  
  36.         public void SetResolution(int Width, int Height, bool fullscreen)
  37.         {
  38.             m_ScreenWidth = Width;
  39.             m_ScreenHeight = Height;
  40.             m_IsFullscreen = fullscreen;
  41.             ApplySettings();
  42.             m_Dirty = true;
  43.         }
  44.  
  45.         public void SetReferenceResolution(int Width, int Height)
  46.         {
  47.             m_ReferenceWidth    = Width;
  48.             m_ReferenceHeight   = Height;
  49.             m_Dirty = true;
  50.         }
  51.  
  52.         public void UpscaleTarget(bool value)
  53.         {
  54.             m_UpscaleTarget = value;
  55.             m_Dirty = true;
  56.         }
  57.  
  58.         public bool IsUpscaledReference()
  59.         {
  60.             return m_UpscaleTarget;
  61.         }
  62.  
  63.         public int Width
  64.         {
  65.             get { return m_ScreenWidth; }
  66.         }
  67.  
  68.         public int Height
  69.         {
  70.             get { return m_ScreenHeight; }
  71.         }
  72.  
  73.         public int ReferenceWidth
  74.         {
  75.             get { return m_ReferenceWidth; }
  76.         }
  77.  
  78.         public int ReferenceHeight
  79.         {
  80.             get { return m_ReferenceHeight; }
  81.         }
  82.  
  83.         public int VirtualWidth
  84.         {
  85.             get { return m_VirtualWidth; }
  86.         }
  87.  
  88.         public int VirtualHeight
  89.         {
  90.             get { return m_VirtualHeight; }
  91.         }
  92.  
  93.         public RenderTarget2D GetVirtualTarget()
  94.         {
  95.             // Recreate the virtual target if its different too our current size.
  96.             if (m_VirtualTarget == null || m_VirtualTarget.Width != m_VirtualWidth || m_VirtualTarget.Height != m_VirtualHeight)
  97.             {
  98.                 m_VirtualTarget = new RenderTarget2D(m_GraphicsManager.GraphicsDevice, m_VirtualWidth, m_VirtualHeight);
  99.             }
  100.  
  101.             return m_VirtualTarget;
  102.         }
  103.  
  104.         public Matrix GetMatrix()
  105.         {
  106.             // Only scale
  107.             if (m_UpscaleTarget == false)
  108.             {
  109.                 return Matrix.CreateScale((float)m_VirtualWidth / m_ReferenceWidth,
  110.                                           (float)m_VirtualHeight / m_ReferenceHeight, 1f);
  111.             }
  112.  
  113.             // Its not effecient but works
  114.             return Matrix.Identity;
  115.         }
  116.  
  117.         public bool IsFullscreen()
  118.         {
  119.             return m_IsFullscreen;
  120.         }
  121.  
  122.         private void ApplySettings()
  123.         {
  124.             if(m_IsFullscreen == false)
  125.             {
  126.                 if (m_ScreenWidth <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width &&
  127.                  m_ScreenHeight <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height)
  128.                 {
  129.                     m_GraphicsManager.PreferredBackBufferWidth  = m_ScreenWidth;
  130.                     m_GraphicsManager.PreferredBackBufferHeight = m_ScreenHeight;
  131.                     m_GraphicsManager.IsFullScreen              = m_IsFullscreen;
  132.                     m_GraphicsManager.ApplyChanges();
  133.                 }
  134.             }
  135.             else
  136.             {
  137.                 m_GraphicsManager.PreferredBackBufferWidth  = m_ScreenWidth;
  138.                 m_GraphicsManager.PreferredBackBufferHeight = m_ScreenHeight;
  139.                 m_GraphicsManager.IsFullScreen              = m_IsFullscreen;
  140.                 m_GraphicsManager.ApplyChanges();
  141.             }
  142.         }
  143.  
  144.         public void BeginDraw()
  145.         {
  146.             // Clear the entire back buffer first!
  147.             m_GraphicsManager.GraphicsDevice.Viewport = new Viewport(0, 0, m_ScreenWidth, m_ScreenHeight);
  148.             m_GraphicsManager.GraphicsDevice.Clear(Color.Black);
  149.  
  150.             UpdateData();
  151.             m_GraphicsManager.GraphicsDevice.Viewport = m_Viewport;
  152.  
  153.         }
  154.  
  155.         private void UpdateData()
  156.         {
  157.             if (m_Dirty)
  158.             {
  159.                 //--CREATE VIEWPORT AT ASPECT TARGET ASPECT--
  160.                 float targetAspectRatio = (float)m_ReferenceWidth / m_ReferenceHeight;
  161.  
  162.                 // Try width first with scaled height to target aspect (letter box)
  163.                 int width = m_ScreenWidth;
  164.                 int height = (int)(width / targetAspectRatio + 0.5f);
  165.  
  166.                 // Height too big, use screen height and squish width to target aspect (Pillar Box)
  167.                 if (height > m_ScreenHeight)
  168.                 {
  169.                     height = m_ScreenHeight;
  170.                     width = (int)(height * targetAspectRatio + 0.5f);
  171.                 }
  172.  
  173.                 // Create Viewport at target aaspect ratio, centered in screen.
  174.                 m_Viewport = new Viewport();
  175.                 m_Viewport.X = (int)((m_ScreenWidth * 0.5f) - (width * 0.5f));
  176.                 m_Viewport.Y = (int)((m_ScreenHeight * 0.5f) - (height * 0.5f));
  177.                 m_Viewport.Width = width;
  178.                 m_Viewport.Height = height;
  179.                 m_Viewport.MinDepth = 0;
  180.                 m_Viewport.MaxDepth = 1;
  181.  
  182.                 //--FIND CLOSEST INTEGER TARGET--
  183.                 if(m_UpscaleTarget == false)
  184.                 {
  185.                     float sw = (float)m_ScreenWidth / m_ReferenceWidth;
  186.                     float sh = (float)m_ScreenHeight / m_ReferenceHeight;
  187.                     int integerScaling = 1;
  188.  
  189.                     if(sw > sh)
  190.                     {
  191.                         integerScaling = (int)sw;
  192.                     }
  193.                     else
  194.                     {
  195.                         integerScaling = (int)sh;
  196.                     }
  197.  
  198.                     // Set virtual too be the closest integer scaling too screen size!
  199.                     m_VirtualWidth  = m_ReferenceWidth  * integerScaling;
  200.                     m_VirtualHeight = m_ReferenceHeight * integerScaling;
  201.                 }
  202.                 else
  203.                 {
  204.                     m_VirtualWidth  = m_ReferenceWidth;
  205.                     m_VirtualHeight = m_ReferenceHeight;
  206.                 }
  207.  
  208.                 m_Dirty = false;
  209.             }
  210.         }
  211.  
  212.         public void BlitTarget(RenderTarget2D virtualTarget)
  213.         {
  214.             SamplerState samplerState = SamplerState.PointClamp;
  215.             if (m_UpscaleTarget == false)
  216.             {
  217.                 samplerState = SamplerState.LinearClamp;
  218.             }
  219.  
  220.             SpriteBatch spriteBatch = Engine.Instance.SpriteBatch;
  221.             spriteBatch.Begin(SpriteSortMode.Immediate, null, samplerState);
  222.             spriteBatch.Draw(virtualTarget, new Rectangle(m_Viewport.X, m_Viewport.Y, m_Viewport.Width, m_Viewport.Height), new Rectangle(0,0, m_VirtualWidth, m_VirtualHeight), Color.White);
  223.             spriteBatch.End();
  224.         }
  225.  
  226.     }
  227. }
  228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement