Take care lambda implicit capture std::string_view

refer to: #1002
This commit is contained in:
halx99 2023-01-03 23:02:17 +08:00
parent 2522b1ef45
commit cb35a48885
4 changed files with 15 additions and 14 deletions

View File

@ -431,7 +431,7 @@ bool RenderTexture::saveToFileAsNonPMA(std::string_view fileName,
auto renderer = _director->getRenderer(); auto renderer = _director->getRenderer();
auto saveToFileCommand = renderer->nextCallbackCommand(); auto saveToFileCommand = renderer->nextCallbackCommand();
saveToFileCommand->init(_globalZOrder); saveToFileCommand->init(_globalZOrder);
saveToFileCommand->func = AX_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA, true); saveToFileCommand->func = AX_CALLBACK_0(RenderTexture::onSaveToFile, this, std::move(fullpath), isRGBA, true);
renderer->addCommand(saveToFileCommand); renderer->addCommand(saveToFileCommand);
return true; return true;
@ -454,26 +454,26 @@ bool RenderTexture::saveToFile(std::string_view fileName,
auto renderer = _director->getRenderer(); auto renderer = _director->getRenderer();
auto saveToFileCommand = renderer->nextCallbackCommand(); auto saveToFileCommand = renderer->nextCallbackCommand();
saveToFileCommand->init(_globalZOrder); saveToFileCommand->init(_globalZOrder);
saveToFileCommand->func = AX_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA, false); saveToFileCommand->func = AX_CALLBACK_0(RenderTexture::onSaveToFile, this, std::move(fullpath), isRGBA, false);
_director->getRenderer()->addCommand(saveToFileCommand); _director->getRenderer()->addCommand(saveToFileCommand);
return true; return true;
} }
void RenderTexture::onSaveToFile(std::string_view filename, bool isRGBA, bool forceNonPMA) void RenderTexture::onSaveToFile(std::string filename, bool isRGBA, bool forceNonPMA)
{ {
auto callbackFunc = [&, filename, isRGBA, forceNonPMA](RefPtr<Image> image) { auto callbackFunc = [this, _filename = std::move(filename), isRGBA, forceNonPMA](RefPtr<Image> image) {
if (image) if (image)
{ {
if (forceNonPMA && image->hasPremultipliedAlpha()) if (forceNonPMA && image->hasPremultipliedAlpha())
{ {
image->reversePremultipliedAlpha(); image->reversePremultipliedAlpha();
} }
image->saveToFile(filename, !isRGBA); image->saveToFile(_filename, !isRGBA);
} }
if (_saveFileCallback) if (_saveFileCallback)
{ {
_saveFileCallback(this, filename); _saveFileCallback(this, _filename);
} }
}; };
newImage(callbackFunc); newImage(callbackFunc);

View File

@ -381,7 +381,7 @@ protected:
void onEnd(); void onEnd();
void clearColorAttachment(); void clearColorAttachment();
void onSaveToFile(std::string_view fileName, bool isRGBA = true, bool forceNonPMA = false); void onSaveToFile(std::string fileName, bool isRGBA = true, bool forceNonPMA = false);
bool _keepMatrix = false; bool _keepMatrix = false;
Rect _rtTextureRect; Rect _rtTextureRect;

View File

@ -296,9 +296,10 @@ void SpriteFrameCache::insertFrame(const std::shared_ptr<SpriteSheet>& spriteShe
bool SpriteFrameCache::eraseFrame(std::string_view frameName) bool SpriteFrameCache::eraseFrame(std::string_view frameName)
{ {
_spriteFrames.erase(frameName); // drop SpriteFrame // drop SpriteFrame
const auto itFrame = _spriteFrameToSpriteSheetMap.find(frameName); const auto itFrame = _spriteFrameToSpriteSheetMap.find(frameName);
if (itFrame != _spriteFrameToSpriteSheetMap.end()) bool hint = itFrame != _spriteFrameToSpriteSheetMap.end();
if (hint)
{ {
auto& spriteSheet = itFrame->second; auto& spriteSheet = itFrame->second;
spriteSheet->full = false; spriteSheet->full = false;
@ -316,9 +317,9 @@ bool SpriteFrameCache::eraseFrame(std::string_view frameName)
//{ //{
// _spriteSheets.clear(); // _spriteSheets.clear();
//} //}
return true;
} }
return false; _spriteFrames.erase(frameName);
return hint;
} }
bool SpriteFrameCache::eraseFrames(const std::vector<std::string_view>& frames) bool SpriteFrameCache::eraseFrames(const std::vector<std::string_view>& frames)

View File

@ -102,7 +102,7 @@ void RenderTextureSave::saveImageWithPremultipliedAlpha(ax::Ref* sender)
char png[20]; char png[20];
sprintf(png, "image-pma-%d.png", counter); sprintf(png, "image-pma-%d.png", counter);
auto callback = [&](RenderTexture* rt, std::string_view path) { auto callback = [this](RenderTexture* rt, std::string_view path) {
auto sprite = Sprite::create(path); auto sprite = Sprite::create(path);
addChild(sprite); addChild(sprite);
sprite->setScale(0.3f); sprite->setScale(0.3f);
@ -127,7 +127,7 @@ void RenderTextureSave::saveImageWithNonPremultipliedAlpha(ax::Ref* sender)
char png[20]; char png[20];
sprintf(png, "image-no-pma-%d.png", counter); sprintf(png, "image-no-pma-%d.png", counter);
auto callback = [&](RenderTexture* rt, std::string_view path) { auto callback = [this](RenderTexture* rt, std::string_view path) {
auto sprite = Sprite::create(path); auto sprite = Sprite::create(path);
addChild(sprite); addChild(sprite);
sprite->setScale(0.3f); sprite->setScale(0.3f);
@ -792,7 +792,7 @@ Issue16113Test::Issue16113Test()
text->setPosition(winSize.width / 2, winSize.height / 2); text->setPosition(winSize.width / 2, winSize.height / 2);
text->Node::visit(); text->Node::visit();
target->end(); target->end();
auto callback = [&](RenderTexture* rt, std::string_view path) { rt->release(); }; auto callback = [this](RenderTexture* rt, std::string_view path) { rt->release(); };
target->retain(); target->retain();
target->saveToFile("issue16113.png", Image::Format::PNG, true, callback); target->saveToFile("issue16113.png", Image::Format::PNG, true, callback);
}); });