Skip to content

Instantly share code, notes, and snippets.

@prime31
Last active December 16, 2016 04:50
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 prime31/79e0b404459dd26f7873a9b855de5bcc to your computer and use it in GitHub Desktop.
Save prime31/79e0b404459dd26f7873a9b855de5bcc to your computer and use it in GitHub Desktop.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Nez;
namespace TheWall
{
public class Game2 : Game
{
GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
Texture2D _moonTexture;
RenderTarget2D _renderTarget;
RenderTarget2D _renderTarget2;
public Game2()
{
_graphics = new GraphicsDeviceManager( this );
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch( GraphicsDevice );
_moonTexture = Content.Load<Texture2D>( "moon.png" );
}
protected override void Update( GameTime gameTime )
{
Input.update();
if( Input.isKeyDown( Keys.A ) )
{
_renderTarget = new RenderTarget2D( _graphics.GraphicsDevice, 128, 128, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents );
renderMoon( _renderTarget, Color.Red );
}
if( Input.isKeyDown( Keys.B ) )
{
_renderTarget2 = new RenderTarget2D( _graphics.GraphicsDevice, 128, 128, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents );
renderMoon( _renderTarget2, Color.Blue );
}
base.Update( gameTime );
}
void renderMoon( RenderTarget2D target, Color color )
{
_graphics.GraphicsDevice.SetRenderTarget( target );
_spriteBatch.Begin( SpriteSortMode.Immediate, BlendState.AlphaBlend );
_spriteBatch.Draw( _moonTexture, Vector2.Zero, color );
_spriteBatch.End();
_graphics.GraphicsDevice.SetRenderTarget( null );
}
protected override void Draw( GameTime gameTime )
{
GraphicsDevice.Clear( Color.CornflowerBlue );
_spriteBatch.Begin( SpriteSortMode.Immediate, BlendState.AlphaBlend );
if( _renderTarget != null )
_spriteBatch.Draw( _renderTarget, new Vector2( 150, 150 ), Color.White );
if( _renderTarget2 != null )
_spriteBatch.Draw( _renderTarget2, new Vector2( 0, 0 ), 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