Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Runic
- {
- public class DisplayManager
- {
- private Viewport m_Viewport;
- private GraphicsDeviceManager m_GraphicsManager;
- private RenderTarget2D m_VirtualTarget;
- private int m_ScreenWidth;
- private int m_ScreenHeight;
- private int m_ReferenceWidth;
- private int m_ReferenceHeight;
- private int m_VirtualWidth;
- private int m_VirtualHeight;
- private bool m_IsFullscreen;
- private bool m_UpscaleTarget;
- private bool m_Dirty = false;
- public DisplayManager(GraphicsDeviceManager manager, int width, int height, int refWidth, int refHeight, bool Upscale, bool fullScreen)
- {
- m_GraphicsManager = manager;
- m_ScreenWidth = width;
- m_ScreenHeight = height;
- m_ReferenceWidth = refWidth;
- m_ReferenceHeight = refHeight;
- m_IsFullscreen = fullScreen;
- m_UpscaleTarget = Upscale;
- ApplySettings();
- }
- public void SetResolution(int Width, int Height, bool fullscreen)
- {
- m_ScreenWidth = Width;
- m_ScreenHeight = Height;
- m_IsFullscreen = fullscreen;
- ApplySettings();
- m_Dirty = true;
- }
- public void SetReferenceResolution(int Width, int Height)
- {
- m_ReferenceWidth = Width;
- m_ReferenceHeight = Height;
- m_Dirty = true;
- }
- public void UpscaleTarget(bool value)
- {
- m_UpscaleTarget = value;
- m_Dirty = true;
- }
- public bool IsUpscaledReference()
- {
- return m_UpscaleTarget;
- }
- public int Width
- {
- get { return m_ScreenWidth; }
- }
- public int Height
- {
- get { return m_ScreenHeight; }
- }
- public int ReferenceWidth
- {
- get { return m_ReferenceWidth; }
- }
- public int ReferenceHeight
- {
- get { return m_ReferenceHeight; }
- }
- public int VirtualWidth
- {
- get { return m_VirtualWidth; }
- }
- public int VirtualHeight
- {
- get { return m_VirtualHeight; }
- }
- public RenderTarget2D GetVirtualTarget()
- {
- // Recreate the virtual target if its different too our current size.
- if (m_VirtualTarget == null || m_VirtualTarget.Width != m_VirtualWidth || m_VirtualTarget.Height != m_VirtualHeight)
- {
- m_VirtualTarget = new RenderTarget2D(m_GraphicsManager.GraphicsDevice, m_VirtualWidth, m_VirtualHeight);
- }
- return m_VirtualTarget;
- }
- public Matrix GetMatrix()
- {
- // Only scale
- if (m_UpscaleTarget == false)
- {
- return Matrix.CreateScale((float)m_VirtualWidth / m_ReferenceWidth,
- (float)m_VirtualHeight / m_ReferenceHeight, 1f);
- }
- // Its not effecient but works
- return Matrix.Identity;
- }
- public bool IsFullscreen()
- {
- return m_IsFullscreen;
- }
- private void ApplySettings()
- {
- if(m_IsFullscreen == false)
- {
- if (m_ScreenWidth <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width &&
- m_ScreenHeight <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height)
- {
- m_GraphicsManager.PreferredBackBufferWidth = m_ScreenWidth;
- m_GraphicsManager.PreferredBackBufferHeight = m_ScreenHeight;
- m_GraphicsManager.IsFullScreen = m_IsFullscreen;
- m_GraphicsManager.ApplyChanges();
- }
- }
- else
- {
- m_GraphicsManager.PreferredBackBufferWidth = m_ScreenWidth;
- m_GraphicsManager.PreferredBackBufferHeight = m_ScreenHeight;
- m_GraphicsManager.IsFullScreen = m_IsFullscreen;
- m_GraphicsManager.ApplyChanges();
- }
- }
- public void BeginDraw()
- {
- // Clear the entire back buffer first!
- m_GraphicsManager.GraphicsDevice.Viewport = new Viewport(0, 0, m_ScreenWidth, m_ScreenHeight);
- m_GraphicsManager.GraphicsDevice.Clear(Color.Black);
- UpdateData();
- m_GraphicsManager.GraphicsDevice.Viewport = m_Viewport;
- }
- private void UpdateData()
- {
- if (m_Dirty)
- {
- //--CREATE VIEWPORT AT ASPECT TARGET ASPECT--
- float targetAspectRatio = (float)m_ReferenceWidth / m_ReferenceHeight;
- // Try width first with scaled height to target aspect (letter box)
- int width = m_ScreenWidth;
- int height = (int)(width / targetAspectRatio + 0.5f);
- // Height too big, use screen height and squish width to target aspect (Pillar Box)
- if (height > m_ScreenHeight)
- {
- height = m_ScreenHeight;
- width = (int)(height * targetAspectRatio + 0.5f);
- }
- // Create Viewport at target aaspect ratio, centered in screen.
- m_Viewport = new Viewport();
- m_Viewport.X = (int)((m_ScreenWidth * 0.5f) - (width * 0.5f));
- m_Viewport.Y = (int)((m_ScreenHeight * 0.5f) - (height * 0.5f));
- m_Viewport.Width = width;
- m_Viewport.Height = height;
- m_Viewport.MinDepth = 0;
- m_Viewport.MaxDepth = 1;
- //--FIND CLOSEST INTEGER TARGET--
- if(m_UpscaleTarget == false)
- {
- float sw = (float)m_ScreenWidth / m_ReferenceWidth;
- float sh = (float)m_ScreenHeight / m_ReferenceHeight;
- int integerScaling = 1;
- if(sw > sh)
- {
- integerScaling = (int)sw;
- }
- else
- {
- integerScaling = (int)sh;
- }
- // Set virtual too be the closest integer scaling too screen size!
- m_VirtualWidth = m_ReferenceWidth * integerScaling;
- m_VirtualHeight = m_ReferenceHeight * integerScaling;
- }
- else
- {
- m_VirtualWidth = m_ReferenceWidth;
- m_VirtualHeight = m_ReferenceHeight;
- }
- m_Dirty = false;
- }
- }
- public void BlitTarget(RenderTarget2D virtualTarget)
- {
- SamplerState samplerState = SamplerState.PointClamp;
- if (m_UpscaleTarget == false)
- {
- samplerState = SamplerState.LinearClamp;
- }
- SpriteBatch spriteBatch = Engine.Instance.SpriteBatch;
- spriteBatch.Begin(SpriteSortMode.Immediate, null, samplerState);
- 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);
- spriteBatch.End();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement