diff --git a/include/llmr/map/sprite.hpp b/include/llmr/map/sprite.hpp index 07f5903e5..079f0decc 100644 --- a/include/llmr/map/sprite.hpp +++ b/include/llmr/map/sprite.hpp @@ -52,6 +52,7 @@ private: private: Map ↦ + std::string url; std::string body; std::string image; std::atomic loaded; diff --git a/include/llmr/platform/event.hpp b/include/llmr/platform/event.hpp index 133cfc3c5..81b3e1d02 100644 --- a/include/llmr/platform/event.hpp +++ b/include/llmr/platform/event.hpp @@ -32,6 +32,7 @@ enum class Event : uint8_t { ParseTile, Render, HttpRequest, + Sprite, }; LLMR_DEFINE_ENUM_CLASS(EventClass, Event, { @@ -42,6 +43,7 @@ LLMR_DEFINE_ENUM_CLASS(EventClass, Event, { { Event::ParseTile, "ParseTile" }, { Event::Render, "Render" }, { Event::HttpRequest, "HttpRequest" }, + { Event::Sprite, "Sprite" }, { Event(-1), "Unknown" }, }); diff --git a/src/geometry/sprite_atlas.cpp b/src/geometry/sprite_atlas.cpp index 8a055e5a6..2dfb792f7 100644 --- a/src/geometry/sprite_atlas.cpp +++ b/src/geometry/sprite_atlas.cpp @@ -128,9 +128,7 @@ Rect SpriteAtlas::allocateImage(size_t width, size_t hei Rect SpriteAtlas::getIcon(const int size, const std::string &name) { std::lock_guard lock(mtx); - const std::string id = name + "-" + std::to_string(size); - - auto rect_it = images.find(id); + auto rect_it = images.find(name); if (rect_it != images.end()) { return rect_it->second; } @@ -143,7 +141,7 @@ Rect SpriteAtlas::getIcon(const int size, const std::str return rect; } - images.emplace(id, rect); + images.emplace(name, rect); allocate(); @@ -158,7 +156,7 @@ Rect SpriteAtlas::getIcon(const int size, const std::str 1.0f / size / pixelRatio ); - uninitialized.emplace(id); + uninitialized.emplace(name); dirty = true; @@ -251,11 +249,13 @@ void SpriteAtlas::update(const Sprite &sprite) { } void SpriteAtlas::bind(bool linear) { + bool first = false; if (!texture) { glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + first = true; } else { glBindTexture(GL_TEXTURE_2D, texture); } @@ -270,20 +270,34 @@ void SpriteAtlas::bind(bool linear) { if (dirty) { std::lock_guard lock(mtx); allocate(); - glTexImage2D( - GL_TEXTURE_2D, // GLenum target - 0, // GLint level - GL_RGBA, // GLint internalformat - width * pixelRatio, // GLsizei width - height * pixelRatio, // GLsizei height - 0, // GLint border - GL_RGBA, // GLenum format - GL_UNSIGNED_BYTE, // GLenum type - data // const GLvoid * data - ); + + if (first) { + glTexImage2D( + GL_TEXTURE_2D, // GLenum target + 0, // GLint level + GL_RGBA, // GLint internalformat + width * pixelRatio, // GLsizei width + height * pixelRatio, // GLsizei height + 0, // GLint border + GL_RGBA, // GLenum format + GL_UNSIGNED_BYTE, // GLenum type + data // const GLvoid * data + ); + } else { + glTexSubImage2D(GL_TEXTURE_2D, // GLenum target + 0, // GLint level + 0, // GLint xoffset + 0, // GLint yoffset + width * pixelRatio, // GLsizei width + height * pixelRatio, // GLsizei height + GL_RGBA, // GLenum format + GL_UNSIGNED_BYTE, // GLenum type + data // const GLvoid *pixels + ); + } #if defined(DEBUG) - // platform::show_color_debug_image("Sprite Atlas", data, width, height, width * pixelRatio, height * pixelRatio); + // platform::show_color_debug_image("Sprite Atlas", reinterpret_cast(data), width, height, width * pixelRatio, height * pixelRatio); #endif dirty = false; } @@ -291,6 +305,10 @@ void SpriteAtlas::bind(bool linear) { SpriteAtlas::~SpriteAtlas() { std::lock_guard lock(mtx); + + glDeleteTextures(1, &texture); + texture = 0; + if (data) { free(data); data = nullptr; diff --git a/src/map/map.cpp b/src/map/map.cpp index 68db66370..4d374f1d5 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -97,6 +97,7 @@ void Map::run() { // If the map rendering wasn't started asynchronously, we perform one render // *after* all events have been processed. if (!async) { + prepare(); render(); } } diff --git a/src/map/sprite.cpp b/src/map/sprite.cpp index 4b23b120c..823d64665 100644 --- a/src/map/sprite.cpp +++ b/src/map/sprite.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -27,6 +28,8 @@ Sprite::Sprite(Map &map, float pixelRatio) } void Sprite::load(const std::string& base_url) { + loaded = false; + url = base_url; std::shared_ptr sprite = shared_from_this(); std::string suffix = (pixelRatio > 1 ? "@2x" : ""); @@ -53,12 +56,12 @@ void Sprite::load(const std::string& base_url) { } void Sprite::complete(std::shared_ptr &sprite) { - if (sprite->raster && sprite->pos.size()) { + const bool raster = bool(sprite->raster); + const bool json = bool(sprite->pos.size()); + if (raster && json && !sprite->loaded) { sprite->loaded = true; sprite->map.update(); -#if defined(DEBUG) - fprintf(stderr, "sprite loaded\n"); -#endif + Log::Info(Event::Sprite, "loaded %s", sprite->url.c_str()); } } diff --git a/src/renderer/painter_icon.cpp b/src/renderer/painter_icon.cpp index 499b124fb..29e8af396 100644 --- a/src/renderer/painter_icon.cpp +++ b/src/renderer/painter_icon.cpp @@ -23,7 +23,7 @@ void Painter::renderIcon(IconBucket& bucket, std::shared_ptr layer_d useProgram(iconShader->program); iconShader->setMatrix(matrix); // TODO: update -// iconShader->setColor(color); + iconShader->setColor({{ 1, 1, 1, 1 }}); iconShader->setImage(0); iconShader->setRatio(map.getState().getPixelRatio()); iconShader->setDimension({{ diff --git a/src/style/filter_expression.cpp b/src/style/filter_expression.cpp index 58483acca..cba7e21f1 100644 --- a/src/style/filter_expression.cpp +++ b/src/style/filter_expression.cpp @@ -61,11 +61,11 @@ bool FilterComparison::Instance::compare(const std::forward_list &propert switch (op) { case Operator::Equal: for (const Value &property_value : property_values) { - if (!includes(property_value, util::relaxed_equal)) { - return false; + if (includes(property_value, util::relaxed_equal)) { + return true; } } - return true; + return false; case Operator::NotEqual: for (const Value &property_value : property_values) { if (includes(property_value, util::relaxed_equal)) { diff --git a/src/util/io.cpp b/src/util/io.cpp index 1312b907b..2fd638cdf 100644 --- a/src/util/io.cpp +++ b/src/util/io.cpp @@ -15,7 +15,7 @@ void write_file(const std::string &filename, const std::string &data) { fwrite(data.data(), sizeof(std::string::value_type), data.size(), fd); fclose(fd); } else { - fprintf(stderr, "Failed to open file\n"); + throw std::runtime_error(std::string("Failed to open file ") + filename); } } @@ -26,7 +26,7 @@ std::string read_file(const std::string &filename) { data << file.rdbuf(); return data.str(); } else { - throw std::runtime_error("Cannot read file " + filename); + throw std::runtime_error(std::string("Cannot read file ") + filename); } } diff --git a/test/fixtures/fixture_log.cpp b/test/fixtures/fixture_log.cpp index 159f927e2..9253f5f98 100644 --- a/test/fixtures/fixture_log.cpp +++ b/test/fixtures/fixture_log.cpp @@ -24,6 +24,7 @@ std::vector FixtureLogBackend::unchecked() const for (const LogMessage &msg : messages) { if (!msg.checked) { unchecked.push_back(msg); + msg.checked = true; } } return unchecked; @@ -37,10 +38,10 @@ std::vector FixtureLogBackend::unchecked() const } ::std::ostream& operator<<(::std::ostream& os, const FixtureLogBackend::LogMessage& message) { - os << "[\"" << message.severity.get() << "\", \"" << message.event.get(); - if (message.code) os << "\", " << message.code.get(); - if (message.msg) os << ", \"" << message.msg.get(); - return os << "\"]" << std::endl; + os << "[\"" << message.severity.get() << "\", \"" << message.event.get() << "\""; + if (message.code) os << ", " << message.code.get(); + if (message.msg) os << ", \"" << message.msg.get() << "\""; + return os << "]" << std::endl; } } \ No newline at end of file diff --git a/test/fixtures/fixture_log.hpp b/test/fixtures/fixture_log.hpp index 8a4124185..c41d74ab9 100644 --- a/test/fixtures/fixture_log.hpp +++ b/test/fixtures/fixture_log.hpp @@ -2,6 +2,7 @@ #define LLMR_TEST_FIXTURE_LOG #include +#include #include @@ -10,7 +11,7 @@ namespace llmr { -class FixtureLogBackend : public LogBackend { +class FixtureLogBackend : public LogBackend, private util::noncopyable { public: struct LogMessage { inline LogMessage(EventSeverity severity, Event event, int64_t code, const std::string &msg) diff --git a/test/fixtures/styles/icons.info.json b/test/fixtures/styles/icons.info.json new file mode 100644 index 000000000..e116913aa --- /dev/null +++ b/test/fixtures/styles/icons.info.json @@ -0,0 +1,18 @@ +{ + "default": { + "zoom": 14, + "center": [52.499167, 13.418056], + "height": 256, + "log": [ + [1, "INFO", "HttpRequest", 200, "sprites/outdoors.json"], + [1, "INFO", "HttpRequest", 200, "sprites/outdoors.png"], + [1, "INFO", "HttpRequest", 200, "tiles/14-8803-5375.vector.pbf"], + [1, "INFO", "HttpRequest", 200, "tiles/14-8802-5375.vector.pbf"], + [1, "INFO", "HttpRequest", 200, "tiles/14-8803-5374.vector.pbf"], + [1, "INFO", "HttpRequest", 200, "tiles/14-8802-5374.vector.pbf"], + [6, "INFO", "HttpRequest"], + + [1, "INFO", "Sprite", "loaded sprites/outdoors"] + ] + } +} diff --git a/test/fixtures/styles/icons.style.json b/test/fixtures/styles/icons.style.json new file mode 100644 index 000000000..46adfbd40 --- /dev/null +++ b/test/fixtures/styles/icons.style.json @@ -0,0 +1,33 @@ +{ + "version": 3, + "sources": { + "mapbox": { + "type": "vector", + "url": "tiles/{z}-{x}-{y}.vector.pbf", + "tileSize": 512, + "maxZoom": 14 + } + }, + "sprite": "sprites/outdoors", + "layers": [ + { + "id": "background", + "type": "background", + "style": { + "background-color": "green" + } + }, + { + "id": "poi", + "source": "mapbox", + "source-layer": "poi_label", + "type": "icon", + "filter": { "maki": "restaurant" }, + "render": { + "icon-image": "{maki}-12", + "icon-size": 12, + "icon-allow-overlap": true + } + } + ] +} diff --git a/test/fixtures/styles/icons/default.expected.png b/test/fixtures/styles/icons/default.expected.png new file mode 100644 index 000000000..17e3252c8 Binary files /dev/null and b/test/fixtures/styles/icons/default.expected.png differ