diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index 21f1d170c..3f91c5bc0 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -32,8 +32,8 @@ public: void moveBy(double dx, double dy); void scaleBy(double ds, double cx, double cy); void rotateBy(double cx, double cy, double sx, double sy, double ex, double ey); - void tileLoaded(tile::ptr tile); - void tileFailed(tile::ptr tile); + void tileLoaded(Tile::Ptr tile); + void tileFailed(Tile::Ptr tile); /* position */ void resetNorth(); @@ -46,11 +46,11 @@ public: void toggleDebug(); private: - bool findLoadedChildren(const tile_id& id, int32_t maxCoveringZoom, std::forward_list& retain); - bool findLoadedParent(const tile_id& id, int32_t minCoveringZoom, std::forward_list& retain); + bool findLoadedChildren(const Tile::ID& id, int32_t maxCoveringZoom, std::forward_list& retain); + bool findLoadedParent(const Tile::ID& id, int32_t minCoveringZoom, std::forward_list& retain); void updateTiles(); - tile::ptr addTile(const tile_id& id); - tile::ptr hasTile(const tile_id& id); + Tile::Ptr addTile(const Tile::ID& id); + Tile::Ptr hasTile(const Tile::ID& id); void update(); @@ -63,7 +63,7 @@ private: int32_t min_zoom; int32_t max_zoom; - std::forward_list tiles; + std::forward_list tiles; }; } diff --git a/include/llmr/map/tile.hpp b/include/llmr/map/tile.hpp index 69ff62c54..f9d0d217d 100644 --- a/include/llmr/map/tile.hpp +++ b/include/llmr/map/tile.hpp @@ -11,11 +11,13 @@ namespace llmr { -typedef vec3 tile_id; -class tile { +class Tile { public: - typedef std::shared_ptr ptr; + typedef std::shared_ptr Ptr; + + typedef vec3 ID; + enum state { initial, loading, @@ -24,14 +26,14 @@ public: }; public: - tile(tile_id id); - ~tile(); + Tile(ID id); + ~Tile(); // Make noncopyable - tile(const tile&) = delete; - tile(const tile&&) = delete; - tile &operator=(const tile&) = delete; - tile &operator=(const tile&&) = delete; + Tile(const Tile&) = delete; + Tile(const Tile&&) = delete; + Tile &operator=(const Tile&) = delete; + Tile &operator=(const Tile&&) = delete; // Other functions void setData(uint8_t *data, uint32_t bytes); @@ -44,11 +46,11 @@ public: const std::string toString() const; - static tile_id parent(const tile_id& id, int32_t z); - static std::forward_list children(const tile_id& id, int32_t z); + static ID parent(const ID& id, int32_t z); + static std::forward_list children(const ID& id, int32_t z); public: - const tile_id id; + const ID id; state state; linevertexbuffer lineVertex; debug_font_buffer debugFontVertex; diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index 30dca6216..74144f12e 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -5,12 +5,12 @@ namespace llmr { -class tile; +class Tile; namespace platform { void restart(void *obj); -void request(void *obj, std::shared_ptr tile); +void request(void *obj, std::shared_ptr tile); double time(); } diff --git a/include/llmr/renderer/painter.hpp b/include/llmr/renderer/painter.hpp index 687f86889..ccbab684e 100644 --- a/include/llmr/renderer/painter.hpp +++ b/include/llmr/renderer/painter.hpp @@ -1,16 +1,15 @@ #ifndef LLMR_RENDERER_PAINTER #define LLMR_RENDERER_PAINTER -#include "../map/settings.hpp" -#include "../map/transform.hpp" #include "shader-fill.hpp" #include "shader-line.hpp" #include "shader-outline.hpp" namespace llmr { -class transform; -class tile; +class Settings; +class Transform; +class Tile; class Painter { public: @@ -27,15 +26,15 @@ public: void teardown(); void clear(); - void render(const std::shared_ptr& tile); - void renderDebug(const std::shared_ptr& tile); + void render(const std::shared_ptr& tile); + void renderDebug(const std::shared_ptr& tile); void drawClippingMask(); bool switchShader(Shader *shader); private: void setupShaders(); - void changeMatrix(const std::shared_ptr& tile); + void changeMatrix(const std::shared_ptr& tile); public: diff --git a/macosx/main.mm b/macosx/main.mm index 71ca00592..3fb856444 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -208,7 +208,7 @@ void restart(void *) { view->dirty = true; } -void request(void *, tile::ptr tile) { +void request(void *, Tile::Ptr tile) { assert((bool)tile); // NSString *urlTemplate = @"http://api.tiles.mapbox.com/v3/mapbox.mapbox-streets-v4/%d/%d/%d.vector.pbf"; diff --git a/src/map/map.cpp b/src/map/map.cpp index 81ba185de..768a4fc16 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -96,22 +96,22 @@ void Map::update() { } -tile::ptr Map::hasTile(const tile_id& id) { - for (tile::ptr& tile : tiles) { +Tile::Ptr Map::hasTile(const Tile::ID& id) { + for (Tile::Ptr& tile : tiles) { if (tile->id == id) { return tile; } } - return tile::ptr(); + return Tile::Ptr(); } -tile::ptr Map::addTile(const tile_id& id) { - tile::ptr tile = hasTile(id); +Tile::Ptr Map::addTile(const Tile::ID& id) { + Tile::Ptr tile = hasTile(id); if (!tile.get()) { // We couldn't find the tile in the list. Create a new one. - tile = std::make_shared(id); + tile = std::make_shared(id); assert(tile); // std::cerr << "init " << id.z << "/" << id.x << "/" << id.y << std::endl; // std::cerr << "add " << tile->toString() << std::endl; @@ -130,14 +130,14 @@ tile::ptr Map::addTile(const tile_id& id) { * * @return boolean Whether the children found completely cover the tile. */ -bool Map::findLoadedChildren(const tile_id& id, int32_t maxCoveringZoom, std::forward_list& retain) { +bool Map::findLoadedChildren(const Tile::ID& id, int32_t maxCoveringZoom, std::forward_list& retain) { bool complete = true; int32_t z = id.z; - auto ids = tile::children(id, z + 1); - for (const tile_id& child_id : ids) { - const tile::ptr& tile = hasTile(child_id); - if (tile && tile->state == tile::ready) { + auto ids = Tile::children(id, z + 1); + for (const Tile::ID& child_id : ids) { + const Tile::Ptr& tile = hasTile(child_id); + if (tile && tile->state == Tile::ready) { assert(tile); retain.emplace_front(tile->id); } else { @@ -160,11 +160,11 @@ bool Map::findLoadedChildren(const tile_id& id, int32_t maxCoveringZoom, std::fo * * @return boolean Whether a parent was found. */ -bool Map::findLoadedParent(const tile_id& id, int32_t minCoveringZoom, std::forward_list& retain) { +bool Map::findLoadedParent(const Tile::ID& id, int32_t minCoveringZoom, std::forward_list& retain) { for (int32_t z = id.z - 1; z >= minCoveringZoom; z--) { - const tile_id parent_id = tile::parent(id, z); - const tile::ptr tile = hasTile(parent_id); - if (tile && tile->state == tile::ready) { + const Tile::ID parent_id = Tile::parent(id, z); + const Tile::Ptr tile = hasTile(parent_id); + if (tile && tile->state == Tile::ready) { assert(tile); retain.emplace_front(tile->id); return true; @@ -201,7 +201,7 @@ void Map::updateTiles() { // TODO: Discard tiles that are outside the viewport - std::forward_list required; + std::forward_list required; for (int32_t y = tl.y; y < br.y; y++) { for (int32_t x = tl.x; x < br.x; x++) { required.emplace_front(x, y, zoom); @@ -211,14 +211,14 @@ void Map::updateTiles() { // Retain is a list of tiles that we shouldn't delete, even if they are not // the most ideal tile for the current viewport. This may include tiles like // parent or child tiles that are *already* loaded. - std::forward_list retain(required); + std::forward_list retain(required); // Add existing child/parent tiles if the actual tile is not yet loaded - for (const tile_id& id : required) { - tile::ptr tile = addTile(id); + for (const Tile::ID& id : required) { + Tile::Ptr tile = addTile(id); assert(tile); - if (tile->state != tile::ready) { + if (tile->state != Tile::ready) { // The tile we require is not yet loaded. Try to find a parent or // child tile that we already have. @@ -233,16 +233,16 @@ void Map::updateTiles() { } } - if (tile->state == tile::initial) { + if (tile->state == Tile::initial) { // If the tile is new, we have to make sure to load it. - tile->state = tile::loading; + tile->state = Tile::loading; platform::request(this, tile); } } // Remove tiles that we definitely don't need, i.e. tiles that are not on // the required list. - tiles.remove_if([&retain](const tile::ptr& tile) { + tiles.remove_if([&retain](const Tile::Ptr& tile) { assert(tile); bool obsolete = std::find(retain.begin(), retain.end(), tile->id) == retain.end(); if (obsolete) { @@ -254,7 +254,7 @@ void Map::updateTiles() { // Sort tiles by zoom level, front to back. // We're painting front-to-back, so we want to draw more detailed tiles first // before filling in other parts with lower zoom levels. - tiles.sort([](const tile::ptr& a, const tile::ptr& b) { + tiles.sort([](const Tile::Ptr& a, const Tile::Ptr& b) { return a->id.z > b->id.z; }); } @@ -264,9 +264,9 @@ bool Map::render() { painter.clear(); - for (tile::ptr& tile : tiles) { + for (Tile::Ptr& tile : tiles) { assert(tile); - if (tile->state == tile::ready) { + if (tile->state == Tile::ready) { painter.render(tile); } } @@ -274,12 +274,12 @@ bool Map::render() { return transform.needsAnimation(); } -void Map::tileLoaded(tile::ptr tile) { +void Map::tileLoaded(Tile::Ptr tile) { // std::cerr << "loaded " << tile->toString() << std::endl; update(); } -void Map::tileFailed(tile::ptr tile) { +void Map::tileFailed(Tile::Ptr tile) { // fprintf(stderr, "[%8zx] tile failed to load %d/%d/%d\n", // std::hash()(std::this_thread::get_id()), // tile->z, tile->x, tile->y); diff --git a/src/map/tile.cpp b/src/map/tile.cpp index 3a2c5ce0b..59cf2e4c6 100644 --- a/src/map/tile.cpp +++ b/src/map/tile.cpp @@ -12,9 +12,9 @@ using namespace llmr; -tile_id tile::parent(const tile_id& id, int32_t z) { +Tile::ID Tile::parent(const ID& id, int32_t z) { assert(z < id.z); - tile_id pos(id); + ID pos(id); while (pos.z > z) { pos.z--; pos.x = floor(pos.x / 2); @@ -24,11 +24,11 @@ tile_id tile::parent(const tile_id& id, int32_t z) { } -std::forward_list tile::children(const tile_id& id, int32_t z) { +std::forward_list Tile::children(const ID& id, int32_t z) { assert(z > id.z); int32_t factor = pow(2, z - id.z); - std::forward_list children; + std::forward_list children; for (int32_t y = id.y * factor, y_max = (id.y + 1) * factor; y < y_max; y++) { for (int32_t x = id.x * factor, x_max = (id.x + 1) * factor; x < x_max; x++) { children.emplace_front(x, y, z); @@ -38,7 +38,7 @@ std::forward_list tile::children(const tile_id& id, int32_t z) { } -tile::tile(tile_id id) +Tile::Tile(ID id) : id(id), state(initial), data(0), @@ -50,25 +50,25 @@ tile::tile(tile_id id) debugFontVertex.addText(coord, 50, 200, 5); } -tile::~tile() { +Tile::~Tile() { // fprintf(stderr, "[%p] deleting tile %d/%d/%d\n", this, id.z, id.x, id.y); if (this->data) { free(this->data); } } -const std::string tile::toString() const { +const std::string Tile::toString() const { return util::sprintf("[tile %d/%d/%d]", id.z, id.x, id.y); } -void tile::setData(uint8_t *data, uint32_t bytes) { +void Tile::setData(uint8_t *data, uint32_t bytes) { this->data = (uint8_t *)malloc(bytes); this->bytes = bytes; memcpy(this->data, data, bytes); } -void tile::cancel() { +void Tile::cancel() { // TODO: thread safety if (state != obsolete) { state = obsolete; @@ -77,7 +77,7 @@ void tile::cancel() { } } -bool tile::parse() { +bool Tile::parse() { if (state == obsolete) { return false; } @@ -112,7 +112,7 @@ bool tile::parse() { return true; } -void tile::parseLayer(const uint8_t *data, uint32_t bytes) { +void Tile::parseLayer(const uint8_t *data, uint32_t bytes) { pbf layer(data, bytes); std::string name; while (layer.next()) { @@ -128,7 +128,7 @@ void tile::parseLayer(const uint8_t *data, uint32_t bytes) { } } -void tile::parseFeature(const uint8_t *data, uint32_t bytes) { +void Tile::parseFeature(const uint8_t *data, uint32_t bytes) { pbf feature(data, bytes); while (feature.next()) { if (feature.tag == 1) { @@ -151,7 +151,7 @@ void tile::parseFeature(const uint8_t *data, uint32_t bytes) { } } -void tile::loadGeometry(const uint8_t *data, uint32_t bytes) { +void Tile::loadGeometry(const uint8_t *data, uint32_t bytes) { geometry geometry(data, bytes); geometry::command cmd; diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index b0c49d802..c0e0ce7c9 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -83,7 +83,7 @@ void Painter::teardown() { } -void Painter::changeMatrix(const tile::ptr& tile) { +void Painter::changeMatrix(const Tile::Ptr& tile) { // Initialize projection matrix float projMatrix[16]; mat4::ortho(projMatrix, 0, transform.width, transform.height, 0, 1, 10); @@ -135,8 +135,8 @@ void Painter::clear() { glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); } -void Painter::render(const tile::ptr& tile) { - if (tile->state != tile::ready) { +void Painter::render(const Tile::Ptr& tile) { + if (tile->state != Tile::ready) { return; } @@ -164,7 +164,7 @@ void Painter::render(const tile::ptr& tile) { } } -void Painter::renderDebug(const tile::ptr& tile) { +void Painter::renderDebug(const Tile::Ptr& tile) { // draw tile outline switchShader(lineShader); glUniformMatrix4fv(lineShader->u_matrix, 1, GL_FALSE, matrix);