XNA - How to increase 2D sprite rendering performance

Discussion in 'Game Development (Technical)' started by earlgraycz, Feb 4, 2014.

  1. earlgraycz

    earlgraycz New Member

    Joined:
    Feb 4, 2014
    Messages:
    1
    Likes Received:
    0
    Hello,

    I plan to program the isometric strategy game. Right now I'm doing performance tests of XNA drawing and I'm really disappointed. When I draw4000 tilesFPS drops to 30 frames per second. That's a pretty poor performance, so I wonder if I do not do anything wrong.

    My code is very simple:

    Code:
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    
    namespace XnaDrawingPerformanceTest
    {
        public class Game1 : Game
        {
            GraphicsDeviceManager _graphics;
            SpriteBatch _spriteBatch;
    
            SpriteFont font;
            Texture2D TilePng;
            Vector2 tilePosition, textPosition;
    
            public Game1()
            {
                _graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
            }
    
            protected override void Initialize()
            {
                base.IsMouseVisible = true;
    
                base.Initialize();
            }
    
            protected override void LoadContent()
            {
                _spriteBatch = new SpriteBatch(GraphicsDevice);
    
                font = Content.Load<SpriteFont>("Debug");
                TilePng = Content.Load<Texture2D>("TilePng");
    
                tilePosition = new Vector2(32, 32);
                textPosition = new Vector2(8, 8);
            }
    
            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
            }
    
            protected override void Update(GameTime gameTime)
            {
                base.Update(gameTime);
            }
    
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                _spriteBatch.Begin();
    
                _spriteBatch.DrawString(font, "FPS: " + (1000 / gameTime.ElapsedGameTime.Milliseconds), textPosition, Color.White);
    
                for (int i = 0; i < 4000; i++)
                {
                    _spriteBatch.Draw(TilePng, tilePosition, Color.White);
                }
    
                _spriteBatch.End();
    
                base.Draw(gameTime);
            }
        }
    }
    
    When I delete row _spriteBatch.Draw(TilePng, tilePosition, Color.White); then FPS is again full 60 frames per second. Please advice me how can I improve this code for better performance.
     
  2. xteamsoftware

    xteamsoftware New Member

    Joined:
    Jun 4, 2013
    Messages:
    3
    Likes Received:
    0
    Dear earlgraycz

    the size ( pixels ) of the TilePng ?
     
  3. geek_freek

    geek_freek New Member

    Joined:
    May 7, 2014
    Messages:
    1
    Likes Received:
    0
    there are two things you can look up with the optimizations.
    1.) Try compressing the tile image. you can use photoshop too.(not the best solution though).
    2.) you can cut down on the drawing portion a bit. when you are drawing its simultaneously rendering those 4000 tiles !!
    draw 4 tiles or any number you like then delete that batch !! draw the next 4 tiles n so on till 4000. !!
     
  4. Macro

    Macro New Member

    Joined:
    May 20, 2014
    Messages:
    32
    Likes Received:
    24
    From the information given I can assume that you may have 4000 active game objects, but only a few of them would be on the screen at the same time. Look into spatial partitioning such as quadtrees, BSP trees, KDtrees etc. This way you can just cull everything from rendering if it is outside of the initial screen. If you for some reason need all 4000 to be on screen at once then look at this thread:

    http://gamedev.stackexchange.com/questions/13865/xna-spritebatch-slow-when-rendering-lots-of-sprites
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer