Skip to content

Commit

Permalink
Handle tileset related errors more gracefully
Browse files Browse the repository at this point in the history
Ref #26
  • Loading branch information
soulweaver91 committed Dec 26, 2016
1 parent 89705d8 commit 1a5b558
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/gamestate/TileMap.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "TileMap.h"

#include <cmath>
#include <exception>
#include "EventMap.h"
#include "GameView.h"
#include "LevelManager.h"
Expand All @@ -18,7 +19,7 @@ TileMap::TileMap(LevelManager* root, const QString& tilesetFilename,
// Reserve textures for tileset and its mask counterpart
levelTileset = std::make_unique<Tileset>(tilesetFilename, maskFilename);
if (!levelTileset->getIsValid()) {
return;
throw std::runtime_error(QString("Unknown error loading the tileset!").toStdString());
}

// initialize the trigger store
Expand Down
15 changes: 9 additions & 6 deletions src/graphics/Tileset.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#include "Tileset.h"
#include "../struct/Layers.h"
#include <exception>

Tileset::Tileset(const QString& tilesFilename, const QString& maskFilename) : isValid(false) {
texture = std::make_shared<sf::Texture>();

texture->loadFromFile(tilesFilename.toUtf8().data());

if (texture->getSize().x == 0) {
// Loading the tileset failed for some reason, texture is empty.
throw std::runtime_error(QString("Tileset texture could not be loaded!").toStdString());
}

sf::Image maskfile;
maskfile.loadFromFile(maskFilename.toUtf8().data());
if (texture->getSize() != maskfile.getSize()) {
// mask doesn't match the tiles in size
return;
}
if (texture->getSize().x == 0) {
// TODO: loading the tileset failed for some reason, texture is empty
return;
// Mask could not be loaded or it didn't match the texture.
// As a fallback, get mask from texture image itself.
maskfile = texture->copyToImage();
}

uint width = texture->getSize().x / 32;
Expand Down

0 comments on commit 1a5b558

Please sign in to comment.