mirror of https://github.com/axmolengine/axmol.git
136 lines
4.5 KiB
C++
136 lines
4.5 KiB
C++
/****************************************************************************
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
|
|
#include "base/ccUtils.h"
|
|
#include "base/CCDirector.h"
|
|
#include "renderer/CCCustomCommand.h"
|
|
#include "renderer/CCRenderer.h"
|
|
#include "platform/CCImage.h"
|
|
#include "platform/CCFileUtils.h"
|
|
#include "CCGLView.h"
|
|
|
|
NS_CC_BEGIN
|
|
|
|
int ccNextPOT(int x)
|
|
{
|
|
x = x - 1;
|
|
x = x | (x >> 1);
|
|
x = x | (x >> 2);
|
|
x = x | (x >> 4);
|
|
x = x | (x >> 8);
|
|
x = x | (x >>16);
|
|
return x + 1;
|
|
}
|
|
|
|
namespace utils
|
|
{
|
|
/**
|
|
* Capture screen implementation, don't use it directly.
|
|
*/
|
|
void onCaptureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename)
|
|
{
|
|
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<int>(frameSize.width);
|
|
int height = static_cast<int>(frameSize.height);
|
|
|
|
bool succeed = false;
|
|
std::string outputFile = "";
|
|
|
|
do
|
|
{
|
|
std::shared_ptr<GLubyte> 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<GLubyte> 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);
|
|
}
|
|
|
|
std::shared_ptr<Image> image(new 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;
|
|
}
|
|
succeed = image->saveToFile(outputFile);
|
|
}
|
|
}while(0);
|
|
|
|
if (afterCaptured)
|
|
{
|
|
afterCaptured(succeed, outputFile);
|
|
}
|
|
}
|
|
/*
|
|
* Capture screen interface
|
|
*/
|
|
void captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename)
|
|
{
|
|
static CustomCommand captureScreenCommand;
|
|
captureScreenCommand.init(std::numeric_limits<float>::max());
|
|
captureScreenCommand.func = std::bind(onCaptureScreen, afterCaptured, filename);
|
|
Director::getInstance()->getRenderer()->addCommand(&captureScreenCommand);
|
|
}
|
|
|
|
std::vector<Node*> findChildren(const Node &node, const std::string &name)
|
|
{
|
|
std::vector<Node*> vec;
|
|
|
|
node.enumerateChildren(name, [&vec](Node* nodeFound) -> bool {
|
|
vec.push_back(nodeFound);
|
|
return false;
|
|
});
|
|
|
|
return vec;
|
|
}
|
|
|
|
}
|
|
|
|
NS_CC_END
|