Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7712511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:39:01+00:00 2026-06-01T01:39:01+00:00

This method that draws my tiles seems to be quite slow, Im not sure

  • 0

This method that draws my tiles seems to be quite slow, Im not sure exactly whats wrong, it belive my culling method isnt working and is drawing stuff offscreen, but im not completeley sure. Here it is:

        // Calculate the visible range of tiles.
        int left = (int)Math.Floor(cameraPosition.X / 16);
        int right = left + spriteBatch.GraphicsDevice.Viewport.Width / 16;
        right = Math.Min(right, Width) + 1; // Width -1 originally - didn't look good as tiles drawn on screen
        if (right > tiles.GetUpperBound(0))
            right = tiles.GetUpperBound(0) + 1; // adding 1 to get the last right tile drawn

        int top = (int)Math.Floor(cameraPosition.Y / 16);
        int bottom = left + spriteBatch.GraphicsDevice.Viewport.Height/ 16;
        bottom = Math.Min(bottom, Height) + 1; // Height -1 originally - didn't look good as tiles drawn on screen
        if (bottom > tiles.GetUpperBound(1))
            bottom = tiles.GetUpperBound(1) + 1; // adding 1 to get the last bottom tile drawn

        // For each tile position
        for (int y = top; y < bottom; ++y)
        {
            for (int x = left; x < right; ++x)
            {
                // If there is a visible tile in that position, draw it
                if (tiles[x, y].BlockType.Name != "Blank")
                {
                    Texture2D texture = tileContent["DirtBlock_" + getTileSetType(tiles,x,y)];
                    spriteBatch.Draw(texture, new Vector2(x * 16, y * 16), Color.White);
                    if (isMinimap)
                    spriteBatch.Draw(pixel, new Vector2(30+x, 30+y), Color.White);
                }

            }
        }

GetTileSetTypes is a function to get what tiles are around it, for different textures, like DirtBlock_North, DirtBlock_Center, etc.

Tile content is just a class with my block textures.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:39:03+00:00Added an answer on June 1, 2026 at 1:39 am

    Try changing SpriteBatch.Begin to defered and combining all of the tiles onto one texture.

    See this GameDev question for info about why deferred is most likely the fastest option for you.

    Also realize that every time you draw a new texture you have to take the old one out of the GPU and put the new one in. This process is called texture swapping and usually isn’t an issue but you are swapping textures twice per tile which is likely to impact performance noticeably.

    This can be fixed by combining multiple sprites onto one texture and using the source rectangle argument. This allows you to draw multiple sprites without a texture swap. There are a few OSS libraries for this. Sprite Sheet Packer is my personal favorite.

    Unfortunantly without the project and a profiler I’m just guessing; however, these are the two biggest gotchas for rendering tilemaps I know of. I can’t really see anything wrong from here. Below is the code I use to draw my tile maps and as you see its very similar to yours.

    If all else fails I would suggest using a profiler to figure out which bits are running slowly.

            //Init the holder
            _holder = new Rectangle(0, 0, TileWidth, TileHeight);
    
            //Figure out the min and max tile indices to draw
            var minX = Math.Max((int)Math.Floor((float)worldArea.Left / TileWidth), 0);
            var maxX = Math.Min((int)Math.Ceiling((float)worldArea.Right / TileWidth), Width);
    
            var minY = Math.Max((int)Math.Floor((float)worldArea.Top / TileHeight), 0);
            var maxY = Math.Min((int)Math.Ceiling((float)worldArea.Bottom / TileHeight), Height);
    
            for (var y = minY; y < maxY; y++) {
                for (var x = minX; x < maxX; x++) {
    
                    _holder.X = x * TileWidth;
                    _holder.Y = y * TileHeight;
    
                    var t = tileLayer[y * Width + x];
                    spriteBatch.Draw(
                        t.Texture,
                         _holder, 
                        t.SourceRectangle,
                        Color.White, 
                        0,
                        Vector2.Zero,
                        t.SpriteEffects, 
                        0);
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that draws tiles onto the screen, and inside this method
I have this method that gets called when i press a button and removes
I have this method that changes an larger image source on click. I need
All, I have a method that returns a List. This method is used to
I'm using Microsoft Moles to mock a method. This method call another method that
I notice that this method is provided in UIViewController .m files, but is commented
I'm trying to use that method this way: public void Method() { ThreadPool.QueueUserWorkItem(() =>
I have a method with this signature, and another method that will be the
Is there builtin method that would do this or do I always have to
I have a method that looks like this: public static String escape(String text) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.