Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to bypass SpriteBatch sorting? C# XNA
    primarykey
    data
    text
    <p>I am working on a "falling sand" game in XNA. Of course, that means lots and lots of pixels must be drawn! It started fine, using a transparent blue color per pixel purely for testing algorithms. I was able to draw about 100,000 pixels to the screen before losing any frame rate (a steady 60 FPS), which was more than I needed! However, when I finally added a different color pixel (blue and red) and no other changes, my FPS dropped exponentially, only allowing for about 10,000 pixels to be drawn to the screen before FPS dropped to about 10. After experimenting some, I concluded that the sprite batch was most likely at fault, although I could be completely wrong and please let me know if you think of something different, because the sprite batch automatically sorts and blends the pixels together. I assume the act of blending the two colors together is where I am losing speed. So, how could I get around the sprite batch's methods? Or am I completely off base?</p> <p>I've tried messing with SpriteSortMode and BlendMode, and even only drawing like colored pixels per sprite batch call, but no luck so far.</p> <p>Also, the pixels I am drawing to the screen never have the same coordinates to begin with, so blending and the like is not needed.</p> <pre><code> //Game Draw Method protected override void Draw(GameTime gameTime) { stopWatch.Start(); GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(); //backgroundSprite.Draw(spriteBatch); terrainSprite.Draw(spriteBatch); resourceMap.Draw(spriteBatch, spriteFont); frameCounter++; string fps = string.Format("fps: {0}", frameRate); int totalint = 0; for (int i = 0; i &lt; resourceMap.activeCoordinates.Count; i++) { totalint += resourceMap.activeCoordinates[i].Count; } string total = string.Format("resource count: {0}", totalint); spriteBatch.DrawString(spriteFont, fps, new Vector2(33, 33), Color.White); spriteBatch.DrawString(spriteFont, total, new Vector2(33, 45), Color.White); spriteBatch.DrawString(spriteFont, totalTime, new Vector2(33, 17), Color.White); spriteBatch.DrawString(spriteFont, totalTime, new Vector2(33, 17), Color.White); spriteBatch.End(); stopWatch.Stop(); totalTime = "Draw Time: " + stopWatch.Elapsed; stopWatch = new Stopwatch(); base.Draw(gameTime); } //resourceMap.Draw(SpriteBatch spriteBatch, SpriteFont font) from Game Draw public void Draw(SpriteBatch spriteBatch, SpriteFont font) { //spriteBatch.End(); //this was originally one for loop to draw all pixels, but was separated to draw each different color pixel after discovering performance issue for (int c = 0; c &lt; activeCoordinates.Count(); c++) { //spriteBatch.Begin(); for (int i = 0; i &lt; activeCoordinates[c].Count; i++) { mapArray[(int)activeCoordinates[c][i].X][(int)activeCoordinates[c][i].Y].Draw(spriteBatch); } //spriteBatch.End(); } //spriteBatch.Begin(); spriteBatch.DrawString(font, timeInMilli, new Vector2(33, 0), Color.White); } </code></pre> <p>Again, the only change made to the code was adding different colors to the list of pixels to be drawn, so the code here was working great (100,000 pixels drawn at around 60fps) until I added an extra color.</p> <p>Thanks for the help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload