日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Cocos2d-x 截屏功能集成

發布時間:2025/7/25 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Cocos2d-x 截屏功能集成 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?

1.集成到Director
這里選擇把截屏功能繼承到Director中,讓全局的導演來執行截屏功能是一個很好的主意。

void Director::saveScreenshot(const std::string& fileName,const std::function<void(const std::string&)>& callback) { Image::Format format; //進行后綴判斷 if(std::string::npos != fileName.find_last_of(".")){ auto extension = fileName.substr(fileName.find_last_of("."),fileName.length()); if (!extension.compare(".png")) { format = Image::Format::PNG; } else if(!extension.compare(".jpg")) { format = Image::Format::JPG; } else{ CCLOG("cocos2d: the image can only be saved as JPG or PNG format"); return; } } else { CCLOG("cocos2d: the image can only be saved as JPG or PNG format"); return ; } //獲取屏幕尺寸,初始化一個空的渲染紋理對象 auto renderTexture = RenderTexture::create(getWinSize().width, getWinSize().height, Texture2D::PixelFormat::RGBA8888); //清空并開始獲取 renderTexture->beginWithClear(0.0f, 0.0f, 0.0f, 0.0f); //遍歷場景節點對象,填充紋理到RenderTexture中 getRunningScene()->visit(); //結束獲取 renderTexture->end(); //保存文件 renderTexture->saveToFile(fileName , format); //使用schedule在下一幀中調用callback函數 auto fullPath = FileUtils::getInstance()->getWritablePath() + fileName; auto scheduleCallback = [&,fullPath,callback](float dt){ callback(fullPath); }; auto _schedule = getRunningScene()->getScheduler(); _schedule->schedule(scheduleCallback, this, 0.0f,0,0.0f, false, "screenshot"); }

2.如何使用saveScreenshot
截屏功能使用起來也很簡單,直接調用saveSecreenshot,其中第一個參數為文件名(支持png和jpg格式,不帶后綴名默認為png格式),第二個參數為回調函數,你可以在回調函數中處理這個文件。

void ScreenshotTest::saveImage(Ref *pSender){ static int counter = 0; char png[20]; sprintf(png, "image-%d.png", counter); char jpg[20]; sprintf(jpg, "image-%d.jpg", counter); //截屏后的回調函數,這里顯示在左下角 auto callback = [&](const std::string& fullPath){ auto sprite = Sprite::create(fullPath); CCASSERT(sprite!=nullptr, "Failed to create sprite."); addChild(sprite); sprite->setScale(0.3f); sprite->setPosition(Point(40, 40)); sprite->setRotation(counter * 3); CCLOG("Image saved %s", fullPath.c_str()); }; //調用Director的截屏功能 Director::getInstance()->saveScreenshot(png, callback); counter++; }

?

轉載于:https://www.cnblogs.com/guangyun/p/7040372.html

總結

以上是生活随笔為你收集整理的Cocos2d-x 截屏功能集成的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。