Skip to content

Instantly share code, notes, and snippets.

@cilliemalan
Last active July 1, 2020 17:35
Show Gist options
  • Save cilliemalan/9e4bb05bf4bfbbb4ad011d36096e81e3 to your computer and use it in GitHub Desktop.
Save cilliemalan/9e4bb05bf4bfbbb4ad011d36096e81e3 to your computer and use it in GitHub Desktop.
A workaround for multisampling in MonoGame under UWP by rendering to texture first.
/*
One cannot use multisampling directly in a UWP application because
of reasons stipulated here:
https://docs.microsoft.com/tr-tr/windows/uwp/gaming/multisampling--multi-sample-anti-aliasing--in-windows-store-apps
The workaround is to create a render target with multisampling, render to that
and then render the render target as a sprite
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Wut
{
public class MainView : Game
{
private readonly GraphicsDeviceManager graphics;
private SpriteFont defaultFont;
private SpriteBatch spriteBatch;
private RenderTarget2D renderTarget;
public MainView()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
defaultFont = Content.Load<SpriteFont>("Default");
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
UpdateRenderTarget();
DrawInternal(gameTime);
ApplyRenderTarget();
base.Draw(gameTime);
}
private void DrawInternal(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.DrawString(defaultFont, "Hello World", new Vector2(10, 10), Color.White);
spriteBatch.DrawCircle(new Vector2(graphics.PreferredBackBufferWidth / 2.0f, graphics.PreferredBackBufferHeight / 2.0f), 200, 32, Color.Green);
spriteBatch.End();
}
private void UpdateRenderTarget()
{
bool change = renderTarget == null ||
renderTarget.Width != graphics.PreferredBackBufferWidth ||
renderTarget.Height != graphics.PreferredBackBufferHeight;
if (change)
{
renderTarget?.Dispose();
renderTarget = new RenderTarget2D(
GraphicsDevice,
graphics.PreferredBackBufferWidth,
graphics.PreferredBackBufferHeight,
false,
graphics.PreferredBackBufferFormat,
graphics.PreferredDepthStencilFormat,
8,
RenderTargetUsage.PlatformContents);
}
GraphicsDevice.SetRenderTarget(renderTarget);
}
private void ApplyRenderTarget()
{
GraphicsDevice.SetRenderTarget(null);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque,
SamplerState.LinearClamp, DepthStencilState.Default,
RasterizerState.CullNone);
spriteBatch.Draw(renderTarget, new Rectangle(0, 0, renderTarget.Width, renderTarget.Height), Color.White);
spriteBatch.End();
}
}
}
@cilliemalan
Copy link
Author

The DrawCircle thing is from here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment