在cocos2dx 3.2之前,引擎没有提供截图功能,我们可以用RenderTexture来实现截图功能,这个方法在cocos2dx 3.2之后也是可以用的。
--lua 脚本实现截图功能的函数:
function Resources.getScreenShot(node)
if node == nil or tolua.isnull(node) then
cclog("getScreenShot node == nil")
return nil
end
local size = node:getContentSize()
if size.width <= 0 or size.height <= 0 then
size = cc.Director:getInstance():getWinSize()
end
local screen = cc.RenderTexture:create(size.width, size.height, cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888, 0x88F0)
screen:begin()
node:visit()
screen:endToLua()
local uroot = cc.FileUtils:getInstance():getWritablePath()
local filePath = string.format("ss_%d.png",os.time)
screen:saveToFile(filePath, kCCImageFormatPNG)
return uroot..filePath
end
创建RenderTexture时需要四个参数(宽,高,渲染纹理格式,深度),渲染纹理格式参数为空时,默认渲染纹理格式为RGBA8888,深度参数为空时,默认深度为0。
用RenderTexture实现截图功能的原理是:先创建一个指定的渲染纹理并开始记录,然后通过访问指定节点并递归的绘制所有子节点,当所有子节点都绘制完成之后,渲染纹理停止记录并且将渲染纹理保存成图片。简单的说就是创建一个画布,将需要的节点的内容画在画布上,然后将画布保存起来。
用RenderTexture实现截图功能的优点:1、对内存消耗比较小,在真机上测试时体现为截图快,耗时少;2、能够指定截图的大小和内容;
用RenderTexture实现截图功能的缺点:如果使用RenderTexture实现截图时,指定的截图根节点下包含Sprite3D,则得到的截图并不包含Sprite3D(这个缺点是我在使用过程中遇到的,具体原因还没深究,欢迎指点)
第二种方法:用utils:captureScreen的方法截图
在cocos2dx 3.2版本,utils:captureScreen()方法被加入用于保存屏幕截图
//captureScreen接口源码
static EventListenerCustom* s_captureScreenListener;
static CustomCommand s_captureScreenCommand;
void captureScreen(const std::function& afterCaptured, const std::string& filename)
{
if (s_captureScreenListener)
{
CCLOG("Warning: CaptureScreen has been called already, don't call more than once in one frame.");
return;
}
s_captureScreenCommand.init(std::numeric_limits::max());
s_captureScreenCommand.func = std::bind(onCaptureScreen, afterCaptured, filename);
s_captureScreenListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_AFTER_DRAW, [](EventCustom* /*event*/) {
auto director = Director::getInstance();
director->getEventDispatcher()->removeEventListener((EventListener*)(s_captureScreenListener));
s_captureScreenListener = nullptr;
director->getRenderer()->addCommand(&s_captureScreenCommand);
director->getRenderer()->render();
});
}
//captureScreen实现的源码
/**
* Capture screen implementation, don't use it directly.
*/
void onCaptureScreen(const std::function& afterCaptured, const std::string& filename)
{
static bool startedCapture = false;
if (startedCapture)
{
CCLOG("Screen capture is already working");
if (afterCaptured)
{
afterCaptured(false, filename);
}
return;
}
else
{
startedCapture = true;
}
auto glView = Director::getInstance()->getOpenGLView();
auto frameSize = glView->getFrameSize();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
frameSize = frameSize * glView->getFrameZoomFactor() * glView->getRetinaFactor();
#endif
int width = static_cast(frameSize.width);
int height = static_cast(frameSize.height);
bool succeed = false;
std::string outputFile = "";
do
{
std::shared_ptr buffer(new GLubyte[width * height * 4], [](GLubyte* p){ CC_SAFE_DELETE_ARRAY(p); });
if (!buffer)
{
break;
}
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer.get());
std::shared_ptr flippedBuffer(new GLubyte[width * height * 4], [](GLubyte* p) { CC_SAFE_DELETE_ARRAY(p); });
if (!flippedBuffer)
{
break;
}
for (int row = 0; row < height; ++row)
{
memcpy(flippedBuffer.get() + (height - row - 1) * width * 4, buffer.get() + row * width * 4, width * 4);
}
Image* image = new (std::nothrow) Image;
if (image)
{
image->initWithRawData(flippedBuffer.get(), width * height * 4, width, height, 8);
if (FileUtils::getInstance()->isAbsolutePath(filename))
{
outputFile = filename;
}
else
{
CCASSERT(filename.find("/") == std::string::npos, "The existence of a relative path is not guaranteed!");
outputFile = FileUtils::getInstance()->getWritablePath() + filename;
}
// Save image in AsyncTaskPool::TaskType::TASK_IO thread, and call afterCaptured in mainThread
static bool succeedSaveToFile = false;
std::function mainThread = [afterCaptured, outputFile](void* /*param*/)
{
if (afterCaptured)
{
afterCaptured(succeedSaveToFile, outputFile);
}
startedCapture = false;
};
AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, std::move(mainThread), nullptr, [image, outputFile]()
{
succeedSaveToFile = image->saveToFile(outputFile);
delete image;
});
}
else
{
CCLOG("Malloc Image memory failed!");
if (afterCaptured)
{
afterCaptured(succeed, outputFile);
}
startedCapture = false;
}
} while (0);
}
--lua脚本调用captureScreen
function Resources.getNewScreenShot( imgName, callBack )
local function defaultCallBack( ... )
-- cclog("test getNewScreenShot()")
end
local afterCaptured = callBack or defaultCallBack
local uroot = cc.FileUtils:getInstance():getWritablePath()
local filePath = imgName or string.format("ss_%d.png",os.time())
cc.utils:captureScreen(afterCaptured, filePath)
return uroot..filePath
end
调用captureScreen需要两个参数:截图保存的文件名以及截图完成后的回调
通过源码分析,captureScreen截图是通过glReadPixels()把已经绘制好的像素(它可能已经被保存到显卡的显存中)读取到内存,然后再将这部分数据复制一份出来,然后新建一个Image,用这份数据初始化Image,然后另外开启一个线程完成将Image保存到指定路径、执行完成截图后的回调、删除Image对象的工作。
调用captureScreen截图的优点:captureScreen截图就像是手机截图,能够将手机屏幕显示的所有像素保存在截图内,即使面对Sprite3D,也能完成截图。
调用captureScreen截图的缺点:1、无法指定截图大小,截图内容;2、对内存消耗比较大,在真机上测试时体现为截图较慢,耗时较长