| 69 | } |
| 70 | |
| 71 | void AtlasCache::loadAsync(String filename, const std::function<void(Atlas*)>& handler) { |
| 72 | std::string fullPath = SharedContent.getFullPath(filename); |
| 73 | auto it = _atlas.find(fullPath); |
| 74 | if (it != _atlas.end()) { |
| 75 | handler(it->second); |
| 76 | return; |
| 77 | } |
| 78 | auto dir = Path::getPath(fullPath); |
| 79 | auto file = filename.toString(); |
| 80 | SharedContent.loadAsync(fullPath, [file, dir, handler, this](String data) { |
| 81 | if (data.empty()) { |
| 82 | Error("failed to async load atlas \"{}\".", file); |
| 83 | handler(nullptr); |
| 84 | return; |
| 85 | } |
| 86 | auto atlas = Atlas::create(New<spine::Atlas>( |
| 87 | r_cast<const char*>(data.begin()), |
| 88 | s_cast<int>(data.size()), dir.c_str(), |
| 89 | getTextureLoader(), false)); |
| 90 | auto& pages = atlas->get()->getPages(); |
| 91 | size_t size = pages.size(); |
| 92 | auto atlasData = std::make_shared<std::tuple<Ref<Atlas>, size_t, size_t, bool>>(atlas, 0, size, false); |
| 93 | for (size_t i = 0; i < size; i++) { |
| 94 | const auto& path = pages[i]->texturePath; |
| 95 | std::string texFile(path.buffer(), path.length()); |
| 96 | SharedTextureCache.loadAsync(texFile, [texFile, file, atlasData, i, handler, this](Texture2D* texture) { |
| 97 | auto& data = *atlasData.get(); |
| 98 | auto& atlas = std::get<0>(data); |
| 99 | auto& count = std::get<1>(data); |
| 100 | auto total = std::get<2>(data); |
| 101 | auto& failed = std::get<3>(data); |
| 102 | if (texture) { |
| 103 | auto& page = atlas->get()->getPages()[i]; |
| 104 | page->texture = texture; |
| 105 | page->width = texture->getWidth(); |
| 106 | page->height = texture->getHeight(); |
| 107 | auto& regions = atlas->get()->getRegions(); |
| 108 | for (size_t j = 0; j < regions.size(); j++) { |
| 109 | auto region = regions[j]; |
| 110 | if (region->getPage() == page) { |
| 111 | region->setRendererObject(texture); |
| 112 | } |
| 113 | } |
| 114 | texture->retain(); |
| 115 | } else { |
| 116 | failed = true; |
| 117 | Error("failed to load texture \"{}\" of atlas \"{}\".", texFile, file); |
| 118 | } |
| 119 | count++; |
| 120 | if (count == total) { |
| 121 | if (failed) { |
| 122 | handler(nullptr); |
| 123 | } else { |
| 124 | auto fullPath = SharedContent.getFullPath(file); |
| 125 | _atlas[fullPath] = atlas; |
| 126 | handler(atlas); |
| 127 | } |
| 128 | } |
nothing calls this directly
no test coverage detected