ShaderTest.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. namespace ShaderTests
  12. {
  13. public class ShaderTest : Microsoft.Xna.Framework.Game
  14. {
  15. GraphicsDeviceManager graphics;
  16. SpriteBatch spriteBatch;
  17. KeyboardState PreviousKeyState;
  18. KeyboardState CurrentKeyState;
  19. Texture2D background;
  20. Texture2D surge;
  21. List<Effect> shaderEffects;
  22. int shaderEffectIdx;
  23. public ShaderTest()
  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. background = Content.Load<Texture2D>(@"images\bg5");
  37. surge = Content.Load<Texture2D>(@"images\surge");
  38. shaderEffects = new List<Effect>();
  39. shaderEffects.Add(Content.Load<Effect>(@"effects\NoEffect"));
  40. shaderEffects.Add(Content.Load<Effect>(@"effects\HighContrast"));
  41. shaderEffects.Add(Content.Load<Effect>(@"effects\Bevels"));
  42. shaderEffects.Add(Content.Load<Effect>(@"effects\Grayscale"));
  43. shaderEffects.Add(Content.Load<Effect>(@"effects\ColorFlip"));
  44. shaderEffects.Add(Content.Load<Effect>(@"effects\Invert"));
  45. shaderEffects.Add(Content.Load<Effect>(@"effects\BlackOut"));
  46. shaderEffects.Add(Content.Load<Effect>(@"effects\RainbowH"));
  47. }
  48. protected override void Update(GameTime gameTime)
  49. {
  50. PreviousKeyState = CurrentKeyState;
  51. CurrentKeyState = Keyboard.GetState();
  52. if (CurrentKeyState.IsKeyDown(Keys.Escape)) this.Exit();
  53. if (CurrentKeyState.IsKeyDown(Keys.Up) && PreviousKeyState.IsKeyUp(Keys.Up))
  54. {
  55. shaderEffectIdx++;
  56. if (shaderEffectIdx >= shaderEffects.Count()) shaderEffectIdx = 0;
  57. }
  58. if (CurrentKeyState.IsKeyDown(Keys.Down) && PreviousKeyState.IsKeyUp(Keys.Down))
  59. {
  60. shaderEffectIdx--;
  61. if (shaderEffectIdx < 0 ) shaderEffectIdx = shaderEffects.Count() - 1;
  62. }
  63. base.Update(gameTime);
  64. }
  65. protected override void Draw(GameTime gameTime)
  66. {
  67. GraphicsDevice.Clear(Color.Black);
  68. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
  69. spriteBatch.Draw(background, new Vector2(-200, -200), Color.White);
  70. shaderEffects[shaderEffectIdx].CurrentTechnique.Passes[0].Apply();
  71. spriteBatch.Draw(surge, new Vector2(300,200), null, Color.White, 0f, Vector2.Zero, 2.0f, SpriteEffects.None, 0f);
  72. spriteBatch.End();
  73. base.Draw(gameTime);
  74. }
  75. }
  76. }