AtlasSampleComponent.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. using nkast.Aether.Graphics;
  9. namespace Samples.Atlas
  10. {
  11. internal class AtlasSampleComponent : DrawableGameComponent
  12. {
  13. GraphicsDeviceManager graphics;
  14. ContentManager Content;
  15. SpriteBatch spriteBatch;
  16. SpriteFont font;
  17. KeyboardState previousKeyboardState;
  18. int mipLevel = 4;
  19. bool showAtlas = false;
  20. bool useGenerateBitmap = true;
  21. bool useMipmapPerSprite = true;
  22. RenderTarget2D rt;
  23. Rectangle atlasSize = new Rectangle(0, 0, 1024, 512);
  24. TextureAtlas atlasMipmapPerSprite;
  25. TextureAtlas atlasMipmap;
  26. TextureAtlas atlasNoMipmap;
  27. public AtlasSampleComponent(Game game, GraphicsDeviceManager graphics) : base(game)
  28. {
  29. this.graphics = graphics;
  30. }
  31. /// <summary>Initializes the component. Used to load non-graphical resources.</summary>
  32. public override void Initialize()
  33. {
  34. Content = new ContentManager(Game.Services, "Content");
  35. base.Initialize();
  36. }
  37. /// <summary>Load graphical resources needed by this component.</summary>
  38. protected override void LoadContent()
  39. {
  40. spriteBatch = new SpriteBatch(GraphicsDevice);
  41. font = Content.Load<SpriteFont>("font");
  42. rt = new RenderTarget2D(GraphicsDevice, atlasSize.Width, atlasSize.Height);
  43. // Load Atlas
  44. atlasMipmapPerSprite = Content.Load<TextureAtlas>("atlasMipmapPerSprite");
  45. atlasMipmap = Content.Load<TextureAtlas>("atlasMipmap");
  46. atlasNoMipmap = Content.Load<TextureAtlas>("atlasNoMipmap");
  47. graphics.PreferredBackBufferWidth = atlasSize.Width;
  48. graphics.PreferredBackBufferHeight = atlasSize.Height;
  49. graphics.ApplyChanges();
  50. }
  51. /// <summary>Unload graphical resources needed by this component.</summary>
  52. protected override void UnloadContent()
  53. {
  54. Content.Unload();
  55. }
  56. /// <summary>Update the component.</summary>
  57. /// <param name="gameTime">GameTime of the Game.</param>
  58. public override void Update(GameTime gameTime)
  59. {
  60. KeyboardState keyState = Keyboard.GetState();
  61. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
  62. if (keyState.IsKeyDown(Keys.F1) && !previousKeyboardState.IsKeyDown(Keys.F1))
  63. useMipmapPerSprite = !useMipmapPerSprite;
  64. if (keyState.IsKeyDown(Keys.F2) && !previousKeyboardState.IsKeyDown(Keys.F2))
  65. useGenerateBitmap = !useGenerateBitmap;
  66. if (keyState.IsKeyDown(Keys.F3) && !previousKeyboardState.IsKeyDown(Keys.F3))
  67. showAtlas = !showAtlas;
  68. if (keyState.IsKeyDown(Keys.OemPlus) && !previousKeyboardState.IsKeyDown(Keys.OemPlus) && mipLevel < 10)
  69. mipLevel++;
  70. if (keyState.IsKeyDown(Keys.Add) && !previousKeyboardState.IsKeyDown(Keys.Add) && mipLevel < 10)
  71. mipLevel++;
  72. if (keyState.IsKeyDown(Keys.OemMinus) && !previousKeyboardState.IsKeyDown(Keys.OemMinus) && mipLevel > 0)
  73. mipLevel--;
  74. if (keyState.IsKeyDown(Keys.Subtract) && !previousKeyboardState.IsKeyDown(Keys.Subtract) && mipLevel > 0)
  75. mipLevel--;
  76. previousKeyboardState = keyState;
  77. }
  78. private void DrawSprites(GameTime gameTime, SpriteBatch spriteBatch, TextureAtlas atlas)
  79. {
  80. var sprite18 = atlas.Sprites["18"];
  81. spriteBatch.Draw(sprite18, new Vector2(128, 128), Color.White);
  82. var spriteMushroom_2 = atlas.Sprites["Mushroom_2"];
  83. spriteBatch.Draw(spriteMushroom_2, new Vector2(256 + 128, 128), Color.White);
  84. var sprite10 = atlas.Sprites["10"];
  85. spriteBatch.Draw(sprite10, new Vector2(512, 128), Color.White);
  86. }
  87. /// <summary>Draw this component.</summary>
  88. /// <param name="gameTime">The time elapsed since the last call to Draw.</param>
  89. public override void Draw(GameTime gameTime)
  90. {
  91. GraphicsDevice.BlendState = BlendState.Opaque;
  92. GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  93. int mipLevel2 = (int)Math.Pow(2, mipLevel);
  94. var mipSize = atlasSize;
  95. mipSize.Width /= mipLevel2;
  96. mipSize.Height /= mipLevel2;
  97. GraphicsDevice.SetRenderTarget(rt);
  98. GraphicsDevice.Clear(Color.Black);
  99. var currentAtlas = (useGenerateBitmap) ? (useMipmapPerSprite ? atlasMipmapPerSprite : atlasMipmap) : atlasNoMipmap;
  100. if (showAtlas)
  101. {
  102. spriteBatch.Begin();
  103. spriteBatch.Draw(currentAtlas.Texture, mipSize, Color.White);
  104. spriteBatch.End();
  105. }
  106. else
  107. {
  108. var scaleMtx = Matrix.CreateScale(1f / mipLevel2);
  109. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, scaleMtx);
  110. // Draw sprites from Atlas
  111. DrawSprites(gameTime, spriteBatch, currentAtlas);
  112. spriteBatch.End();
  113. }
  114. GraphicsDevice.SetRenderTarget(null);
  115. GraphicsDevice.Clear(Color.CornflowerBlue);
  116. spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointWrap, null, null);
  117. spriteBatch.Draw(rt, atlasSize, mipSize, Color.White);
  118. spriteBatch.End();
  119. spriteBatch.Begin();
  120. spriteBatch.DrawString(font, String.Format("[F1] MipmapPerSprite - ({0})", useMipmapPerSprite ? "ON" : "OFF"), new Vector2(20, 20), Color.White);
  121. spriteBatch.DrawString(font, String.Format("[F2] GenerateMipmap - ({0})", useGenerateBitmap ? "ON" : "OFF"), new Vector2(20, 40), Color.White);
  122. spriteBatch.DrawString(font, String.Format("[F3] {0}", showAtlas ? "Show Sprites" : "Show Atlas"), new Vector2(20, 60), Color.White);
  123. spriteBatch.DrawString(font, String.Format("[+/-] MipLevel - ({0})", mipLevel), new Vector2(20, 80), Color.White);
  124. spriteBatch.End();
  125. }
  126. }
  127. }