Skip to content

Instantly share code, notes, and snippets.

@KyleGobel
Created May 29, 2016 19:05
Show Gist options
  • Save KyleGobel/df51c2a1952b3ed6b44fa11f874f2959 to your computer and use it in GitHub Desktop.
Save KyleGobel/df51c2a1952b3ed6b44fa11f874f2959 to your computer and use it in GitHub Desktop.
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
private bool _testRenderTarget = false;
private Texture2D _guiTexture;
private RenderTarget2D _renderTarget;
public Game1()
{
_graphics = new GraphicsDeviceManager(this)
{
PreferredBackBufferHeight = 1024,
PreferredBackBufferWidth = 768
};
_graphics.ApplyChanges();
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
_renderTarget = new RenderTarget2D(GraphicsDevice, 800, 600);
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_guiTexture = Content.Load<Texture2D>("gui");
}
protected override void UnloadContent() { }
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
_testRenderTarget = Keyboard.GetState().IsKeyDown(Keys.F1);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin(SpriteSortMode.Immediate);
if (_testRenderTarget)
{
GraphicsDevice.SetRenderTarget(_renderTarget);
_spriteBatch.Draw(_guiTexture, new Rectangle(0, 0, 64, 64), new Rectangle(0, 0, 64, 64), Color.White);
GraphicsDevice.SetRenderTarget(null);
_spriteBatch.Draw(_renderTarget, Vector2.Zero, Color.White);
}
else
{
GraphicsDevice.SetRenderTarget(null);
_spriteBatch.Draw(_guiTexture, new Rectangle(0, 0, 64, 64), new Rectangle(0, 0, 64, 64), 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