SLMCSampleComponent.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Audio;
  5. using Microsoft.Xna.Framework.Content;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. namespace Samples.SLMC
  9. {
  10. internal class SLMCSampleComponent : DrawableGameComponent
  11. {
  12. GraphicsDeviceManager graphics;
  13. ContentManager Content;
  14. SpriteBatch spriteBatch;
  15. SpriteFont font;
  16. KeyboardState previousKeyboardState;
  17. int mipLevel = 0;
  18. Rectangle rtSize;
  19. RenderTarget2D rt;
  20. Texture2D tx;
  21. public SLMCSampleComponent(Game game, GraphicsDeviceManager graphics) : base(game)
  22. {
  23. this.graphics = graphics;
  24. }
  25. /// <summary>Initializes the component. Used to load non-graphical resources.</summary>
  26. public override void Initialize()
  27. {
  28. Content = new ContentManager(Game.Services, "Content");
  29. base.Initialize();
  30. }
  31. /// <summary>Load graphical resources needed by this component.</summary>
  32. protected override void LoadContent()
  33. {
  34. spriteBatch = new SpriteBatch(GraphicsDevice);
  35. font = Content.Load<SpriteFont>("font");
  36. tx = Content.Load<Texture2D>("b_c0123");
  37. rtSize = new Rectangle(0, 0, tx.Width * (1+4), tx.Height);
  38. rt = new RenderTarget2D(GraphicsDevice, rtSize.Width, rtSize.Height);
  39. graphics.PreferredBackBufferWidth = (int)rtSize.Width;
  40. graphics.PreferredBackBufferHeight = (int)rtSize.Height;
  41. graphics.ApplyChanges();
  42. }
  43. /// <summary>Unload graphical resources needed by this component.</summary>
  44. protected override void UnloadContent()
  45. {
  46. Content.Unload();
  47. }
  48. /// <summary>Update the component.</summary>
  49. /// <param name="gameTime">GameTime of the Game.</param>
  50. public override void Update(GameTime gameTime)
  51. {
  52. KeyboardState keyState = Keyboard.GetState();
  53. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  54. if (keyState.IsKeyDown(Keys.OemPlus) && !previousKeyboardState.IsKeyDown(Keys.OemPlus) && mipLevel < tx.LevelCount-1)
  55. mipLevel++;
  56. if (keyState.IsKeyDown(Keys.Add) && !previousKeyboardState.IsKeyDown(Keys.Add) && mipLevel < tx.LevelCount-1)
  57. mipLevel++;
  58. if (keyState.IsKeyDown(Keys.OemMinus) && !previousKeyboardState.IsKeyDown(Keys.OemMinus) && mipLevel > 0)
  59. mipLevel--;
  60. if (keyState.IsKeyDown(Keys.Subtract) && !previousKeyboardState.IsKeyDown(Keys.Subtract) && mipLevel > 0)
  61. mipLevel--;
  62. previousKeyboardState = keyState;
  63. }
  64. /// <summary>Draw this component.</summary>
  65. /// <param name="gameTime">The time elapsed since the last call to Draw.</param>
  66. public override void Draw(GameTime gameTime)
  67. {
  68. GraphicsDevice.BlendState = BlendState.Opaque;
  69. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  70. int mipLevel2 = (int)Math.Pow(2, mipLevel);
  71. var mipSize = rtSize;
  72. mipSize.Width /= mipLevel2;
  73. mipSize.Height /= mipLevel2;
  74. GraphicsDevice.SetRenderTarget(rt);
  75. GraphicsDevice.Clear(Color.Black);
  76. spriteBatch.Begin();
  77. {
  78. var destRect = new Rectangle(0, 0, tx.Width, tx.Height);
  79. destRect.X /= mipLevel2;
  80. destRect.Y /= mipLevel2;
  81. destRect.Width /= mipLevel2;
  82. destRect.Height /= mipLevel2;
  83. // draw all channels
  84. destRect.X = (tx.Width * 0) / mipLevel2;
  85. spriteBatch.Draw(tx, destRect, Color.White);
  86. // draw each channels
  87. destRect.X = (tx.Width * 1) / mipLevel2;
  88. spriteBatch.Draw(tx, destRect, new Color(1f, 0f, 0f, 0f));
  89. destRect.X = (tx.Width * 2) / mipLevel2;
  90. spriteBatch.Draw(tx, destRect, new Color(0f, 1f, 0f, 0f));
  91. destRect.X = (tx.Width * 3) / mipLevel2;
  92. spriteBatch.Draw(tx, destRect, new Color(0f, 0f, 1f, 0f));
  93. destRect.X = (tx.Width * 4) / mipLevel2;
  94. spriteBatch.Draw(tx, destRect, new Color(0f, 0f, 0f, 1f)); // NOTE: alpha channel is not visible
  95. }
  96. spriteBatch.End();
  97. GraphicsDevice.SetRenderTarget(null);
  98. GraphicsDevice.Clear(Color.CornflowerBlue);
  99. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null);
  100. spriteBatch.Draw(rt, rtSize, mipSize, Color.White);
  101. spriteBatch.End();
  102. spriteBatch.Begin();
  103. spriteBatch.DrawString(font, String.Format("[+/-] MipLevel - ({0})", mipLevel), new Vector2(11, 11), Color.Black);
  104. spriteBatch.DrawString(font, String.Format("[+/-] MipLevel - ({0})", mipLevel), new Vector2(10, 10), Color.White);
  105. spriteBatch.End();
  106. }
  107. }
  108. }