From fb1c233e59fb50a95a295b4c23142d0b3fe8ca0f Mon Sep 17 00:00:00 2001 From: Dylan Wilson Date: Wed, 15 Jul 2015 09:04:38 +1000 Subject: [PATCH] started on virtual screen / viewport --- MonoGame.Extended/Game1/Game1.cs | 16 +++++-- .../MonoGame.Extended.csproj | 1 + .../MonoGame.Extended/VirtualScreen.cs | 46 +++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 MonoGame.Extended/MonoGame.Extended/VirtualScreen.cs diff --git a/MonoGame.Extended/Game1/Game1.cs b/MonoGame.Extended/Game1/Game1.cs index b56149c0..1cb15224 100644 --- a/MonoGame.Extended/Game1/Game1.cs +++ b/MonoGame.Extended/Game1/Game1.cs @@ -1,4 +1,5 @@ -using Microsoft.Xna.Framework; +using System; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using MonoGame.Extended; @@ -13,16 +14,25 @@ namespace Game1 private Texture2D[] _backgroundTexture; private Texture2D _backgroundTextureClouds; private Texture2D _backgroundTextureSky; + private VirtualScreen _virtualScreen; public Game1() { _graphicsDeviceManager = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; + Window.AllowUserResizing = true; + Window.ClientSizeChanged += WindowOnClientSizeChanged; + } + + private void WindowOnClientSizeChanged(object sender, EventArgs eventArgs) + { + _virtualScreen.OnClientSizeChanged(); } protected override void Initialize() { + _virtualScreen = new VirtualScreen(GraphicsDevice, 800, 480); _camera = new Camera2D(GraphicsDevice.Viewport); base.Initialize(); } @@ -108,7 +118,7 @@ namespace Game1 { GraphicsDevice.Clear(Color.CornflowerBlue); - _spriteBatch.Begin(transformMatrix: _camera.GetViewMatrix(new Vector2(0.0f, 1.0f))); + _spriteBatch.Begin(transformMatrix: _camera.GetViewMatrix(new Vector2(0.0f, 1.0f)) * _virtualScreen.GetScaleMatrix()); _spriteBatch.Draw(_backgroundTextureSky, Vector2.Zero); _spriteBatch.Draw(_backgroundTextureClouds, Vector2.Zero); _spriteBatch.End(); @@ -116,7 +126,7 @@ namespace Game1 for (var i = 0; i < 4; i++) { var parallaxFactor = new Vector2(0.5f + 0.25f * i, 1.0f); - var viewMatrix = _camera.GetViewMatrix(parallaxFactor); + var viewMatrix = _camera.GetViewMatrix(parallaxFactor) * _virtualScreen.GetScaleMatrix(); _spriteBatch.Begin(transformMatrix: viewMatrix); _spriteBatch.Draw(_backgroundTexture[i], Vector2.Zero); _spriteBatch.End(); diff --git a/MonoGame.Extended/MonoGame.Extended/MonoGame.Extended.csproj b/MonoGame.Extended/MonoGame.Extended/MonoGame.Extended.csproj index aedbdf04..bbfc707f 100644 --- a/MonoGame.Extended/MonoGame.Extended/MonoGame.Extended.csproj +++ b/MonoGame.Extended/MonoGame.Extended/MonoGame.Extended.csproj @@ -38,6 +38,7 @@ + diff --git a/MonoGame.Extended/MonoGame.Extended/VirtualScreen.cs b/MonoGame.Extended/MonoGame.Extended/VirtualScreen.cs new file mode 100644 index 00000000..59465f7f --- /dev/null +++ b/MonoGame.Extended/MonoGame.Extended/VirtualScreen.cs @@ -0,0 +1,46 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace MonoGame.Extended +{ + public class VirtualScreen + { + private readonly GraphicsDevice _graphicsDevice; + + public VirtualScreen(GraphicsDevice graphicsDevice, int virtualWidth, int virtualHeight) + { + _graphicsDevice = graphicsDevice; + + VirtualWidth = virtualWidth; + VirtualHeight = virtualHeight; + } + + public int VirtualWidth { get; private set; } + public int VirtualHeight { get; private set; } + + public int ActualWidth + { + get { return _graphicsDevice.Viewport.Width; } + } + + public int ActualHeight + { + get { return _graphicsDevice.Viewport.Height; } + } + + public Matrix GetScaleMatrix() + { + var scaleX = (float) ActualWidth / VirtualWidth; + var scaleY = (float) ActualHeight / VirtualHeight; + return Matrix.CreateScale(scaleX, scaleY, 1.0f); + } + + public void OnClientSizeChanged() + { + var aspectRatio = (float) VirtualWidth / VirtualHeight; + var newWidth = _graphicsDevice.Viewport.Width; + var newHeight = _graphicsDevice.Viewport.Height; + _graphicsDevice.Viewport = new Viewport(100, 100, 300, 300); + } + } +} \ No newline at end of file