ShaderTest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Microsoft.Xna.Framework.Input;
  9. using Microsoft.Xna.Framework.Media;
  10. namespace ShaderTest
  11. {
  12. public class ShaderTestGame : Game
  13. {
  14. GraphicsDeviceManager graphics;
  15. SpriteBatch spriteBatch;
  16. KeyboardState PreviousKeyState;
  17. KeyboardState CurrentKeyState;
  18. Texture2D background;
  19. Texture2D surge;
  20. SpriteFont font;
  21. List<Effect> shaderEffects;
  22. int shaderEffectIdx;
  23. public ShaderTestGame()
  24. {
  25. graphics = new GraphicsDeviceManager(this);
  26. Content.RootDirectory = "Content";
  27. shaderEffectIdx = 0;
  28. }
  29. protected override void Initialize()
  30. {
  31. spriteBatch = new SpriteBatch(GraphicsDevice);
  32. base.Initialize();
  33. }
  34. protected override void LoadContent()
  35. {
  36. font = Content.Load<SpriteFont>("font");
  37. background = Content.Load<Texture2D>(@"images\bg5");
  38. surge = Content.Load<Texture2D>(@"images\surge");
  39. shaderEffects = new List<Effect>();
  40. shaderEffects.Add(Content.Load<Effect>(@"effects\NoEffect"));
  41. shaderEffects.Add(Content.Load<Effect>(@"effects\HighContrast"));
  42. shaderEffects.Add(Content.Load<Effect>(@"effects\Bevels"));
  43. shaderEffects.Add(Content.Load<Effect>(@"effects\Grayscale"));
  44. shaderEffects.Add(Content.Load<Effect>(@"effects\ColorFlip"));
  45. shaderEffects.Add(Content.Load<Effect>(@"effects\Invert"));
  46. shaderEffects.Add(Content.Load<Effect>(@"effects\BlackOut"));
  47. shaderEffects.Add(Content.Load<Effect>(@"effects\RainbowH"));
  48. }
  49. protected override void Update(GameTime gameTime)
  50. {
  51. PreviousKeyState = CurrentKeyState;
  52. CurrentKeyState = Keyboard.GetState();
  53. if (CurrentKeyState.IsKeyDown(Keys.Escape)) this.Exit();
  54. if (CurrentKeyState.IsKeyDown(Keys.Up) && PreviousKeyState.IsKeyUp(Keys.Up))
  55. {
  56. shaderEffectIdx++;
  57. if (shaderEffectIdx >= shaderEffects.Count()) shaderEffectIdx = 0;
  58. }
  59. if (CurrentKeyState.IsKeyDown(Keys.Down) && PreviousKeyState.IsKeyUp(Keys.Down))
  60. {
  61. shaderEffectIdx--;
  62. if (shaderEffectIdx < 0 ) shaderEffectIdx = shaderEffects.Count() - 1;
  63. }
  64. base.Update(gameTime);
  65. }
  66. protected override void Draw(GameTime gameTime)
  67. {
  68. GraphicsDevice.Clear(Color.Black);
  69. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
  70. spriteBatch.Draw(background, new Vector2(-200, -200), Color.White);
  71. shaderEffects[shaderEffectIdx].CurrentTechnique.Passes[0].Apply();
  72. spriteBatch.Draw(surge, new Vector2(300,200), null, Color.White, 0f, Vector2.Zero, 2.0f, SpriteEffects.None, 0f);
  73. spriteBatch.End();
  74. spriteBatch.Begin();
  75. spriteBatch.DrawString(font, "Press Up/Down to change shader effect", new Vector2(10, 10), Color.White);
  76. spriteBatch.DrawString(font, "Current Effect: " + shaderEffects[shaderEffectIdx].Name, new Vector2(10, 30), Color.White);
  77. spriteBatch.End();
  78. base.Draw(gameTime);
  79. }
  80. }
  81. }