SampleGame.cs 4.6 KB

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