Skip to content

Instantly share code, notes, and snippets.

@Jjagg
Created November 7, 2017 20:45
Show Gist options
  • Save Jjagg/f6eb94ef687505ca468ff65240c26ebf to your computer and use it in GitHub Desktop.
Save Jjagg/f6eb94ef687505ca468ff65240c26ebf to your computer and use it in GitHub Desktop.
Upscaling with Point sampling in MonoGame
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace WinDesktopPlatformer
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
static int PPU = 1;
static int TILE_SIZE = 16;
static int ROOM_WIDTH = 32;
static int ROOM_HEIGHT = 18;
static Rectangle CANVAS = new Rectangle(0, 0, (TILE_SIZE * ROOM_WIDTH) / PPU, (TILE_SIZE * ROOM_HEIGHT) / PPU);
Texture2D tileSet;
Rectangle tile1;
private const int ScreenWidth = 1920;
private const int ScreenHeight = 1080;
private RenderTarget2D _renderTarget;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferMultiSampling = false;
graphics.SynchronizeWithVerticalRetrace = true;
graphics.PreferredBackBufferWidth = ScreenWidth;
graphics.PreferredBackBufferHeight = ScreenHeight;
graphics.IsFullScreen = true;
Content.RootDirectory = "Content";
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
tileSet = Content.Load<Texture2D>(@"Images/Tiles2");
tile1 = new Rectangle(0, 32, TILE_SIZE, TILE_SIZE);
_renderTarget = new RenderTarget2D(GraphicsDevice, CANVAS.Width, CANVAS.Height);
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(_renderTarget);
GraphicsDevice.Clear(Color.CornflowerBlue);
// render your complete game here at a low resolution to the render target
// we use point sampling here because we enlarge the texture
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
spriteBatch.Draw(tileSet, new Rectangle(16, 16, 64, 64), tile1, Color.White);
spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
// here we upscale it to the full screen, again with point sampling enabled
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
spriteBatch.Draw(_renderTarget, new Rectangle(0, 0, ScreenWidth, ScreenHeight), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment