Skip to content

Instantly share code, notes, and snippets.

@flibitijibibo
Created September 24, 2020 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flibitijibibo/69dbf5fb80d6bdf80a58a449be4379d4 to your computer and use it in GitHub Desktop.
Save flibitijibibo/69dbf5fb80d6bdf80a58a449be4379d4 to your computer and use it in GitHub Desktop.
Batching/Sorting accruacy test for SpriteBatch
/* SpriteBatch Stress Test
* Written by Ethan "flibitijibibo" Lee
* http://www.flibitijibibo.com/
*
* Released under public domain.
* No warranty implied; use at your own risk.
*/
using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
class SpriteBatchTest : Game
{
private const int SPRITECOUNT = 2048;
private const int TEXTURESIZE = 128;
private SpriteSortMode mode = SpriteSortMode.BackToFront;
private Texture2D box;
private SpriteBatch batch;
public SpriteBatchTest() : base()
{
new GraphicsDeviceManager(this);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
batch.Begin(mode, BlendState.Opaque);
for (int i = 0; i < SPRITECOUNT; i += 1)
{
batch.Draw(
box,
Vector2.Zero,
null,
new Color(
i / (float) SPRITECOUNT,
0.0f,
(SPRITECOUNT - i) / (float) SPRITECOUNT,
1.0f
),
0.0f,
Vector2.Zero,
1.0f,
SpriteEffects.None,
i / (float) SPRITECOUNT
);
}
batch.End();
}
protected override void Update(GameTime gameTime)
{
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Keys.D1))
{
// Blue
mode = SpriteSortMode.BackToFront;
}
else if (state.IsKeyDown(Keys.D2))
{
// Red
mode = SpriteSortMode.FrontToBack;
}
}
protected override void LoadContent()
{
Color[] color = new Color[TEXTURESIZE * TEXTURESIZE];
for (int i = 0; i < color.Length; i += 1)
{
color[i] = Color.White;
}
box = new Texture2D(GraphicsDevice, TEXTURESIZE, TEXTURESIZE);
box.SetData(color);
batch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
batch.Dispose();
batch = null;
box.Dispose();
box = null;
}
public static void Main(string[] args)
{
SpriteBatchTest game = new SpriteBatchTest();
{
game.Run();
}
game.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment