Game1.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. using tainicom.Aether.Graphics;
  6. namespace Samples.Atlas
  7. {
  8. public class Game1 : Microsoft.Xna.Framework.Game
  9. {
  10. GraphicsDeviceManager graphics;
  11. SpriteBatch spriteBatch;
  12. SpriteFont font;
  13. KeyboardState previousKeyboardState;
  14. int mipLevel = 4;
  15. bool showAtlas = false;
  16. bool useGenerateBitmap = true;
  17. bool useMipmapPerSprite = true;
  18. Rectangle atlasSize = new Rectangle(0, 0, 1024, 512);
  19. RenderTarget2D rt;
  20. TextureAtlas atlasMipmapPerSprite;
  21. TextureAtlas atlasMipmap;
  22. TextureAtlas atlasNoMipmap;
  23. public Game1()
  24. {
  25. graphics = new GraphicsDeviceManager(this);
  26. Content.RootDirectory = "Content";
  27. graphics.PreferredBackBufferWidth = atlasSize.Width;
  28. graphics.PreferredBackBufferHeight = atlasSize.Height;
  29. }
  30. protected override void LoadContent()
  31. {
  32. spriteBatch = new SpriteBatch(GraphicsDevice);
  33. font = Content.Load<SpriteFont>("font");
  34. rt = new RenderTarget2D(GraphicsDevice, atlasSize.Width, atlasSize.Height);
  35. // Load Atlas
  36. atlasMipmapPerSprite = Content.Load<TextureAtlas>("atlasMipmapPerSprite");
  37. atlasMipmap = Content.Load<TextureAtlas>("atlasMipmap");
  38. atlasNoMipmap = Content.Load<TextureAtlas>("atlasNoMipmap");
  39. }
  40. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  41. protected override void Update(GameTime gameTime)
  42. {
  43. KeyboardState keyState = Keyboard.GetState();
  44. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  45. if (keyState.IsKeyDown(Keys.Escape) || gamePadState.Buttons.Back == ButtonState.Pressed)
  46. Exit();
  47. if (keyState.IsKeyDown(Keys.F1) && !previousKeyboardState.IsKeyDown(Keys.F1))
  48. useMipmapPerSprite = !useMipmapPerSprite;
  49. if (keyState.IsKeyDown(Keys.F2) && !previousKeyboardState.IsKeyDown(Keys.F2))
  50. useGenerateBitmap = !useGenerateBitmap;
  51. if (keyState.IsKeyDown(Keys.F3) && !previousKeyboardState.IsKeyDown(Keys.F3))
  52. showAtlas = !showAtlas;
  53. if (keyState.IsKeyDown(Keys.OemPlus) && !previousKeyboardState.IsKeyDown(Keys.OemPlus) && mipLevel < 10)
  54. mipLevel++;
  55. if (keyState.IsKeyDown(Keys.Add) && !previousKeyboardState.IsKeyDown(Keys.Add) && mipLevel < 10)
  56. mipLevel++;
  57. if (keyState.IsKeyDown(Keys.OemMinus) && !previousKeyboardState.IsKeyDown(Keys.OemMinus) && mipLevel > 0)
  58. mipLevel--;
  59. if (keyState.IsKeyDown(Keys.Subtract) && !previousKeyboardState.IsKeyDown(Keys.Subtract) && mipLevel > 0)
  60. mipLevel--;
  61. previousKeyboardState = keyState;
  62. base.Update(gameTime);
  63. }
  64. protected override void Draw(GameTime gameTime)
  65. {
  66. GraphicsDevice.BlendState = BlendState.Opaque;
  67. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  68. int mipLevel2 = (int)Math.Pow(2, mipLevel);
  69. var mipSize = atlasSize;
  70. mipSize.Width /= mipLevel2;
  71. mipSize.Height /= mipLevel2;
  72. GraphicsDevice.SetRenderTarget(rt);
  73. GraphicsDevice.Clear(Color.Black);
  74. var currentAtlas = (useGenerateBitmap) ? (useMipmapPerSprite ? atlasMipmapPerSprite : atlasMipmap) : atlasNoMipmap;
  75. if (showAtlas)
  76. {
  77. spriteBatch.Begin();
  78. spriteBatch.Draw(currentAtlas.Texture, mipSize, Color.White);
  79. spriteBatch.End();
  80. }
  81. else
  82. {
  83. var scaleMtx = Matrix.CreateScale(1f/mipLevel2);
  84. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, scaleMtx);
  85. // Draw sprites from Atlas
  86. DrawSprites(gameTime, spriteBatch, currentAtlas);
  87. spriteBatch.End();
  88. }
  89. GraphicsDevice.SetRenderTarget(null);
  90. GraphicsDevice.Clear(Color.CornflowerBlue);
  91. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointWrap, null, null);
  92. spriteBatch.Draw(rt, atlasSize, mipSize, Color.White);
  93. spriteBatch.End();
  94. spriteBatch.Begin();
  95. spriteBatch.DrawString(font, String.Format("[F1] MipmapPerSprite - ({0})", useMipmapPerSprite ? "ON" : "OFF"), new Vector2(20, 20), Color.White);
  96. spriteBatch.DrawString(font, String.Format("[F2] GenerateMipmap - ({0})", useGenerateBitmap ? "ON" : "OFF"), new Vector2(20, 40), Color.White);
  97. spriteBatch.DrawString(font, String.Format("[F3] {0}", showAtlas? "Show Sprites" : "Show Atlas"), new Vector2(20, 60), Color.White);
  98. spriteBatch.DrawString(font, String.Format("[+/-] MipLevel - ({0})", mipLevel), new Vector2(20, 80), Color.White);
  99. spriteBatch.End();
  100. base.Draw(gameTime);
  101. }
  102. private void DrawSprites(GameTime gameTime, SpriteBatch spriteBatch, TextureAtlas atlas)
  103. {
  104. var sprite18 = atlas.Sprites["18"];
  105. spriteBatch.Draw(sprite18, new Vector2(128,128), Color.White);
  106. var spriteMushroom_2 = atlas.Sprites["Mushroom_2"];
  107. spriteBatch.Draw(spriteMushroom_2, new Vector2(256 + 128, 128), Color.White);
  108. var sprite10 = atlas.Sprites["10"];
  109. spriteBatch.Draw(sprite10, new Vector2(512, 128), Color.White);
  110. }
  111. }
  112. }