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