Advertisement
Guest User

Sprite Outline Test

a guest
Feb 7th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4.  
  5. namespace SpriteOutlineTest
  6. {
  7.     /// <summary>
  8.     /// This is the main type for your game.
  9.     /// </summary>
  10.     public class Game1 : Game
  11.     {
  12.         private GraphicsDeviceManager _graphics;
  13.         private SpriteBatch _spriteBatch;
  14.         private Texture2D _texture;
  15.         private Vector2 _pos = new Vector2(100, 100);
  16.         private Effect _outlineEffect;
  17.  
  18.         public Game1()
  19.         {
  20.             _graphics = new GraphicsDeviceManager(this);
  21.             Content.RootDirectory = "Content";
  22.             this.IsMouseVisible = true;
  23.         }
  24.  
  25.         protected override void Initialize()
  26.         {
  27.  
  28.             base.Initialize();
  29.         }
  30.  
  31.         protected override void LoadContent()
  32.         {
  33.             // Create a new SpriteBatch, which can be used to draw textures.
  34.             _spriteBatch = new SpriteBatch(GraphicsDevice);
  35.  
  36.             _texture = this.Content.Load<Texture2D>("sprite");
  37.             _outlineEffect = this.Content.Load<Effect>("SpriteOutline");
  38.         }
  39.  
  40.         protected override void UnloadContent()
  41.         {
  42.         }
  43.  
  44.         protected override void Update(GameTime gameTime)
  45.         {
  46.             KeyboardState ks = Keyboard.GetState();
  47.  
  48.             if (ks.IsKeyDown(Keys.Escape))
  49.                 this.Exit();
  50.  
  51.             float speed = 100;
  52.             Vector2 dir = Vector2.Zero;
  53.  
  54.             if (ks.IsKeyDown(Keys.LeftShift))
  55.                 speed *= 2;
  56.  
  57.             if (ks.IsKeyDown(Keys.Left))
  58.                 dir.X -= 1;
  59.             else if (ks.IsKeyDown(Keys.Right))
  60.                 dir.X += 1;
  61.  
  62.             if (ks.IsKeyDown(Keys.Up))
  63.                 dir.Y -= 1;
  64.             else if (ks.IsKeyDown(Keys.Down))
  65.                 dir.Y += 1;
  66.  
  67.             Vector2 velocity = speed * dir;
  68.             _pos += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
  69.  
  70.             if (_pos.X <= 0)
  71.                 _pos.X = 0;
  72.             else if (_pos.X + _texture.Width > _graphics.PreferredBackBufferWidth)
  73.                 _pos.X = _graphics.PreferredBackBufferWidth - _texture.Width;
  74.  
  75.             if (_pos.Y <= 0)
  76.                 _pos.Y = 0;
  77.             else if (_pos.Y + _texture.Height > _graphics.PreferredBackBufferHeight)
  78.                 _pos.Y = _graphics.PreferredBackBufferHeight - _texture.Height;
  79.  
  80.             base.Update(gameTime);
  81.         }
  82.  
  83.         protected override void Draw(GameTime gameTime)
  84.         {
  85.             GraphicsDevice.Clear(Color.Black);
  86.  
  87.             _spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, _outlineEffect, null);
  88.  
  89.             _outlineEffect.Parameters["xTextureSize"].SetValue(new Vector2(_texture.Width, _texture.Height));
  90.             _outlineEffect.Parameters["xOutlineColour"].SetValue(new Vector4(0, 255, 0, 255));
  91.             _outlineEffect.CurrentTechnique.Passes[0].Apply();
  92.  
  93.             _spriteBatch.Draw(_texture, _pos, Color.White);
  94.  
  95.             _spriteBatch.End();
  96.  
  97.  
  98.             base.Draw(gameTime);
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement