diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2015-07-02 17:44:32 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2015-07-08 19:46:01 +0200 |
commit | f9601ae68595e5bd2d7951d8ecf8e11117b2162c (patch) | |
tree | cb6efa3128e8c3d27d104b6c15cb7e3c44ea6854 /src | |
parent | 70d0a26f98da50b348433a772cf02b025a70c5be (diff) | |
download | qtlocation-mapboxgl-f9601ae68595e5bd2d7951d8ecf8e11117b2162c.tar.gz |
add ability to set custom sprites on the Map object
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/geometry/sprite_atlas.cpp | 28 | ||||
-rw-r--r-- | src/mbgl/geometry/sprite_atlas.hpp | 3 | ||||
-rw-r--r-- | src/mbgl/map/map.cpp | 11 | ||||
-rw-r--r-- | src/mbgl/map/map_context.cpp | 10 | ||||
-rw-r--r-- | src/mbgl/map/map_context.hpp | 3 |
5 files changed, 55 insertions, 0 deletions
diff --git a/src/mbgl/geometry/sprite_atlas.cpp b/src/mbgl/geometry/sprite_atlas.cpp index 779afe2884..7b77d7b35f 100644 --- a/src/mbgl/geometry/sprite_atlas.cpp +++ b/src/mbgl/geometry/sprite_atlas.cpp @@ -170,6 +170,34 @@ void SpriteAtlas::upload() { } } +void SpriteAtlas::updateDirty() { + auto dirtySprites = store.getDirty(); + if (dirtySprites.empty()) { + return; + } + + std::lock_guard<std::recursive_mutex> lock(mtx); + + auto imageIterator = images.begin(); + auto spriteIterator = dirtySprites.begin(); + while (imageIterator != images.end() && spriteIterator != dirtySprites.end()) { + if (imageIterator->first.first < spriteIterator->first) { + ++imageIterator; + } else if (spriteIterator->first < imageIterator->first.first) { + ++spriteIterator; + } else { + // The two names match; + Holder& holder = imageIterator->second; + holder.texture = spriteIterator->second; + copy(holder, imageIterator->first.second); + + ++imageIterator; + // Don't advance the spriteIterator because there might be another sprite with the same + // name, but a different wrap value. + } + } +} + void SpriteAtlas::bind(bool linear) { if (!texture) { MBGL_CHECK_ERROR(glGenTextures(1, &texture)); diff --git a/src/mbgl/geometry/sprite_atlas.hpp b/src/mbgl/geometry/sprite_atlas.hpp index c595d2ab76..e9653f6faa 100644 --- a/src/mbgl/geometry/sprite_atlas.hpp +++ b/src/mbgl/geometry/sprite_atlas.hpp @@ -54,6 +54,9 @@ public: // Binds the atlas texture to the GPU, and uploads data if it is out of date. void bind(bool linear = false); + // Updates sprites in the atlas texture that may have changed in the source SpriteStore object. + void updateDirty(); + // Uploads the texture to the GPU to be available when we need it. This is a lazy operation; // the texture is only bound when the data is out of date (=dirty). void upload(); diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index ca8303ec6e..37cd3be335 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -346,6 +346,17 @@ LatLngBounds Map::getBoundsForAnnotations(const std::vector<uint32_t>& annotatio } +#pragma mark - Sprites + +void Map::setSprite(const std::string& name, std::shared_ptr<const SpriteImage> sprite) { + context->invoke(&MapContext::setSprite, name, sprite); +} + +void Map::removeSprite(const std::string& name) { + setSprite(name, nullptr); +} + + #pragma mark - Toggles void Map::setDebug(bool value) { diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 05a046fdb1..a27d7301a5 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -13,6 +13,8 @@ #include <mbgl/storage/resource.hpp> #include <mbgl/storage/response.hpp> +#include <mbgl/geometry/sprite_atlas.hpp> + #include <mbgl/style/style.hpp> #include <mbgl/style/style_bucket.hpp> #include <mbgl/style/style_layer.hpp> @@ -373,6 +375,14 @@ void MapContext::onLowMemory() { view.invalidate(); } +void MapContext::setSprite(const std::string& name, std::shared_ptr<const SpriteImage> sprite) { + if (!style) return; + + style->spriteStore->setSprite(name, sprite); + + style->spriteAtlas->updateDirty(); +} + void MapContext::onTileDataChanged() { assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); asyncUpdate->send(); diff --git a/src/mbgl/map/map_context.hpp b/src/mbgl/map/map_context.hpp index 2c085c1aa8..2e60d8eae0 100644 --- a/src/mbgl/map/map_context.hpp +++ b/src/mbgl/map/map_context.hpp @@ -23,6 +23,7 @@ class Painter; class Sprite; class Worker; class StillImage; +class SpriteImage; struct LatLng; struct LatLngBounds; @@ -64,6 +65,8 @@ public: void cleanup(); + void setSprite(const std::string&, std::shared_ptr<const SpriteImage>); + // Style::Observer implementation. void onTileDataChanged() override; void onResourceLoadingFailed(std::exception_ptr error) override; |