add ability to set custom sprites on the Map object

This commit is contained in:
Konstantin Käfer
2015-07-02 17:44:32 +02:00
parent 70d0a26f98
commit f9601ae685
6 changed files with 60 additions and 0 deletions

View File

@@ -21,6 +21,7 @@ class View;
class MapData;
class MapContext;
class StillImage;
class SpriteImage;
class Transform;
class PointAnnotation;
class ShapeAnnotation;
@@ -141,6 +142,10 @@ public:
AnnotationIDs getAnnotationsInBounds(const LatLngBounds&, const AnnotationType& = AnnotationType::Any);
LatLngBounds getBoundsForAnnotations(const AnnotationIDs&);
// Sprites
void setSprite(const std::string&, std::shared_ptr<const SpriteImage>);
void removeSprite(const std::string&);
// Memory
void setSourceTileCacheSize(size_t);
void onLowMemory();

View File

@@ -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));

View File

@@ -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();

View File

@@ -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) {

View File

@@ -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();

View File

@@ -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;