From 62ac43a85bac29bed4a1c93bab592e6bdaf2838b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Mon, 6 Jul 2015 11:16:44 +0200 Subject: [PATCH] add ability to add random custom markers to test app --- .../mbgl/annotation/sprite_image.hpp | 0 include/mbgl/platform/default/glfw_view.hpp | 4 ++ platform/default/glfw_view.cpp | 47 +++++++++++++++++++ 3 files changed, 51 insertions(+) rename {src => include}/mbgl/annotation/sprite_image.hpp (100%) diff --git a/src/mbgl/annotation/sprite_image.hpp b/include/mbgl/annotation/sprite_image.hpp similarity index 100% rename from src/mbgl/annotation/sprite_image.hpp rename to 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 cb40db124..f65565c03 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 + 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 spriteIDs; private: bool fullscreen = false; diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 9ccf194f6..fb3bb1097 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -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 +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(const_cast(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(width, height, pixelRatio, std::move(pixels)); +} + +void GLFWView::addRandomCustomPointAnnotations(int count) { + std::vector 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 points;