mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-15 15:09:29 +00:00
viewport adapters complete
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using MonoGame.Extended;
|
||||
@@ -14,26 +13,22 @@ namespace Game1
|
||||
private Texture2D[] _backgroundTexture;
|
||||
private Texture2D _backgroundTextureClouds;
|
||||
private Texture2D _backgroundTextureSky;
|
||||
private VirtualViewportAdapter _virtualViewportAdapter;
|
||||
|
||||
public Game1()
|
||||
{
|
||||
_graphicsDeviceManager = new GraphicsDeviceManager(this);
|
||||
Content.RootDirectory = "Content";
|
||||
IsMouseVisible = true;
|
||||
Window.AllowUserResizing = true;
|
||||
Window.ClientSizeChanged += WindowOnClientSizeChanged;
|
||||
}
|
||||
|
||||
private void WindowOnClientSizeChanged(object sender, EventArgs eventArgs)
|
||||
{
|
||||
_virtualViewportAdapter.OnClientSizeChanged();
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
_virtualViewportAdapter = new VirtualViewportAdapter(GraphicsDevice, 800, 480);
|
||||
_camera = new Camera2D(GraphicsDevice.Viewport);
|
||||
var viewportAdapter = new ViewportAdapter(GraphicsDevice);
|
||||
_camera = new Camera2D(viewportAdapter);
|
||||
|
||||
Window.AllowUserResizing = true;
|
||||
Window.ClientSizeChanged += (s,e) => viewportAdapter.OnClientSizeChanged();
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
@@ -112,21 +107,21 @@ namespace Game1
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
private MouseState _previousMouseState;
|
||||
//private MouseState _previousMouseState;
|
||||
|
||||
protected override void Draw(GameTime gameTime)
|
||||
{
|
||||
GraphicsDevice.Clear(Color.Black);
|
||||
|
||||
_spriteBatch.Begin(transformMatrix: _camera.GetViewMatrix(new Vector2(0.0f, 1.0f)) * _virtualViewportAdapter.GetScaleMatrix());
|
||||
_spriteBatch.Begin(transformMatrix: _camera.GetViewMatrix(new Vector2(0.25f, 1.0f)));
|
||||
_spriteBatch.Draw(_backgroundTextureSky, Vector2.Zero);
|
||||
_spriteBatch.Draw(_backgroundTextureClouds, Vector2.Zero);
|
||||
_spriteBatch.End();
|
||||
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
var parallaxFactor = new Vector2(0.5f + 0.25f * i, 1.0f);
|
||||
var viewMatrix = _camera.GetViewMatrix(parallaxFactor) * _virtualViewportAdapter.GetScaleMatrix();
|
||||
var parallaxFactor = new Vector2(0.5f + 0.5f * i, 1.0f);
|
||||
var viewMatrix = _camera.GetViewMatrix(parallaxFactor);
|
||||
_spriteBatch.Begin(transformMatrix: viewMatrix);
|
||||
_spriteBatch.Draw(_backgroundTexture[i], Vector2.Zero);
|
||||
_spriteBatch.End();
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace MonoGame.Extended
|
||||
{
|
||||
public enum BoxingMode
|
||||
{
|
||||
Letterbox, Pillarbox
|
||||
}
|
||||
|
||||
public class BoxingViewportAdapter : ScalingViewportAdapter
|
||||
{
|
||||
public BoxingViewportAdapter(GraphicsDevice graphicsDevice, int virtualWidth, int virtualHeight)
|
||||
: base(graphicsDevice, virtualWidth, virtualHeight)
|
||||
{
|
||||
}
|
||||
|
||||
public BoxingMode BoxingMode { get; private set; }
|
||||
|
||||
public override void OnClientSizeChanged()
|
||||
{
|
||||
var viewport = GraphicsDevice.Viewport;
|
||||
var aspectRatio = (float) VirtualWidth / VirtualHeight;
|
||||
var width = viewport.Width;
|
||||
var height = (int)(width / aspectRatio + 0.5f);
|
||||
|
||||
if (height > viewport.Height)
|
||||
{
|
||||
BoxingMode = BoxingMode.Pillarbox;
|
||||
height = viewport.Height;
|
||||
width = (int) (height * aspectRatio + 0.5f);
|
||||
}
|
||||
else
|
||||
{
|
||||
BoxingMode = BoxingMode.Letterbox;
|
||||
}
|
||||
|
||||
var x = (viewport.Width / 2) - (width / 2);
|
||||
var y = (viewport.Height / 2) - (height / 2);
|
||||
GraphicsDevice.Viewport = new Viewport(x, y, width, height);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,20 @@ namespace MonoGame.Extended
|
||||
{
|
||||
public class Camera2D
|
||||
{
|
||||
private readonly Viewport _viewport;
|
||||
private readonly ViewportAdapter _viewportAdapter;
|
||||
|
||||
public Camera2D(Viewport viewport)
|
||||
public Camera2D(GraphicsDevice graphicsDevice)
|
||||
: this(new ViewportAdapter(graphicsDevice))
|
||||
{
|
||||
_viewport = viewport;
|
||||
}
|
||||
|
||||
public Camera2D(ViewportAdapter viewportAdapter)
|
||||
{
|
||||
_viewportAdapter = viewportAdapter;
|
||||
|
||||
Rotation = 0;
|
||||
Zoom = 1;
|
||||
Origin = new Vector2(viewport.Width / 2f, viewport.Height / 2f);
|
||||
Origin = new Vector2(viewportAdapter.VirtualWidth / 2f, viewportAdapter.VirtualHeight / 2f);
|
||||
Position = Vector2.Zero;
|
||||
}
|
||||
|
||||
@@ -29,7 +34,7 @@ namespace MonoGame.Extended
|
||||
|
||||
public void LookAt(Vector2 position)
|
||||
{
|
||||
Position = position - new Vector2(_viewport.Width / 2f, _viewport.Height / 2f);
|
||||
Position = position - new Vector2(_viewportAdapter.VirtualWidth / 2f, _viewportAdapter.VirtualHeight / 2f);
|
||||
}
|
||||
|
||||
public Vector2 WorldToScreen(Vector2 worldPosition)
|
||||
@@ -49,7 +54,8 @@ namespace MonoGame.Extended
|
||||
Matrix.CreateTranslation(new Vector3(-Origin, 0.0f)) *
|
||||
Matrix.CreateRotationZ(Rotation) *
|
||||
Matrix.CreateScale(Zoom, Zoom, 1) *
|
||||
Matrix.CreateTranslation(new Vector3(Origin, 0.0f));
|
||||
Matrix.CreateTranslation(new Vector3(Origin, 0.0f)) *
|
||||
_viewportAdapter.GetScaleMatrix();
|
||||
}
|
||||
|
||||
public Matrix GetViewMatrix()
|
||||
|
||||
@@ -35,10 +35,12 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Camera2D.cs" />
|
||||
<Compile Include="ScalingViewportAdapter.cs" />
|
||||
<Compile Include="SpriteBatchExtensions.cs" />
|
||||
<Compile Include="TextureRegion2D.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="VirtualViewportAdapter.cs" />
|
||||
<Compile Include="BoxingViewportAdapter.cs" />
|
||||
<Compile Include="ViewportAdapter.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MonoGame.Framework, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace MonoGame.Extended
|
||||
{
|
||||
public class ScalingViewportAdapter : ViewportAdapter
|
||||
{
|
||||
public ScalingViewportAdapter(GraphicsDevice graphicsDevice, int virtualWidth, int virtualHeight)
|
||||
: base(graphicsDevice)
|
||||
{
|
||||
_virtualWidth = virtualWidth;
|
||||
_virtualHeight = virtualHeight;
|
||||
}
|
||||
|
||||
private readonly int _virtualWidth;
|
||||
public override int VirtualWidth
|
||||
{
|
||||
get { return _virtualWidth; }
|
||||
}
|
||||
|
||||
private readonly int _virtualHeight;
|
||||
public override int VirtualHeight
|
||||
{
|
||||
get { return _virtualHeight; }
|
||||
}
|
||||
|
||||
public override Matrix GetScaleMatrix()
|
||||
{
|
||||
var scaleX = (float)ActualWidth / VirtualWidth;
|
||||
var scaleY = (float)ActualHeight / VirtualHeight;
|
||||
return Matrix.CreateScale(scaleX, scaleY, 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace MonoGame.Extended
|
||||
{
|
||||
public class ViewportAdapter
|
||||
{
|
||||
public ViewportAdapter(GraphicsDevice graphicsDevice)
|
||||
{
|
||||
GraphicsDevice = graphicsDevice;
|
||||
}
|
||||
|
||||
protected GraphicsDevice GraphicsDevice { get; private set; }
|
||||
|
||||
public virtual int VirtualWidth
|
||||
{
|
||||
get { return ActualWidth; }
|
||||
}
|
||||
|
||||
public virtual int VirtualHeight
|
||||
{
|
||||
get { return ActualHeight; }
|
||||
}
|
||||
|
||||
public int ActualWidth
|
||||
{
|
||||
get { return GraphicsDevice.Viewport.Width; }
|
||||
}
|
||||
|
||||
public int ActualHeight
|
||||
{
|
||||
get { return GraphicsDevice.Viewport.Height; }
|
||||
}
|
||||
|
||||
public virtual void OnClientSizeChanged()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual Matrix GetScaleMatrix()
|
||||
{
|
||||
return Matrix.Identity;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace MonoGame.Extended
|
||||
{
|
||||
public class VirtualViewportAdapter
|
||||
{
|
||||
private readonly GraphicsDevice _graphicsDevice;
|
||||
|
||||
public VirtualViewportAdapter(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 viewport = _graphicsDevice.Viewport;
|
||||
var aspectRatio = (float) VirtualWidth / VirtualHeight;
|
||||
var width = viewport.Width;
|
||||
var height = (int)(width / aspectRatio + 0.5f);
|
||||
|
||||
if (height > viewport.Height)
|
||||
{
|
||||
height = viewport.Height;
|
||||
width = (int)(height * aspectRatio + 0.5f);
|
||||
}
|
||||
|
||||
var x = (viewport.Width / 2) - (width / 2);
|
||||
var y = (viewport.Height / 2) - (height / 2);
|
||||
_graphicsDevice.Viewport = new Viewport(x, y, width, height);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user