Game1.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. namespace Samples.SLMC
  6. {
  7. public class Game1 : Microsoft.Xna.Framework.Game
  8. {
  9. GraphicsDeviceManager graphics;
  10. SpriteBatch spriteBatch;
  11. SpriteFont font;
  12. KeyboardState previousKeyboardState;
  13. int mipLevel = 0;
  14. Rectangle rtSize;
  15. RenderTarget2D rt;
  16. Texture2D tx;
  17. public Game1()
  18. {
  19. graphics = new GraphicsDeviceManager(this);
  20. Content.RootDirectory = "Content";
  21. graphics.GraphicsProfile = GraphicsProfile.HiDef;
  22. graphics.PreferredBackBufferWidth = 800;
  23. graphics.PreferredBackBufferHeight = 480;
  24. }
  25. protected override void LoadContent()
  26. {
  27. spriteBatch = new SpriteBatch(GraphicsDevice);
  28. font = Content.Load<SpriteFont>("font");
  29. tx = Content.Load<Texture2D>("b_c0123");
  30. rtSize = new Rectangle(0, 0, tx.Width * (1+4), tx.Height);
  31. graphics.PreferredBackBufferWidth = (int)rtSize.Width;
  32. graphics.PreferredBackBufferHeight = (int)rtSize.Height;
  33. graphics.ApplyChanges();
  34. rt = new RenderTarget2D(GraphicsDevice, rtSize.Width, rtSize.Height);
  35. }
  36. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  37. protected override void Update(GameTime gameTime)
  38. {
  39. KeyboardState keyState = Keyboard.GetState();
  40. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  41. if (keyState.IsKeyDown(Keys.Escape) || gamePadState.Buttons.Back == ButtonState.Pressed)
  42. Exit();
  43. if (keyState.IsKeyDown(Keys.OemPlus) && !previousKeyboardState.IsKeyDown(Keys.OemPlus) && mipLevel < tx.LevelCount-1)
  44. mipLevel++;
  45. if (keyState.IsKeyDown(Keys.Add) && !previousKeyboardState.IsKeyDown(Keys.Add) && mipLevel < tx.LevelCount-1)
  46. mipLevel++;
  47. if (keyState.IsKeyDown(Keys.OemMinus) && !previousKeyboardState.IsKeyDown(Keys.OemMinus) && mipLevel > 0)
  48. mipLevel--;
  49. if (keyState.IsKeyDown(Keys.Subtract) && !previousKeyboardState.IsKeyDown(Keys.Subtract) && mipLevel > 0)
  50. mipLevel--;
  51. previousKeyboardState = keyState;
  52. base.Update(gameTime);
  53. }
  54. protected override void Draw(GameTime gameTime)
  55. {
  56. GraphicsDevice.BlendState = BlendState.Opaque;
  57. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  58. int mipLevel2 = (int)Math.Pow(2, mipLevel);
  59. var mipSize = rtSize;
  60. mipSize.Width /= mipLevel2;
  61. mipSize.Height /= mipLevel2;
  62. GraphicsDevice.SetRenderTarget(rt);
  63. GraphicsDevice.Clear(Color.Black);
  64. spriteBatch.Begin();
  65. {
  66. var destRect = new Rectangle(0, 0, tx.Width, tx.Height);
  67. destRect.X /= mipLevel2;
  68. destRect.Y /= mipLevel2;
  69. destRect.Width /= mipLevel2;
  70. destRect.Height /= mipLevel2;
  71. // draw all channels
  72. destRect.X = (tx.Width * 0) / mipLevel2;
  73. spriteBatch.Draw(tx, destRect, Color.White);
  74. // draw each channels
  75. destRect.X = (tx.Width * 1) / mipLevel2;
  76. spriteBatch.Draw(tx, destRect, new Color(1f, 0f, 0f, 0f));
  77. destRect.X = (tx.Width * 2) / mipLevel2;
  78. spriteBatch.Draw(tx, destRect, new Color(0f, 1f, 0f, 0f));
  79. destRect.X = (tx.Width * 3) / mipLevel2;
  80. spriteBatch.Draw(tx, destRect, new Color(0f, 0f, 1f, 0f));
  81. destRect.X = (tx.Width * 4) / mipLevel2;
  82. spriteBatch.Draw(tx, destRect, new Color(0f, 0f, 0f, 1f)); // NOTE: alpha channel is not visible
  83. }
  84. spriteBatch.End();
  85. GraphicsDevice.SetRenderTarget(null);
  86. GraphicsDevice.Clear(Color.CornflowerBlue);
  87. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointWrap, null, null);
  88. spriteBatch.Draw(rt, rtSize, mipSize, Color.White);
  89. spriteBatch.End();
  90. spriteBatch.Begin();
  91. spriteBatch.DrawString(font, String.Format("[+/-] MipLevel - ({0})", mipLevel), new Vector2(11, 11), Color.Black);
  92. spriteBatch.DrawString(font, String.Format("[+/-] MipLevel - ({0})", mipLevel), new Vector2(10, 10), Color.White);
  93. spriteBatch.End();
  94. base.Draw(gameTime);
  95. }
  96. }
  97. }