Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace SpriteOutlineTest
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- private GraphicsDeviceManager _graphics;
- private SpriteBatch _spriteBatch;
- private Texture2D _texture;
- private Vector2 _pos = new Vector2(100, 100);
- private Effect _outlineEffect;
- public Game1()
- {
- _graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- this.IsMouseVisible = true;
- }
- protected override void Initialize()
- {
- base.Initialize();
- }
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- _spriteBatch = new SpriteBatch(GraphicsDevice);
- _texture = this.Content.Load<Texture2D>("sprite");
- _outlineEffect = this.Content.Load<Effect>("SpriteOutline");
- }
- protected override void UnloadContent()
- {
- }
- protected override void Update(GameTime gameTime)
- {
- KeyboardState ks = Keyboard.GetState();
- if (ks.IsKeyDown(Keys.Escape))
- this.Exit();
- float speed = 100;
- Vector2 dir = Vector2.Zero;
- if (ks.IsKeyDown(Keys.LeftShift))
- speed *= 2;
- if (ks.IsKeyDown(Keys.Left))
- dir.X -= 1;
- else if (ks.IsKeyDown(Keys.Right))
- dir.X += 1;
- if (ks.IsKeyDown(Keys.Up))
- dir.Y -= 1;
- else if (ks.IsKeyDown(Keys.Down))
- dir.Y += 1;
- Vector2 velocity = speed * dir;
- _pos += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
- if (_pos.X <= 0)
- _pos.X = 0;
- else if (_pos.X + _texture.Width > _graphics.PreferredBackBufferWidth)
- _pos.X = _graphics.PreferredBackBufferWidth - _texture.Width;
- if (_pos.Y <= 0)
- _pos.Y = 0;
- else if (_pos.Y + _texture.Height > _graphics.PreferredBackBufferHeight)
- _pos.Y = _graphics.PreferredBackBufferHeight - _texture.Height;
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.Black);
- _spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, _outlineEffect, null);
- _outlineEffect.Parameters["xTextureSize"].SetValue(new Vector2(_texture.Width, _texture.Height));
- _outlineEffect.Parameters["xOutlineColour"].SetValue(new Vector4(0, 255, 0, 255));
- _outlineEffect.CurrentTechnique.Passes[0].Apply();
- _spriteBatch.Draw(_texture, _pos, Color.White);
- _spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement