Skip to content

Commit

Permalink
Fixed possible crash when missing tileset images
Browse files Browse the repository at this point in the history
This could happen when a map refers to a tileset that uses tile
animations, but the tileset image could not be loaded.

Closes #1393
  • Loading branch information
bjorn committed Nov 14, 2016
1 parent 4b5903c commit 325b1d3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
14 changes: 9 additions & 5 deletions src/libtiled/maprenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,18 @@ CellRenderer::CellRenderer(QPainter *painter)
*/
void CellRenderer::render(const Cell &cell, const QPointF &pos, const QSizeF &cellSize, Origin origin)
{
if (mTile != cell.tile)
const Tile *tile = cell.tile->currentFrameTile();
if (!tile)
return;

if (mTile != tile)
flush();

const QPixmap &image = cell.tile->currentFrameImage();
const QPixmap &image = tile->image();
const QSizeF size = image.size();
const QSizeF objectSize = (cellSize == QSizeF(0,0)) ? size : cellSize;
const QSizeF scale(objectSize.width() / size.width(), objectSize.height() / size.height());
const QPoint offset = cell.tile->offset();
const QPoint offset = tile->offset();
const QPointF sizeHalf = QPointF(objectSize.width() / 2, objectSize.height() / 2);

QPainter::PixmapFragment fragment;
Expand Down Expand Up @@ -152,7 +156,7 @@ void CellRenderer::render(const Cell &cell, const QPointF &pos, const QSizeF &ce
fragment.scaleY = scale.height() * (flippedVertically ? -1 : 1);

if (mIsOpenGL || (fragment.scaleX > 0 && fragment.scaleY > 0)) {
mTile = cell.tile;
mTile = tile;
mFragments.append(fragment);
return;
}
Expand Down Expand Up @@ -187,7 +191,7 @@ void CellRenderer::flush()

mPainter->drawPixmapFragments(mFragments.constData(),
mFragments.size(),
mTile->currentFrameImage());
mTile->image());

mTile = nullptr;
mFragments.resize(0);
Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/maprenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class CellRenderer

private:
QPainter * const mPainter;
Tile *mTile;
const Tile *mTile;
QVector<QPainter::PixmapFragment> mFragments;
const bool mIsOpenGL;
};
Expand Down
13 changes: 7 additions & 6 deletions src/libtiled/tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,18 @@ QSharedPointer<Tileset> Tile::sharedTileset() const
}

/**
* Returns the image for rendering this tile, taking into account tile
* animations.
* Returns the tile to render when taking into account tile animations.
*
* \warning May return null when the tileset is invalid or the image could
* not be loaded.
*/
const QPixmap &Tile::currentFrameImage() const
const Tile *Tile::currentFrameTile() const
{
if (isAnimated()) {
const Frame &frame = mFrames.at(mCurrentFrameIndex);
return mTileset->findTile(frame.tileId)->image();
} else {
return mImage;
return mTileset->findTile(frame.tileId);
}
return this;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/libtiled/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class TILEDSHARED_EXPORT Tile : public Object
const QPixmap &image() const;
void setImage(const QPixmap &image);

const QPixmap &currentFrameImage() const;
const Tile *currentFrameTile() const;

const QString &imageSource() const;
void setImageSource(const QString &imageSource);
Expand Down

0 comments on commit 325b1d3

Please sign in to comment.