1
votes

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

1
What is -Center in the SpriteBatch.Draw call?craftworkgames
Center is half the size of the sprite. This is because normally the sprites are drawn from the top left position. Using -Center means the center of the image is actually on the position itselfM4st3rM1nd
I can't see any obvious bugs in the code you posted. It may be somewhere else.craftworkgames
there isn't much other code than this. But the weird thing is that sometimes he draws the left and right but misses the middle. But all the tiles in the middle are still there and they are all on visible and all have a texture but they just don't displayM4st3rM1nd

1 Answers

1
votes

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.