mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-15 15:09:29 +00:00
started on virtual screen / viewport
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
<Compile Include="SpriteBatchExtensions.cs" />
|
||||
<Compile Include="TextureRegion2D.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="VirtualScreen.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MonoGame.Framework, Version=3.1.2.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user