diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2015-07-06 11:16:44 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2015-07-08 19:46:02 +0200 |
commit | 62ac43a85bac29bed4a1c93bab592e6bdaf2838b (patch) | |
tree | fb936aa4d124674a12a9aa4755e469db5fb811e7 | |
parent | b2a3314192a8a1dc06e0677e1430219dd8655cf0 (diff) | |
download | qtlocation-mapboxgl-62ac43a85bac29bed4a1c93bab592e6bdaf2838b.tar.gz |
add ability to add random custom markers to test app
-rw-r--r-- | include/mbgl/annotation/sprite_image.hpp (renamed from src/mbgl/annotation/sprite_image.hpp) | 0 | ||||
-rw-r--r-- | include/mbgl/platform/default/glfw_view.hpp | 4 | ||||
-rw-r--r-- | platform/default/glfw_view.cpp | 47 |
3 files changed, 51 insertions, 0 deletions
diff --git a/src/mbgl/annotation/sprite_image.hpp b/include/mbgl/annotation/sprite_image.hpp index 7d8ea0501c..7d8ea0501c 100644 --- a/src/mbgl/annotation/sprite_image.hpp +++ b/include/mbgl/annotation/sprite_image.hpp diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp index cb40db1248..f65565c030 100644 --- a/include/mbgl/platform/default/glfw_view.hpp +++ b/include/mbgl/platform/default/glfw_view.hpp @@ -46,14 +46,18 @@ public: private: mbgl::LatLng makeRandomPoint() const; + static std::shared_ptr<const mbgl::SpriteImage> + makeSpriteImage(int width, int height, float pixelRatio); void addRandomPointAnnotations(int count); void addRandomShapeAnnotations(int count); + void addRandomCustomPointAnnotations(int count); void clearAnnotations(); void popAnnotation(); mbgl::AnnotationIDs annotationIDs; + std::vector<std::string> spriteIDs; private: bool fullscreen = false; diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 9ccf194f62..fb3bb1097a 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -1,5 +1,6 @@ #include <mbgl/annotation/point_annotation.hpp> #include <mbgl/annotation/shape_annotation.hpp> +#include <mbgl/annotation/sprite_image.hpp> #include <mbgl/platform/default/glfw_view.hpp> #include <mbgl/platform/gl.hpp> #include <mbgl/platform/log.hpp> @@ -120,6 +121,9 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, case GLFW_KEY_Q: view->clearAnnotations(); break; + case GLFW_KEY_P: { + view->addRandomCustomPointAnnotations(1); + } break; } } @@ -150,6 +154,49 @@ mbgl::LatLng GLFWView::makeRandomPoint() const { return { lat, lon }; } +std::shared_ptr<const mbgl::SpriteImage> +GLFWView::makeSpriteImage(int width, int height, float pixelRatio) { + const int r = 255 * (double(std::rand()) / RAND_MAX); + const int g = 255 * (double(std::rand()) / RAND_MAX); + const int b = 255 * (double(std::rand()) / RAND_MAX); + + const int w = std::ceil(pixelRatio * width); + const int h = std::ceil(pixelRatio * height); + + std::string pixels(w * h * 4, '\x00'); + auto data = reinterpret_cast<uint32_t*>(const_cast<char*>(pixels.data())); + const int dist = (w / 2) * (w / 2); + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + const int dx = x - w / 2; + const int dy = y - h / 2; + const int diff = dist - (dx * dx + dy * dy); + if (diff > 0) { + const int a = std::min(0xFF, diff) * 0xFF / dist; + // Premultiply the rgb values with alpha + data[w * y + x] = + (a << 24) | ((a * r / 0xFF) << 16) | ((a * g / 0xFF) << 8) | (a * b / 0xFF); + } + } + } + + return std::make_shared<mbgl::SpriteImage>(width, height, pixelRatio, std::move(pixels)); +} + +void GLFWView::addRandomCustomPointAnnotations(int count) { + std::vector<mbgl::PointAnnotation> points; + + for (int i = 0; i < count; i++) { + static int spriteID = 1; + const auto name = std::string{ "marker-" } + std::to_string(spriteID++); + map->setSprite(name, makeSpriteImage(22, 22, 1)); + spriteIDs.push_back(name); + points.emplace_back(makeRandomPoint(), name); + } + + auto newIDs = map->addPointAnnotations(points); + annotationIDs.insert(annotationIDs.end(), newIDs.begin(), newIDs.end()); +} void GLFWView::addRandomPointAnnotations(int count) { std::vector<mbgl::PointAnnotation> points; |