Created
June 10, 2019 12:13
-
-
Save BlizzCrafter/1330fa4c967eb3fce9414c3e76649e05 to your computer and use it in GitHub Desktop.
Wraps Texture2Ds around the screen by using a set of RenderTarget2Ds.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Game1 : Game | |
{ | |
private readonly GraphicsDeviceManager graphics; | |
private SpriteBatch spriteBatch; | |
private const int _ScreenWidth = 800; | |
private const int _ScreenHeight = 600; | |
private RenderTarget2D _WrapTarget; | |
private Texture2D _Circle, _Square; | |
private Vector2 _CirclePosition, _SquarePosition; | |
public Game1() | |
{ | |
graphics = new GraphicsDeviceManager(this); | |
Content.RootDirectory = "Content"; | |
} | |
protected override void Initialize() | |
{ | |
graphics.PreferredBackBufferWidth = _ScreenWidth; | |
graphics.PreferredBackBufferHeight = _ScreenHeight; | |
graphics.ApplyChanges(); | |
base.Initialize(); | |
_CirclePosition = new Vector2(_ScreenWidth / 2 - _Circle.Width / 2, _ScreenHeight / 2 - _Circle.Height / 2); | |
_SquarePosition = new Vector2(_ScreenWidth / 2 - _Square.Width / 2, _ScreenHeight / 2 + _Square.Height / 2); | |
} | |
protected override void LoadContent() | |
{ | |
spriteBatch = new SpriteBatch(GraphicsDevice); | |
_WrapTarget = new RenderTarget2D(GraphicsDevice, _ScreenWidth * 3, _ScreenHeight * 3); | |
_Circle = Content.Load<Texture2D>(@"circle"); | |
_Square = Content.Load<Texture2D>(@"square"); | |
} | |
protected override void Update(GameTime gameTime) | |
{ | |
if (Keyboard.GetState().IsKeyDown(Keys.D)) _CirclePosition.X += 6; | |
if (Keyboard.GetState().IsKeyDown(Keys.A)) _CirclePosition.X -= 6; | |
if (Keyboard.GetState().IsKeyDown(Keys.W)) _CirclePosition.Y -= 6; | |
if (Keyboard.GetState().IsKeyDown(Keys.S)) _CirclePosition.Y += 6; | |
if (_CirclePosition.X <= - _ScreenWidth + _Circle.Width / 2) _CirclePosition.X = _Circle.Width / 2; | |
if (_CirclePosition.Y <= - _ScreenHeight + _Circle.Height / 2) _CirclePosition.Y = _Circle.Height / 2; | |
if (_CirclePosition.X >= _ScreenWidth * 2 - _Circle.Width / 2) _CirclePosition.X = _ScreenWidth - _Circle.Width / 2; | |
if (_CirclePosition.Y >= _ScreenHeight * 2 - _Circle.Height / 2) _CirclePosition.Y = _ScreenHeight - _Circle.Height / 2; | |
base.Update(gameTime); | |
} | |
protected override void Draw(GameTime gameTime) | |
{ | |
GraphicsDevice.SetRenderTarget(_WrapTarget); | |
GraphicsDevice.Clear(Color.Transparent); | |
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, Matrix.CreateTranslation(_ScreenWidth, _ScreenHeight, 0.00f)); | |
spriteBatch.Draw(_Circle, _CirclePosition, null, Color.White, 0.00f, new Vector2(_Circle.Width / 2.00f, _Circle.Height / 2.00f), Vector2.One, SpriteEffects.None, 0); | |
spriteBatch.Draw(_Square, _SquarePosition, null, Color.White, 0.00f, new Vector2(_Square.Width / 2.00f, _Square.Height / 2.00f), Vector2.One, SpriteEffects.None, 0); | |
spriteBatch.End(); | |
GraphicsDevice.SetRenderTarget(null); | |
GraphicsDevice.Clear(Color.LightYellow); | |
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend); | |
spriteBatch.Draw(_WrapTarget, Vector2.Zero, new Rectangle(0, 0, _ScreenWidth, _ScreenHeight), Color.White); | |
spriteBatch.Draw(_WrapTarget, Vector2.Zero, new Rectangle(_ScreenWidth, 0, _ScreenWidth, _ScreenHeight), Color.White); | |
spriteBatch.Draw(_WrapTarget, Vector2.Zero, new Rectangle(_ScreenWidth * 2, 0, _ScreenWidth, _ScreenHeight), Color.White); | |
spriteBatch.Draw(_WrapTarget, Vector2.Zero, new Rectangle(0, _ScreenHeight, _ScreenWidth, _ScreenHeight), Color.White); | |
spriteBatch.Draw(_WrapTarget, Vector2.Zero, new Rectangle(_ScreenWidth, _ScreenHeight, _ScreenWidth, _ScreenHeight), Color.White); | |
spriteBatch.Draw(_WrapTarget, Vector2.Zero, new Rectangle(_ScreenWidth * 2, _ScreenHeight, _ScreenWidth, _ScreenHeight), Color.White); | |
spriteBatch.Draw(_WrapTarget, Vector2.Zero, new Rectangle(0, _ScreenHeight * 2, _ScreenWidth, _ScreenHeight), Color.White); | |
spriteBatch.Draw(_WrapTarget, Vector2.Zero, new Rectangle(_ScreenWidth, _ScreenHeight * 2, _ScreenWidth, _ScreenHeight), Color.White); | |
spriteBatch.Draw(_WrapTarget, Vector2.Zero, new Rectangle(_ScreenWidth * 2, _ScreenHeight * 2, _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