diff options
Diffstat (limited to 'src/map')
-rw-r--r-- | src/map/map.cpp | 34 | ||||
-rw-r--r-- | src/map/sprite.cpp | 11 | ||||
-rw-r--r-- | src/map/tile_data.cpp | 5 |
3 files changed, 37 insertions, 13 deletions
diff --git a/src/map/map.cpp b/src/map/map.cpp index b7a9db505b..ef7b9548bd 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -18,6 +18,8 @@ #include <mbgl/style/style_bucket.hpp> #include <mbgl/util/texturepool.hpp> #include <mbgl/geometry/sprite_atlas.hpp> +#include <mbgl/util/filesource.hpp> +#include <mbgl/platform/log.hpp> #include <algorithm> #include <memory> @@ -29,15 +31,16 @@ using namespace mbgl; Map::Map(View& view) - : view(view), + : loop(std::make_shared<uv::loop>()), + view(view), transform(view), + fileSource(std::make_shared<FileSource>()), style(std::make_shared<Style>()), glyphAtlas(std::make_shared<GlyphAtlas>(1024, 1024)), - glyphStore(std::make_shared<GlyphStore>()), + glyphStore(std::make_shared<GlyphStore>(fileSource)), spriteAtlas(std::make_shared<SpriteAtlas>(512, 512)), texturepool(std::make_shared<Texturepool>()), - painter(*this), - loop(std::make_shared<uv::loop>()) { + painter(*this) { view.initialize(this); @@ -184,10 +187,29 @@ void Map::setup() { painter.setup(); } -void Map::setStyleJSON(std::string newStyleJSON) { +void Map::setStyleURL(const std::string &url) { + fileSource->load(ResourceType::JSON, url, [&](platform::Response *res) { + if (res->code == 200) { + // Calculate the base + const size_t pos = url.rfind('/'); + std::string base = ""; + if (pos != std::string::npos) { + base = url.substr(0, pos + 1); + } + + this->setStyleJSON(res->body, base); + } else { + Log::Error(Event::Setup, "loading style failed: %d (%s)", res->code, res->error_message.c_str()); + } + }, loop); +} + + +void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) { styleJSON.swap(newStyleJSON); sprite.reset(); style->loadJSON((const uint8_t *)styleJSON.c_str()); + fileSource->setBase(base); glyphStore->setURL(style->glyph_url); update(); } @@ -208,7 +230,7 @@ std::shared_ptr<Sprite> Map::getSprite() { const float pixelRatio = state.getPixelRatio(); const std::string &sprite_url = style->getSpriteURL(); if (!sprite || sprite->pixelRatio != pixelRatio) { - sprite = Sprite::Create(sprite_url, pixelRatio); + sprite = Sprite::Create(sprite_url, pixelRatio, fileSource); } return sprite; diff --git a/src/map/sprite.cpp b/src/map/sprite.cpp index 68453de52b..f396fec07d 100644 --- a/src/map/sprite.cpp +++ b/src/map/sprite.cpp @@ -5,6 +5,7 @@ #include <string> #include <mbgl/platform/platform.hpp> +#include <mbgl/util/filesource.hpp> #include <mbgl/util/uv.hpp> #include <mbgl/util/std.hpp> @@ -20,9 +21,9 @@ SpritePosition::SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t pixelRatio(pixelRatio) { } -std::shared_ptr<Sprite> Sprite::Create(const std::string& base_url, float pixelRatio) { +std::shared_ptr<Sprite> Sprite::Create(const std::string& base_url, float pixelRatio, const std::shared_ptr<FileSource> &fileSource) { std::shared_ptr<Sprite> sprite(std::make_shared<Sprite>(Key(), base_url, pixelRatio)); - sprite->load(); + sprite->load(fileSource); return sprite; } @@ -49,7 +50,7 @@ Sprite::operator bool() const { // Note: This is a separate function that must be called exactly once after creation // The reason this isn't part of the constructor is that calling shared_from_this() in // the constructor fails. -void Sprite::load() { +void Sprite::load(const std::shared_ptr<FileSource> &fileSource) { if (!valid) { // Treat a non-existent sprite as a successfully loaded empty sprite. loadedImage = true; @@ -60,7 +61,7 @@ void Sprite::load() { std::shared_ptr<Sprite> sprite = shared_from_this(); - platform::request_http(jsonURL, [sprite](platform::Response *res) { + fileSource->load(ResourceType::JSON, jsonURL, [sprite](platform::Response *res) { if (res->code == 200) { sprite->body.swap(res->body); sprite->parseJSON(); @@ -73,7 +74,7 @@ void Sprite::load() { } }); - platform::request_http(spriteURL, [sprite](platform::Response *res) { + fileSource->load(ResourceType::Image, spriteURL, [sprite](platform::Response *res) { if (res->code == 200) { sprite->image.swap(res->body); sprite->parseImage(); diff --git a/src/map/tile_data.cpp b/src/map/tile_data.cpp index 57ad6944bf..a94eb00fbf 100644 --- a/src/map/tile_data.cpp +++ b/src/map/tile_data.cpp @@ -4,6 +4,7 @@ #include <mbgl/util/token.hpp> #include <mbgl/util/string.hpp> +#include <mbgl/util/filesource.hpp> using namespace mbgl; @@ -38,7 +39,7 @@ void TileData::request() { // Note: Somehow this feels slower than the change to request_http() std::weak_ptr<TileData> weak_tile = shared_from_this(); - req = platform::request_http(url, [weak_tile](platform::Response *res) { + map.getFileSource()->load(ResourceType::Tile, url, [weak_tile](platform::Response *res) { std::shared_ptr<TileData> tile = weak_tile.lock(); if (!tile || tile->state == State::obsolete) { // noop. Tile is obsolete and we're now just waiting for the refcount @@ -55,7 +56,7 @@ void TileData::request() { fprintf(stderr, "[%s] tile loading failed: %d, %s\n", tile->url.c_str(), res->code, res->error_message.c_str()); #endif } - }, map.getLoop()); + }); } void TileData::cancel() { |