28using namespace cocos2d::ui;
30RenderTextureTests::RenderTextureTests()
48 auto s = Director::getInstance()->getWinSize();
51 _target = RenderTexture::create(s.width, s.height, backend::PixelFormat::RGBA8888);
53 _target->setPosition(Vec2(s.width / 2, s.height / 2));
59 auto listener = EventListenerTouchAllAtOnce::create();
61 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
this);
64 MenuItemFont::setFontSize(16);
70 auto menu = Menu::create(item1, item2, item3, item4, item5,
nullptr);
72 menu->alignItemsVertically();
78 return "Touch the screen";
83 return "Press 'Save Image' to create an snapshot of the render texture";
88 _target->clear(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());
98 static int counter = 0;
101 sprintf(png,
"image-pma-%d.png", counter);
103 auto callback = [&](RenderTexture* rt,
const std::string& path)
105 auto sprite = Sprite::create(path);
107 sprite->setScale(0.3f);
108 sprite->setPosition(Vec2(40.0f, 40.0f));
109 sprite->setRotation(counter * 3);
114 _target->saveToFile(png, Image::Format::PNG,
true, callback);
116 Director::getInstance()->getRenderer()->render();
117 CCLOG(
"Image saved %s", png);
124 static int counter = 0;
127 sprintf(png,
"image-no-pma-%d.png", counter);
129 auto callback = [&](RenderTexture* rt,
const std::string& path)
131 auto sprite = Sprite::create(path);
133 sprite->setScale(0.3f);
134 sprite->setPosition(Vec2(40.0f, 40.0f));
135 sprite->setRotation(counter * 3);
140 _target->saveToFileAsNonPMA(png, Image::Format::PNG,
true, callback);
143 Director::getInstance()->getRenderer()->render();
144 CCLOG(
"Image saved %s", png);
151 auto s = Director::getInstance()->getWinSize();
156 Sprite* sprite = Sprite::create(
"Images/test-rgba1.png");
157 sprite->setPosition(sprite->getContentSize().width + CCRANDOM_0_1() * (s.width - sprite->getContentSize().width), sprite->getContentSize().height + CCRANDOM_0_1() * (s.height - sprite->getContentSize().height));
167 Director::getInstance()->getTextureCache()->removeUnusedTextures();
172 auto touch = touches[0];
173 auto start = touch->getLocation();
174 auto end = touch->getPreviousLocation();
181 float distance = start.getDistance(end);
184 int d = (int)distance;
186 for(
int i = 0; i < d; ++i)
188 Sprite * sprite = Sprite::create(
"Images/fire.png");
189 sprite->setColor(Color3B::RED);
190 sprite->setOpacity(20);
193 for (
int i = 0; i < d; i++)
195 float difx = end.x - start.x;
196 float dify = end.y - start.y;
197 float delta = (float)i / distance;
198 _brushs.at(i)->setPosition(Vec2(start.x + (difx * delta), start.y + (dify * delta)));
199 _brushs.at(i)->setRotation(rand() % 360);
200 float r = (float)(rand() % 50 / 50.f) + 0.25f;
204 _brushs.at(i)->setColor(Color3B(rand() % 127 + 128, 255, 255));
232 auto background = LayerColor::create(Color4B(200,200,200,255));
233 addChild(background);
235 auto s = Director::getInstance()->getWinSize();
236 auto spr_premulti = Sprite::create(
"Images/fire.png");
237 spr_premulti->setPosition(Vec2(s.width/2-16, s.height/2+16));
239 auto spr_nonpremulti = Sprite::create(
"Images/fire.png");
240 spr_nonpremulti->setPosition(Vec2(s.width/2-16, s.height/2-16));
243 auto rend = RenderTexture::create(32, 64, backend::PixelFormat::RGBA8888);
250 auto spr_size = spr_premulti->getContentSize();
251 rend->setKeepMatrix(
true);
252 Size pixelSize = Director::getInstance()->getWinSizeInPixels();
253 rend->setVirtualViewport(Vec2(s.width/2-32, s.height/2-32),Rect(0,0,s.width,s.height),Rect(0,0,pixelSize.width,pixelSize.height));
258 spr_premulti->visit();
259 spr_nonpremulti->visit();
262 rend->setPosition(Vec2(s.width/2+16, s.height/2));
264 addChild(spr_nonpremulti);
265 addChild(spr_premulti);
271 return "Testing issue #937";
276 return "All images should be equal...";
285 auto listener = EventListenerTouchAllAtOnce::create();
289 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
this);
291 auto size = Director::getInstance()->getWinSize();
292 auto label = Label::createWithTTF(
"vertexZ = 50",
"fonts/Marker Felt.ttf", 64);
293 label->setPosition(Vec2(size.width / 2, size.height * 0.25f));
294 this->addChild(label);
296 auto label2 = Label::createWithTTF(
"vertexZ = 0",
"fonts/Marker Felt.ttf", 64);
297 label2->setPosition(Vec2(size.width / 2, size.height * 0.5f));
298 this->addChild(label2);
300 auto label3 = Label::createWithTTF(
"vertexZ = -50",
"fonts/Marker Felt.ttf", 64);
301 label3->setPosition(Vec2(size.width / 2, size.height * 0.75f));
302 this->addChild(label3);
304 label->setPositionZ(50);
305 label2->setPositionZ(0);
306 label3->setPositionZ(-50);
308 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(
"Images/bugs/circle.plist");
309 mgr = SpriteBatchNode::create(
"Images/bugs/circle.png", 9);
311 sp1 = Sprite::createWithSpriteFrameName(
"circle.png");
312 sp2 = Sprite::createWithSpriteFrameName(
"circle.png");
313 sp3 = Sprite::createWithSpriteFrameName(
"circle.png");
314 sp4 = Sprite::createWithSpriteFrameName(
"circle.png");
315 sp5 = Sprite::createWithSpriteFrameName(
"circle.png");
316 sp6 = Sprite::createWithSpriteFrameName(
"circle.png");
317 sp7 = Sprite::createWithSpriteFrameName(
"circle.png");
318 sp8 = Sprite::createWithSpriteFrameName(
"circle.png");
319 sp9 = Sprite::createWithSpriteFrameName(
"circle.png");
331 sp1->setPositionZ(400);
332 sp2->setPositionZ(300);
333 sp3->setPositionZ(200);
334 sp4->setPositionZ(100);
335 sp5->setPositionZ(0);
336 sp6->setPositionZ(-100);
337 sp7->setPositionZ(-200);
338 sp8->setPositionZ(-300);
339 sp9->setPositionZ(-400);
342 sp9->setColor(Color3B::YELLOW);
347 return "Testing Z Buffer in Render Texture";
352 return "Touch screen. It should be green";
358 for (
auto &item: touches)
360 auto touch =
static_cast<Touch*
>(item);
361 auto location = touch->getLocation();
363 sp1->setPosition(location);
364 sp2->setPosition(location);
365 sp3->setPosition(location);
366 sp4->setPosition(location);
367 sp5->setPosition(location);
368 sp6->setPosition(location);
369 sp7->setPosition(location);
370 sp8->setPosition(location);
371 sp9->setPosition(location);
377 for (
auto &item: touches)
379 auto touch =
static_cast<Touch*
>(item);
380 auto location = touch->getLocation();
382 sp1->setPosition(location);
383 sp2->setPosition(location);
384 sp3->setPosition(location);
385 sp4->setPosition(location);
386 sp5->setPosition(location);
387 sp6->setPosition(location);
388 sp7->setPosition(location);
389 sp8->setPosition(location);
390 sp9->setPosition(location);
401 auto texture = RenderTexture::create(512, 512);
402 if (
nullptr == texture)
413 auto sprite = Sprite::createWithTexture(texture->getSprite()->getTexture());
415 sprite->setPosition(Vec2(256, 256));
416 sprite->setOpacity(182);
417 sprite->setFlippedY(1);
418 this->addChild(sprite, 999999);
419 sprite->setColor(Color3B::GREEN);
421 sprite->runAction(Sequence::create(FadeTo::create(2, 0),
428 auto sprite1 = Sprite::create(
"Images/grossini.png");
429 auto sprite11 = Sprite::create(
"Images/grossini.png");
430 auto sprite2 = Sprite::create(
"Images/grossinis_sister1.png");
431 auto sprite22 = Sprite::create(
"Images/grossinis_sister1.png");
432 Size size = Director::getInstance()->getWinSize();
433 Size sprite1Size = sprite1->getContentSize();
434 sprite1->setPosition((size.width-sprite1Size.width)/2 - 20, (size.height - sprite1Size.height)/2 - 20);
435 sprite11->setPosition(size.width/2 + 20, (size.height - sprite1Size.height)/2 - 20);
437 sprite2->setPosition((size.width-sprite1Size.width)/2 - 20, size.height/2 + 20);
438 sprite22->setPosition(size.width/2 + 20, size.height/2 + 20);
445 _rend = RenderTexture::create(200, 200, backend::PixelFormat::RGBA8888);
447 _rend->setKeepMatrix(
true);
448 Size pixelSize = Director::getInstance()->getWinSizeInPixels();
449 _rend->setVirtualViewport(Vec2(size.width/2-150, size.height/2-150),Rect(0,0,size.width,size.height),Rect(0,0,pixelSize.width,pixelSize.height));
451 _rend->beginWithClear(1, 0, 0, 1);
459 FiniteTimeAction* baseAction = MoveBy::create(1, Vec2(size.width,0.0f));
462 _spriteDraw->runAction(RepeatForever::create(Sequence::create
463 (baseAction,baseAction->reverse(),
nullptr)));
469 CC_SAFE_RELEASE(
_rend);
474 return "Render Texture Part Test";
479 return "Only Grabbing a sub region of fullscreen";
486 auto s = Director::getInstance()->getWinSize();
488 _spriteDS = Sprite::create(
"Images/fire.png");
490 _spriteDS->setPosition(Vec2(s.width * 0.25f, 0.0f));
495 _spriteDraw->setPosition(Vec2(s.width * 0.25f, 0.0f));
500 _rend = RenderTexture::create(s.width, s.height, backend::PixelFormat::RGBA4444, PixelFormat::D24S8);
502 _rend->setPosition(Vec2(s.width * 0.5f, s.height * 0.5f));
504 this->addChild(
_rend);
519 _rend->beginWithClear(0, 0, 0, 0, 0, 0);
542 Director::getInstance()->getRenderer()->setStencilWriteMask(0xFF);
548 auto renderer = Director::getInstance()->getRenderer();
549 renderer->setStencilTest(
true);
550 renderer->setStencilCompareFunction(backend::CompareFunction::NEVER, 1, 0xFF);
551 renderer->setStencilOperation(backend::StencilOperation::REPLACE, backend::StencilOperation::REPLACE, backend::StencilOperation::REPLACE);
556 Director::getInstance()->getRenderer()->setStencilCompareFunction(backend::CompareFunction::NOT_EQUAL, 1, 0xFF);
561 Director::getInstance()->getRenderer()->setStencilTest(
false);
566 return "Testing depthStencil attachment";
571 return "Circle should be missing 1/4 of its region";
589 auto background = LayerColor::create(Color4B(40,40,40,255));
590 addChild(background);
593 sprite1 = Sprite::create(
"Images/fire.png");
596 sprite2 = Sprite::create(
"Images/fire_rgba8888.pvr");
598 auto s = Director::getInstance()->getWinSize();
601 auto renderTexture = RenderTexture::create(s.width, s.height, backend::PixelFormat::RGBA4444);
622 auto menu = Menu::create(item,
nullptr);
625 menu->setPosition(Vec2(s.width/2, s.height/2));
638 renderTexture->setClearColor(Color4F( CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 1));
644 static float time = 0;
646 sprite1->setPosition(Vec2(cosf(time * 2) * r, sinf(time * 2) * r));
647 sprite2->setPosition(Vec2(sinf(time * 2) * r, cosf(time * 2) * r));
654 return "Testing Render Target Node";
659 return "Sprites should be equal and move with each frame";
667 CC_SAFE_RELEASE(_rt);
673 if (sprite && sprite->initWithFile(filename, rect))
675 sprite->autorelease();
679 CC_SAFE_DELETE(sprite);
689 auto s = Director::getInstance()->getWinSize();
690 _rt = RenderTexture::create(s.width, s.height, backend::PixelFormat::RGBA8888);
693 _rt->beginWithClear(0.0f, 0.0f, 0.0f, 1.0f);
696 Sprite::draw(renderer, transform, flags);
702 auto listener = EventListenerTouchAllAtOnce::create();
704 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
this);
706 auto s = Director::getInstance()->getWinSize();
712 int idx = CCRANDOM_0_1() * 1400 / 100;
713 int x = (idx%5) * 85;
714 int y = (idx/5) * 121;
720 sprite->setPosition(p);
722 FiniteTimeAction *action =
nullptr;
723 float rd = CCRANDOM_0_1();
726 action = ScaleBy::create(3, 2);
728 action = RotateBy::create(3, 360);
730 action = Blink::create(1, 3);
732 action = TintBy::create(2, 0, -255, -255);
734 action = FadeOut::create(2);
736 auto action_back = action->reverse();
737 auto seq = Sequence::create(action, action_back,
nullptr);
739 sprite->runAction(RepeatForever::create(seq));
747 for (
auto &touch: touches)
749 auto location = touch->getLocation();
756 return "SpriteRenderTextureBug";
761 return "Touch the screen. Sprite should appear on under the touch";
770 auto s = Director::getInstance()->getWinSize();
773 MenuItemFont::setFontSize(16);
774 auto item1 = MenuItemFont::create(
"Save Image", [&](Ref* ref){
775 auto winSize = Director::getInstance()->getVisibleSize();
776 auto text = Label::createWithTTF(
"hello world",
"fonts/Marker Felt.ttf", 40);
777 text->setTextColor(Color4B::RED);
778 auto target = RenderTexture::create(winSize.width, winSize.height, backend::PixelFormat::RGBA8888);
779 target->beginWithClear(0,0,0,0);
780 text->setPosition(winSize.width / 2,winSize.height/2);
783 auto callback = [&](RenderTexture* rt,
const std::string& path){
787 target->saveToFile(
"issue16113.png", Image::Format::PNG,
true, callback);
789 auto menu = Menu::create(item1,
nullptr);
790 this->addChild(menu);
791 menu->setPosition(s.width/2, s.height/2);
796 return "Github Issue 16113";
801 return "aaa.png file without white border on iOS";
809 auto visibleSize = Director::getInstance()->getVisibleSize();
810 Vec2 origin = Director::getInstance()->getVisibleOrigin();
812 for (
int i = 0; i < 3; ++i)
815 auto ship = Sprite3D::create(
"Sprite3DTest/boss.c3b");
817 ship->setRotation3D(Vec3(180.0f,45.0f,0.0f));
818 ship->setPosition(Vec2(visibleSize.width/4 + origin.x, visibleSize.height/2 + origin.y));
819 ship->setForce2DQueue(
true);
826 auto spin = RotateBy::create(4, Vec3(0.0f,180.0f,0.0f));
827 auto repeatspin = RepeatForever::create(spin);
828 ship->runAction(repeatspin);
834 _renderTexDefault = RenderTexture::create(visibleSize.width, visibleSize.height, backend::PixelFormat::RGBA8888);
835 _renderTexDefault->setKeepMatrix(
true);
836 addChild(_renderTexDefault);
837 _renderTexDefault->setPosition(visibleSize.width/4 * 3, visibleSize.height/2);
839 _renderTexWithBuffer = RenderTexture::create(visibleSize.width, visibleSize.height, backend::PixelFormat::RGBA8888, backend::PixelFormat::D24S8);
840 _renderTexWithBuffer->setKeepMatrix(
true);
841 addChild(_renderTexWithBuffer);
842 _renderTexWithBuffer->setPosition(visibleSize.width/4 * 4, visibleSize.height/2);
847 auto label1 = Label::createWithTTF(
"Normal Sprite3D\n",
"fonts/arial.ttf", 10);
848 label1->setPosition(Vec2(visibleSize.width/4 * 1, 60.0f));
849 this->addChild(label1, 1);
851 auto label2 = Label::createWithTTF(
"RenderTexture\nDefault - No depth buffer",
"fonts/arial.ttf", 10);
852 label2->setPosition(Vec2(visibleSize.width/4 * 2, 60.0f));
853 this->addChild(label2, 1);
855 auto label3 = Label::createWithTTF(
"RenderTexture\nGL_DEPTH24_STENCIL8",
"fonts/arial.ttf", 10);
856 label3->setPosition(Vec2(visibleSize.width/4 * 3, 60.0f));
857 this->addChild(label3, 1);
862 for (
int i = 0; i < 3; ++i)
870 RenderTextureTest::visit(renderer, parentTransform, parentFlags);
872 _ship[1]->setRotation3D(_ship[0]->getRotation3D());
873 _ship[2]->setRotation3D(_ship[0]->getRotation3D());
875 _renderTexDefault->beginWithClear(0, 0, 0, 0, 0, 0);
876 _ship[1]->visit(Director::getInstance()->getRenderer(), Mat4::IDENTITY, 0);
877 _renderTexDefault->end();
879 _renderTexWithBuffer->beginWithClear(0, 0, 0, 0, 1, 0);
880 _ship[2]->visit(Director::getInstance()->getRenderer(), Mat4::IDENTITY, 0);
881 _renderTexWithBuffer->end();
886 return "Issue16894: Render Sprite3D to texture";
891 return "3 ships, 1st & 3rd are the same";
#define ADD_TEST_CASE(__className__)
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual ~RenderTexturePartTest()
virtual std::string title() const override
cocos2d::RenderTexture * _rend
cocos2d::Sprite * _spriteDraw
cocos2d::Vector< cocos2d::Sprite * > _brushs
virtual std::string subtitle() const override
void clearImage(cocos2d::Ref *pSender)
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
cocos2d::RenderTexture * _target
void saveImageWithNonPremultipliedAlpha(cocos2d::Ref *pSender)
void saveImageWithPremultipliedAlpha(cocos2d::Ref *pSender)
virtual std::string title() const override
void addImage(cocos2d::Ref *sender)
void clearImageTransparent(cocos2d::Ref *sender)
cocos2d::Sprite * sprite1
virtual void update(float t) override
void touched(cocos2d::Ref *sender)
RenderTextureTargetNode()
virtual std::string subtitle() const override
cocos2d::Sprite * sprite2
virtual std::string title() const override
cocos2d::RenderTexture * renderTexture
virtual std::string title() const override
cocos2d::CallbackCommand _renderCmds[4]
virtual ~RenderTextureTestDepthStencil()
cocos2d::Sprite * _spriteDS
virtual std::string subtitle() const override
cocos2d::RenderTexture * _rend
virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override
cocos2d::Sprite * _spriteDraw
RenderTextureTestDepthStencil()
virtual std::string title() const override
RenderTextureWithSprite3DIssue16894()
virtual std::string subtitle() const override
virtual void visit(cocos2d::Renderer *renderer, const cocos2d::Mat4 &parentTransform, uint32_t parentFlags) override
virtual ~RenderTextureWithSprite3DIssue16894()
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
cocos2d::SpriteBatchNode * mgr
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string subtitle() const override
void onTouchesBegan(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual std::string title() const override
virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)
static SimpleSprite * create(const char *filename, const cocos2d::Rect &rect)
virtual std::string subtitle() const override
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
SimpleSprite * addNewSpriteWithCoords(const cocos2d::Vec2 &p)
virtual std::string title() const override
static cocos2d::Vec2 rightTop()