| 188 | } |
| 189 | |
| 190 | void TextureCache::loadAsync(String filename, const std::function<void(Texture2D*)>& handler) { |
| 191 | std::string fullPath = SharedContent.getFullPath(filename); |
| 192 | auto it = _textures.find(fullPath); |
| 193 | if (it != _textures.end()) { |
| 194 | handler(it->second); |
| 195 | return; |
| 196 | } |
| 197 | std::string file(filename.toString()); |
| 198 | SharedContent.loadAsyncUnsafe(fullPath, [this, file, handler](uint8_t* data, int64_t size) { |
| 199 | if (!data) { |
| 200 | Error("failed to read file data from \"{}\".", file); |
| 201 | handler(nullptr); |
| 202 | return; |
| 203 | } |
| 204 | SharedAsyncThread.run( |
| 205 | [this, data, size]() { |
| 206 | bimg::ImageContainer* imageContainer = bimg::imageParse(&_allocator, data, s_cast<uint32_t>(size)); |
| 207 | delete[] data; |
| 208 | return Values::alloc(imageContainer); |
| 209 | }, |
| 210 | [this, file, handler](Own<Values> result) { |
| 211 | bimg::ImageContainer* imageContainer; |
| 212 | result->get(imageContainer); |
| 213 | if (Texture2D* texture = createTexture(&_allocator, imageContainer, file)) { |
| 214 | std::string fullPath = SharedContent.getFullPath(file); |
| 215 | _textures[fullPath] = texture; |
| 216 | handler(texture); |
| 217 | } else { |
| 218 | Error("texture format \"{}\" is not supported for \"{}\".", Path::getExt(file), file); |
| 219 | handler(nullptr); |
| 220 | } |
| 221 | }); |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | bool TextureCache::unload(Texture2D* texture) { |
| 226 | for (auto it = _textures.begin(); it != _textures.end(); ++it) { |
nothing calls this directly
no test coverage detected