# MapDrawableBatch

### Introduction

The MapDrawableBatch class represents a single layer in a .tmx file. It implements the [IDrawableBatch](/flatredball/api/flatredball/graphics/drawablebatch.md) class to perform custom rendering. It internally creates a single vertex buffer which is drawn all at once with no render state changes for maximum performance. Usually .tmx files are loaded into a [LayeredTileMap](/flatredball/tiled-plugin/flatredball-tilegraphics-layeredtilemap.md), which contains one or more MapDrawableBatch instances.

### Only painted tiles create vertices

When the MapDrawableBatch is created it will only create vertices for tiles which exist. Therefore tiles which are not painted or which have been erased in Tiled will not create vertices.

### MapDrawableBatch Color Values

MapDrawableBatch supports a Modulate color operation through its Red, Green, and Blue values. By default these values are all set to 1.0f, but they can be modified at runtime to tint or darken the map.

### Creating a MapDrawableBatch in Code

MapDrawableBatch instances are typically created by adding a TMX file to your project. By default FlatRedBall top down and platformer projects contain TMX files for each level. MapDrawableBatch instances can also be created in code. The following code shows how to create a new MapDrawableBatch which is filled with a pattern from a texture called FRBeefcakeSpritesheet The following code assumes that an existing Map exists (although this is not necessary), that the code has access to a Texture called FRBeefcakeSpritesheet, and that this code exists in the CustomInitialize of a Screen such as Level1.

```csharp
MapDrawableBatch CustomLayer;

void CustomInitialize()
{
    var numberOfTilesWide = 32;
    var numberOfTilesHigh = 32;
    var totalTiles = numberOfTilesWide * numberOfTilesHigh;
    CustomLayer = new MapDrawableBatch(totalTiles, 16, 16, FRBeefcakeSpritesheet);

    // Adding the CustomLayer to managers enables its rendering...
    CustomLayer.AddToManagers();

    // Strictly speaking, it is not necessary to add CustomLayer to the Map's MapLayers.
    // It is recommended so that any MapDrawableBatch added in code can be accessed through
    // the Map, just like layers created through TMX. In other words, it keeps things the same
    // and avoids gotchas.
    Map.MapLayers.Add(CustomLayer);

    // Attaching the CustomLayer to the Map enables moving the entire map as a single unit:
    CustomLayer.AttachTo(Map);
    CustomLayer.RelativeZ = 1;

    CustomLayer.SortAxis = SortAxis.X;
    // add tiles for the entire map:
    for (int x = 0; x < numberOfTilesWide; x++)
    {
        for(int y = 0; y < numberOfTilesHigh; y++)
        {

            CustomLayer.AddTile(new Vector3(x * 16, (-y-1) * 16, 0), new Vector2(16, 16),
                textureTopLeftX: 16,
                textureTopLeftY: 176,
                textureBottomRightX: 32,
                textureBottomRightY: 192);
        }
    }
}
```

![](/files/SpIAYEbfh2H6jhxF5Wfn)

#### Creation Considerations

The code above includes some subtle considerations:

1. The CustomLayer MapDrawableBatch uses X axis sorting. Therefore, all tiles must be added in order of X, largest to small. Breaking this order will result in incorrect rendering and other unpredictable behavior.
2. The entire tile set is filled. This enables painting later without having to add tiles. If tiles are added on paint, this can make sorting far more difficult.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flatredball.com/flatredball/tiled-plugin/flatredball-tilegraphics-mapdrawablebatch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
