Now it shoudl run fine on iOS

This commit is contained in:
visiblelight 2014-05-14 01:06:26 +08:00
parent 4df8624a54
commit 541d03d668
3 changed files with 36 additions and 25 deletions

View File

@ -39,6 +39,7 @@
#include "base/CCEventType.h"
#include "2d/platform/CCImage.h"
#include "2d/platform/CCFileUtils.h"
#include "CCGLView.h"
NS_CC_BEGIN
@ -546,20 +547,28 @@ void Renderer::captureScreen(std::function<void(bool, const std::string&)> after
void Renderer::onCaptureScreen(std::function<void(bool, const std::string&)> afterCaptured, const std::string& filename, bool flipped, const Rect& rect)
{
float scaleFactor = Director::getInstance()->getContentScaleFactor();
int originx = (int)rect.origin.x;
int originy = (int)rect.origin.y;
int width = (int)rect.size.width * scaleFactor;
int height = (int)rect.size.height * scaleFactor;
// Generally the user specifiy the rect with design resolution, thus we have to convert it
// into a significant value which is metered by pixel.
Size frameSize = Director::getInstance()->getOpenGLView()->getFrameSize();
int originx = 0;
int originy = 0;
int width = (int)frameSize.width;
int height = (int)frameSize.height;
bool succeed = false;
std::string targetFile = "";
std::string outputFile = "";
if (rect.equals(Rect::ZERO))
if (!rect.equals(Rect::ZERO))
{
originx = 0;
originy = 0;
width = (int)Director::getInstance()->getWinSize().width * scaleFactor;
height = (int)Director::getInstance()->getWinSize().height * scaleFactor;
originx = (int)rect.origin.x;
originy = (int)rect.origin.y;
width = (int)rect.size.width * Director::getInstance()->getOpenGLView()->getScaleX();
height = (int)rect.size.height * Director::getInstance()->getOpenGLView()->getScaleY();
auto clip = [](int in, int min, int max) { return std::max(std::min(in, max), min); };
originx = clip(originx, 0, (int)frameSize.width);
originy = clip(originy, 0, (int)frameSize.height);
width = clip(width, 0, frameSize.width - originx);
height = clip(height, 0, frameSize.height - originy);
}
do
@ -596,14 +605,14 @@ void Renderer::onCaptureScreen(std::function<void(bool, const std::string&)> aft
{
image->initWithRawData(buffer, width * height * 4, width, height, 8);
CC_SAFE_DELETE_ARRAY(buffer);
targetFile = FileUtils::getInstance()->getWritablePath() + filename;
image->saveToFile(targetFile);
outputFile = FileUtils::getInstance()->getWritablePath() + filename;
image->saveToFile(outputFile);
succeed = true;
}
CC_SAFE_DELETE(image);
}while(0);
afterCaptured(succeed, targetFile);
afterCaptured(succeed, outputFile);
}
NS_CC_END

View File

@ -561,6 +561,7 @@ std::string VBOFullTest::subtitle() const
return "VBO full Test, everthing should render normally";
}
const int CaptureScreenTest::ChildTag;
CaptureScreenTest::CaptureScreenTest()
{
Size s = Director::getInstance()->getWinSize();
@ -589,7 +590,7 @@ CaptureScreenTest::CaptureScreenTest()
addChild(menu);
menu->setPosition(s.width / 2, s.height / 4);
_savedFilename = "";
_filename = "";
}
CaptureScreenTest::~CaptureScreenTest()
@ -608,25 +609,25 @@ std::string CaptureScreenTest::subtitle() const
void CaptureScreenTest::onCaptured(Ref*, const Rect& rect)
{
TextureCache::getInstance()->removeTextureForKey(_savedFilename);
removeChildByTag(119);
_savedFilename = "CaptureScreenTest-Shot.png";
Director::getInstance()->getTextureCache()->removeTextureForKey(_filename);
removeChildByTag(ChildTag);
_filename = "CaptureScreenTest.png";
Director::getInstance()->getRenderer()->captureScreen(
CC_CALLBACK_2(CaptureScreenTest::afterCaptured, this),
_savedFilename,
_filename,
rect);
}
void CaptureScreenTest::afterCaptured(bool succeed, const std::string& target)
void CaptureScreenTest::afterCaptured(bool succeed, const std::string& outputFile)
{
if (succeed)
{
auto sp = Sprite::create(target);
addChild(sp, 0, 119);
auto sp = Sprite::create(outputFile);
addChild(sp, 0, ChildTag);
Size s = Director::getInstance()->getWinSize();
sp->setPosition(s.width / 2, s.height / 2);
sp->setScale(0.25);
_savedFilename = target;
_filename = outputFile;
}
else
{

View File

@ -148,6 +148,7 @@ protected:
class CaptureScreenTest : public MultiSceneTest
{
static const int ChildTag = 119;
public:
CREATE_FUNC(CaptureScreenTest);
virtual std::string title() const override;
@ -158,9 +159,9 @@ protected:
~CaptureScreenTest();
void onCaptured(Ref*, const Rect& rect);
void afterCaptured(bool succeed, const std::string& target);
void afterCaptured(bool succeed, const std::string& outputFile);
std::string _savedFilename;
std::string _filename;
};
#endif //__NewRendererTest_H_