use SpriteStore from the SpriteAtlas

This commit is contained in:
Konstantin Käfer
2015-07-01 18:13:16 +02:00
parent d2ee014c6e
commit afaf74e41a
14 changed files with 91 additions and 227 deletions

View File

@@ -17,20 +17,8 @@
using namespace mbgl;
SpritePosition::SpritePosition(uint16_t x_, uint16_t y_, uint16_t width_, uint16_t height_, float pixelRatio_, bool sdf_)
: x(x_),
y(y_),
width(width_),
height(height_),
pixelRatio(pixelRatio_),
sdf(sdf_) {
}
Sprite::Sprite(const std::string& baseUrl, float pixelRatio_)
: pixelRatio(pixelRatio_ > 1 ? 2 : 1),
raster(),
loadedImage(false),
loadedJSON(false) {
: pixelRatio(pixelRatio_ > 1 ? 2 : 1) {
if (baseUrl.empty()) {
// Treat a non-existent sprite as a successfully loaded empty sprite.
loadedImage = true;
@@ -45,8 +33,8 @@ Sprite::Sprite(const std::string& baseUrl, float pixelRatio_)
jsonRequest = fs->request({ Resource::Kind::JSON, jsonURL }, util::RunLoop::getLoop(), [this, jsonURL](const Response &res) {
jsonRequest = nullptr;
if (res.status == Response::Successful) {
body = res.data;
parseJSON(jsonURL);
json = res.data;
loadedJSON = true;
} else {
std::stringstream message;
message << "Failed to load [" << jsonURL << "]: " << res.message;
@@ -60,7 +48,7 @@ Sprite::Sprite(const std::string& baseUrl, float pixelRatio_)
spriteRequest = nullptr;
if (res.status == Response::Successful) {
image = res.data;
parseImage(spriteURL);
loadedImage = true;
} else {
std::stringstream message;
message << "Failed to load [" << spriteURL << "]: " << res.message;
@@ -100,69 +88,6 @@ bool Sprite::isLoaded() const {
return loadedImage && loadedJSON;
}
bool Sprite::hasPixelRatio(float ratio) const {
return pixelRatio == (ratio > 1 ? 2 : 1);
}
void Sprite::parseImage(const std::string& spriteURL) {
raster = std::make_unique<util::Image>(image);
if (!*raster) {
raster.reset();
std::stringstream message;
message << "Failed to parse [" << spriteURL << "]";
emitSpriteLoadingFailed(message.str());
return;
}
image.clear();
loadedImage = true;
}
void Sprite::parseJSON(const std::string& jsonURL) {
rapidjson::Document d;
d.Parse<0>(body.c_str());
body.clear();
if (d.HasParseError()) {
std::stringstream message;
message << "Failed to parse [" << jsonURL << "]: " << d.GetErrorOffset() << " - " << d.GetParseError();
emitSpriteLoadingFailed(message.str());
} else if (d.IsObject()) {
for (rapidjson::Value::ConstMemberIterator itr = d.MemberBegin(); itr != d.MemberEnd(); ++itr) {
const std::string name = { itr->name.GetString(), itr->name.GetStringLength() };
const rapidjson::Value& value = itr->value;
if (value.IsObject()) {
uint16_t x = 0;
uint16_t y = 0;
uint16_t width = 0;
uint16_t height = 0;
float spritePixelRatio = 1.0f;
bool sdf = false;
if (value.HasMember("x")) x = value["x"].GetInt();
if (value.HasMember("y")) y = value["y"].GetInt();
if (value.HasMember("width")) width = value["width"].GetInt();
if (value.HasMember("height")) height = value["height"].GetInt();
if (value.HasMember("pixelRatio")) spritePixelRatio = value["pixelRatio"].GetInt();
if (value.HasMember("sdf")) sdf = value["sdf"].GetBool();
pos.emplace(name, SpritePosition { x, y, width, height, spritePixelRatio, sdf });
}
}
loadedJSON = true;
} else {
std::stringstream message;
message << "Failed to parse [" << jsonURL << "]: Root is not an object";
emitSpriteLoadingFailed(message.str());
}
}
const SpritePosition &Sprite::getSpritePosition(const std::string& name) const {
if (!isLoaded()) return empty;
auto it = pos.find(name);
return it == pos.end() ? empty : it->second;
}
void Sprite::setObserver(Observer* observer_) {
observer = observer_;
}