Skip to content

Instantly share code, notes, and snippets.

@Noxalus
Created January 15, 2017 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noxalus/5cbe57c0a17f2aab7c65a5c4d338e8fa to your computer and use it in GitHub Desktop.
Save Noxalus/5cbe57c0a17f2aab7c65a5c4d338e8fa to your computer and use it in GitHub Desktop.
MonoGame.Extended: BoxingViewportAdapter issue with RenderTarget
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.BitmapFonts;
using MonoGame.Extended.ViewportAdapters;
namespace Demo.ViewportAdapters
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private Texture2D _backgroundTexture;
private BitmapFont _bitmapFont;
private ViewportAdapter _viewportAdapter;
private SpriteBatch _spriteBatch;
private Point _virtualResolution;
private RenderTarget2D _renderTarget;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
Window.AllowUserResizing = true;
_virtualResolution = new Point(720, 1280);
_graphics.PreferredBackBufferWidth = _virtualResolution.X;
_graphics.PreferredBackBufferHeight = _virtualResolution.Y;
}
protected override void Initialize()
{
base.Initialize();
_viewportAdapter = new BoxingViewportAdapter(Window, GraphicsDevice, _virtualResolution.X, _virtualResolution.Y);
var pp = GraphicsDevice.PresentationParameters;
_renderTarget = new RenderTarget2D(
GraphicsDevice, 32, 32, false,
pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.DiscardContents, true
);
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_backgroundTexture = Content.Load<Texture2D>("vignette");
_bitmapFont = Content.Load<BitmapFont>("montserrat-32");
}
protected override void Draw(GameTime gameTime)
{
_graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
// Clear the render target
_graphics.GraphicsDevice.SetRenderTarget(_renderTarget);
_graphics.GraphicsDevice.Clear(Color.Red);
_graphics.GraphicsDevice.SetRenderTarget(null);
var destinationRectangle = new Rectangle(0, 0, _virtualResolution.X, _virtualResolution.Y);
_spriteBatch.Begin(transformMatrix: _viewportAdapter.GetScaleMatrix());
_spriteBatch.Draw(_backgroundTexture, destinationRectangle, Color.White);
_spriteBatch.DrawString(_bitmapFont, $"Current: {_viewportAdapter.GetType().Name}", new Vector2(49, 40 + _bitmapFont.LineHeight * 4), Color.Black);
_spriteBatch.DrawString(_bitmapFont, @"Try resizing the window", new Vector2(49, 40 + _bitmapFont.LineHeight * 6), Color.Black);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment