diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-01 17:38:50 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-01 18:05:54 -0800 |
commit | e5181e2668beac377ac4bd93ab2bcb849f33b18a (patch) | |
tree | 5f0dd2d01d374457956438f7e4e13ce7f639e970 /src/mbgl/sprite | |
parent | e326d4972f6320c53be6ac0c696688f4918d4614 (diff) | |
download | qtlocation-mapboxgl-e5181e2668beac377ac4bd93ab2bcb849f33b18a.tar.gz |
[core] Use optional where SpriteAtlas return values might not exist
Fixes #3162
Diffstat (limited to 'src/mbgl/sprite')
-rw-r--r-- | src/mbgl/sprite/sprite_atlas.cpp | 19 | ||||
-rw-r--r-- | src/mbgl/sprite/sprite_atlas.hpp | 13 |
2 files changed, 18 insertions, 14 deletions
diff --git a/src/mbgl/sprite/sprite_atlas.cpp b/src/mbgl/sprite/sprite_atlas.cpp index ae71f18f03..6ebebb9507 100644 --- a/src/mbgl/sprite/sprite_atlas.cpp +++ b/src/mbgl/sprite/sprite_atlas.cpp @@ -47,17 +47,17 @@ Rect<SpriteAtlas::dimension> SpriteAtlas::allocateImage(const size_t pixel_width return rect; } -SpriteAtlasElement SpriteAtlas::getImage(const std::string& name, const bool wrap) { +mapbox::util::optional<SpriteAtlasElement> SpriteAtlas::getImage(const std::string& name, const bool wrap) { std::lock_guard<std::recursive_mutex> lock(mtx); auto rect_it = images.find({ name, wrap }); if (rect_it != images.end()) { - return { rect_it->second.pos, rect_it->second.texture }; + return SpriteAtlasElement { rect_it->second.pos, rect_it->second.texture }; } auto sprite = store.getSprite(name); if (!sprite) { - return { Rect<dimension> { 0, 0, 0, 0 }, nullptr }; + return {}; } Rect<dimension> rect = allocateImage(sprite->width, sprite->height); @@ -65,19 +65,24 @@ SpriteAtlasElement SpriteAtlas::getImage(const std::string& name, const bool wra if (debug::spriteWarnings) { Log::Warning(Event::Sprite, "sprite atlas bitmap overflow"); } - return { Rect<dimension> { 0, 0, 0, 0 }, nullptr }; + return {}; } const Holder& holder = images.emplace(Key{ name, wrap }, Holder{ sprite, rect }).first->second; copy(holder, wrap); - return { rect, sprite }; + return SpriteAtlasElement { rect, sprite }; } -SpriteAtlasPosition SpriteAtlas::getPosition(const std::string& name, bool repeating) { +mapbox::util::optional<SpriteAtlasPosition> SpriteAtlas::getPosition(const std::string& name, bool repeating) { std::lock_guard<std::recursive_mutex> lock(mtx); - auto rect = getImage(name, repeating).pos; + auto img = getImage(name, repeating); + if (!img) { + return {}; + } + + auto rect = (*img).pos; if (repeating) { // When the image is repeating, get the correct position of the image, rather than the // one rounded up to 4 pixels. diff --git a/src/mbgl/sprite/sprite_atlas.hpp b/src/mbgl/sprite/sprite_atlas.hpp index 0e3e8cf225..0d1e52cf81 100644 --- a/src/mbgl/sprite/sprite_atlas.hpp +++ b/src/mbgl/sprite/sprite_atlas.hpp @@ -6,6 +6,8 @@ #include <mbgl/util/noncopyable.hpp> #include <mbgl/util/ptr.hpp> +#include <mapbox/optional.hpp> + #include <string> #include <map> #include <mutex> @@ -41,15 +43,12 @@ public: SpriteAtlas(dimension width, dimension height, float pixelRatio, SpriteStore& store); ~SpriteAtlas(); - // Returns the coordinates of an image that is sourced from the sprite image. - // This getter attempts to read the image from the sprite if it is already loaded. - // In that case, it copies it into the sprite atlas and returns the dimensions. - // Otherwise, it returns a 0/0/0/0 rect. - // This function is used during bucket creation. - SpriteAtlasElement getImage(const std::string& name, const bool wrap); + // If the sprite is loaded, copies the requsted image from it into the atlas and returns + // the resulting icon measurements. If not, returns an empty optional. + mapbox::util::optional<SpriteAtlasElement> getImage(const std::string& name, const bool wrap); // This function is used for getting the position during render time. - SpriteAtlasPosition getPosition(const std::string& name, bool repeating = false); + mapbox::util::optional<SpriteAtlasPosition> getPosition(const std::string& name, bool repeating = false); // Binds the atlas texture to the GPU, and uploads data if it is out of date. void bind(bool linear = false); |