Permalink
Browse files

Fixed crash when opening a map without tilesets

In particular this crash would always happen when creating a new map.
The crash was caused by TerrainBrush::mapDocumentChanged, which did not
handle the case where the new map did not have any tilesets correctly.
  • Loading branch information...
1 parent 79a49da commit 1afabff912bd959e866f084e9c65d89a9cbcf8db @bjorn committed Jun 4, 2012
Showing with 14 additions and 11 deletions.
  1. +14 −11 src/tiled/terrainbrush.cpp
View
@@ -195,24 +195,27 @@ void TerrainBrush::languageChanged()
setShortcut(QKeySequence(tr("T")));
}
+static Terrain *firstTerrain(MapDocument *mapDocument)
+{
+ if (!mapDocument)
+ return 0;
+
+ foreach (Tileset *tileset, mapDocument->map()->tilesets())
+ if (tileset->terrainCount() > 0)
+ return tileset->terrain(0);
+
+ return 0;
+}
+
void TerrainBrush::mapDocumentChanged(MapDocument *oldDocument,
- MapDocument *newDocument)
+ MapDocument *newDocument)
{
AbstractTileTool::mapDocumentChanged(oldDocument, newDocument);
// Reset the brush, since it probably became invalid
brushItem()->setTileRegion(QRegion());
- // if the new document has any terrains defined, choose the first rather than the 'none' terrain
- if (newDocument) {
- Tileset *tileset = newDocument->map()->tilesets().at(0);
- if (tileset && tileset->terrainCount() > 0)
- setTerrain(tileset->terrain(0));
- else
- setTerrain(NULL);
- } else {
- mTerrain = NULL;
- }
+ setTerrain(firstTerrain(newDocument));
}
void TerrainBrush::setTerrain(const Terrain *terrain)

0 comments on commit 1afabff

Please sign in to comment.