Question

I'm currently working on a top down game using MonoGame that uses tiles to indicate whether a position is walkable or not. Tiles have the size of 32x32 (which the images also have)

A grid of 200 x 200 is being made filled with wall tiles (and a random generator is supposed to create a path and rooms) but when I draw all the tiles on the screen a lot of tiles go missing. Below is an image where after position (x81 y183) the tiles are simply not drawn?

http://puu.sh/3JOUO.png

The code used to fill the array puts a wall tile on the grid and the position of the tile is it's array position multiplied by the tile size (32x32) the parent is used for the camera position

    public override void Fill(IResourceContainer resourceContainer)
    {
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
            {
                objectGrid[i, j] = new Wall(resourceContainer);
                objectGrid[i, j].Parent = this;
                objectGrid[i, j].Position = new Vector2(i * TileWidth, j * TileHeight);
            }
    }

When drawing I just loop through all tiles and draw them accordingly. This is what happends in the Game.Draw function

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Yellow);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        map.Draw(gameTime, spriteBatch);

        spriteBatch.End();
        base.Draw(gameTime);
    }

The map.draw function calls this function which basically draws each tile. I tried putting a counter on how much times the draw call for each tile was hit and every update the draw function is called 40000 times which is the amount of tiles I use. So it draws them all but I still don't see them all on the screen

    public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        for (int i = 0; i < width; i++)
            for (int j = 0; j < height; j++)
            {
                if (objectGrid[i, j] != null)
                {
                    objectGrid[i, j].Draw(gameTime, spriteBatch);
                }
            }
    }

This is the code for drawing a tile. Where the current image is 0 at all times and the GlobalPosition is the position of a tile minus the camera position.

    public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        if (visible)
            spriteBatch.Draw(textures[currentImage], GlobalPosition, null, color, 0f, -Center, 1f, SpriteEffects.None, 0f);
    }

My apologies for the wall of code. It all looks very simple to me yet I can't seem to find out why it is not drawing all of the tiles. For the not drawn tiles visible is still true and currentImage is 0 which it should be

Was it helpful?

Solution

The monogame spritebatch has still some bugs and errors when drawing a large number of 16-bit images. In my case around 200.000 this is not something you can easily solve. If you encounter the same problem make sure that every image you draw is on the screen and you will probably have no problems from this anymore.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top