void SpriteStore::setURL(const std::string& url) { if (url.empty()) { // Treat a non-existent sprite as a successfully loaded empty sprite. loaded = true; return; } std::string spriteURL(url + (pixelRatio > 1 ? "@2x" : "") + ".png"); std::string jsonURL(url + (pixelRatio > 1 ? "@2x" : "") + ".json"); loader = std::make_unique<Loader>(); FileSource* fs = util::ThreadContext::getFileSource(); loader->jsonRequest = fs->request({ Resource::Kind::SpriteJSON, jsonURL }, util::RunLoop::getLoop(), [this, jsonURL](const Response& res) { if (res.stale) { // Only handle fresh responses. return; } loader->jsonRequest = nullptr; if (res.error) { std::stringstream message; message << "Failed to load [" << jsonURL << "]: " << res.error->message; emitSpriteLoadingFailed(message.str()); return; } else { loader->json = res.data; } emitSpriteLoadedIfComplete(); }); loader->spriteRequest = fs->request({ Resource::Kind::SpriteImage, spriteURL }, util::RunLoop::getLoop(), [this, spriteURL](const Response& res) { if (res.stale) { // Only handle fresh responses. return; } loader->spriteRequest = nullptr; if (res.error) { std::stringstream message; message << "Failed to load [" << spriteURL << "]: " << res.error->message; emitSpriteLoadingFailed(message.str()); return; } else { loader->image = res.data; } emitSpriteLoadedIfComplete(); }); }
void SpriteStore::setURL(const std::string& url) { if (url.empty()) { // Treat a non-existent sprite as a successfully loaded empty sprite. loaded = true; return; } std::string spriteURL(url + (pixelRatio > 1 ? "@2x" : "") + ".png"); std::string jsonURL(url + (pixelRatio > 1 ? "@2x" : "") + ".json"); loader = std::make_unique<Loader>(); FileSource* fs = util::ThreadContext::getFileSource(); loader->jsonRequest = fs->request({ Resource::Kind::SpriteJSON, jsonURL }, [this, jsonURL](Response res) { if (res.stale) { // Only handle fresh responses. return; } loader->jsonRequest = nullptr; if (res.error) { observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message))); } else { loader->json = res.data; emitSpriteLoadedIfComplete(); } }); loader->spriteRequest = fs->request({ Resource::Kind::SpriteImage, spriteURL }, [this, spriteURL](Response res) { if (res.stale) { // Only handle fresh responses. return; } loader->spriteRequest = nullptr; if (res.error) { observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message))); } else { loader->image = res.data; emitSpriteLoadedIfComplete(); } }); }
Sprite::Sprite(const std::string& baseUrl, float pixelRatio_) : pixelRatio(pixelRatio_ > 1 ? 2 : 1) { if (baseUrl.empty()) { // Treat a non-existent sprite as a successfully loaded empty sprite. loaded = true; return; } std::string spriteURL(baseUrl + (pixelRatio_ > 1 ? "@2x" : "") + ".png"); std::string jsonURL(baseUrl + (pixelRatio_ > 1 ? "@2x" : "") + ".json"); loader = std::make_unique<Loader>(); FileSource* fs = util::ThreadContext::getFileSource(); loader->jsonRequest = fs->request({ Resource::Kind::JSON, jsonURL }, util::RunLoop::getLoop(), [this, jsonURL](const Response& res) { loader->jsonRequest = nullptr; if (res.status == Response::Successful) { loader->data->json = res.data; loader->loadedJSON = true; } else { std::stringstream message; message << "Failed to load [" << jsonURL << "]: " << res.message; emitSpriteLoadingFailed(message.str()); return; } emitSpriteLoadedIfComplete(); }); loader->spriteRequest = fs->request({ Resource::Kind::Image, spriteURL }, util::RunLoop::getLoop(), [this, spriteURL](const Response& res) { loader->spriteRequest = nullptr; if (res.status == Response::Successful) { loader->data->image = res.data; loader->loadedImage = true; } else { std::stringstream message; message << "Failed to load [" << spriteURL << "]: " << res.message; emitSpriteLoadingFailed(message.str()); return; } emitSpriteLoadedIfComplete(); }); }