2013-10-14 14:56:57 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-10-14 14:56:57 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-05-17 05:36:00 +08:00
|
|
|
|
|
|
|
#include "base/ccUtils.h"
|
2014-07-14 20:42:08 +08:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "renderer/CCCustomCommand.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "platform/CCImage.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2013-10-14 14:56:57 +08:00
|
|
|
|
2014-05-14 16:25:47 +08:00
|
|
|
NS_CC_BEGIN
|
2013-10-14 14:56:57 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
int ccNextPOT(int x)
|
2013-10-14 14:56:57 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2014-05-14 16:25:47 +08:00
|
|
|
|
2014-05-27 14:29:52 +08:00
|
|
|
namespace utils
|
2014-05-27 10:44:23 +08:00
|
|
|
{
|
2014-05-27 14:29:52 +08:00
|
|
|
/**
|
|
|
|
* Capture screen implementation, don't use it directly.
|
|
|
|
*/
|
2014-05-27 10:44:23 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-07-01 14:48:43 +08:00
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
for (int row = 0; row < height; ++row)
|
|
|
|
{
|
|
|
|
memcpy(flippedBuffer.get() + (height - row - 1) * width * 4, buffer.get() + row * width * 4, width * 4);
|
|
|
|
}
|
2014-07-01 14:48:43 +08:00
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-05-27 14:29:52 +08:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
2014-06-26 14:05:30 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-07-14 20:42:08 +08:00
|
|
|
|
|
|
|
#define MAX_ITOA_BUFFER_SIZE 256
|
|
|
|
double atof(const char* str)
|
|
|
|
{
|
|
|
|
if (str == nullptr)
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[MAX_ITOA_BUFFER_SIZE];
|
|
|
|
strncpy(buf, str, MAX_ITOA_BUFFER_SIZE);
|
|
|
|
|
|
|
|
// strip string, only remain 7 numbers after '.'
|
|
|
|
char* dot = strchr(buf, '.');
|
|
|
|
if (dot != nullptr && dot - buf + 8 < MAX_ITOA_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
dot[8] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::atof(buf);
|
|
|
|
}
|
2014-08-04 11:45:50 +08:00
|
|
|
|
|
|
|
double gettime()
|
|
|
|
{
|
2014-08-04 11:48:23 +08:00
|
|
|
struct timeval tv;
|
2014-08-04 15:18:01 +08:00
|
|
|
gettimeofday(&tv, nullptr);
|
2014-08-04 11:45:50 +08:00
|
|
|
|
2014-08-04 11:48:23 +08:00
|
|
|
return (double)tv.tv_sec + (double)tv.tv_usec/1000000;
|
2014-08-04 11:45:50 +08:00
|
|
|
}
|
2014-08-04 15:18:01 +08:00
|
|
|
|
2015-06-29 11:52:53 +08:00
|
|
|
long long getTimeInMilliseconds()
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday (&tv, NULL);
|
|
|
|
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
|
|
|
}
|
|
|
|
|
2014-09-18 15:27:11 +08:00
|
|
|
Rect getCascadeBoundingBox(Node *node)
|
|
|
|
{
|
|
|
|
Rect cbb;
|
|
|
|
Size contentSize = node->getContentSize();
|
|
|
|
|
|
|
|
// check all childrens bounding box, get maximize box
|
|
|
|
Node* child = nullptr;
|
|
|
|
bool merge = false;
|
|
|
|
for(auto object : node->getChildren())
|
|
|
|
{
|
|
|
|
child = dynamic_cast<Node*>(object);
|
|
|
|
if (!child->isVisible()) continue;
|
|
|
|
|
2014-09-18 15:40:28 +08:00
|
|
|
const Rect box = getCascadeBoundingBox(child);
|
2014-09-18 15:27:11 +08:00
|
|
|
if (box.size.width <= 0 || box.size.height <= 0) continue;
|
|
|
|
|
|
|
|
if (!merge)
|
|
|
|
{
|
|
|
|
cbb = box;
|
|
|
|
merge = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cbb.merge(box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge content size
|
|
|
|
if (contentSize.width > 0 && contentSize.height > 0)
|
|
|
|
{
|
|
|
|
const Rect box = RectApplyAffineTransform(Rect(0, 0, contentSize.width, contentSize.height), node->getNodeToWorldAffineTransform());
|
|
|
|
if (!merge)
|
|
|
|
{
|
|
|
|
cbb = box;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cbb.merge(box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cbb;
|
|
|
|
}
|
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
}
|
|
|
|
|
2014-05-26 16:15:30 +08:00
|
|
|
NS_CC_END
|