Cocos2d-x3.1TestCpp之NewRenderTest Demo分析
1、代码构成
VisibleRect.h
VisibleRect.cpp
AppDelegate.h
AppDelegate.cpp
HelloWorldScene.h
HelloWorldScene.cpp
NewRenderTest.h
NewRenderTest.cpp
2、HelloWorld代码
#include "cocos2d.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace ui; class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init(); // a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
void touchEvent(Ref *pSender, cocos2d::ui::Widget::TouchEventType type); // implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#include "HelloWorldScene.h"
#include "NewRenderTest.h"
USING_NS_CC; Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create(); // 'layer' is an autorelease object
auto layer = HelloWorld::create(); // add layer as a child to scene
scene->addChild(layer); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
} Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin(); /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it. // add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1); auto text = Text::create();
text->setString("click");
text->setFontSize(40);
text->setPosition(Vec2(200,200));
addChild(text);
text->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchEvent, this)); return true;
} void HelloWorld::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type)
{
switch (type) {
case cocos2d::ui::Widget::TouchEventType::ENDED:
{
auto temp = NewRendererDemo::create();
Director::getInstance()->replaceScene(temp);
break;
}
default:
break;
}
} void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
3、NewRendererTest代码
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "VisibleRect.h"
#include "../cocos2d/cocos/renderer/CCRenderer.h"
USING_NS_CC;
using namespace ui;
//宏定义,用于NewClippingNodeTest
#define kTagSpriteBatchNode 100
#define kTagClipperNode 101
#define kTagContentNode 102
//父类Scene。首先执行HelloWorld中的Scene,然后该Scene类replace HelloWorld使用的Scene
class NewRendererDemo : public Scene
{
public:
CREATE_FUNC(NewRendererDemo);
virtual bool init();
};
//父类Layer,该Layer载入NewRenderDemo所创建的Scene上
class BaseTest : public cocos2d::Layer
{
public:
CREATE_FUNC(BaseTest);
virtual std::string title() const;//标题
virtual std::string subtitle() const;//副标题 virtual void restartCallback(Ref* sender);//又一次执行当前test
virtual void nextCallback(Ref* sender);//下一个test
virtual void backCallback(Ref* sender);//上一个test virtual bool init();
void menuCloseCallback(cocos2d::Ref* pSender);//关闭菜单回调函数
};
//基类Layer,全部測试Demo均继承自该类,而该类继承自BaseTest。BaseTest继承自Layer
class MultiSceneTest : public BaseTest
{
public:
CREATE_FUNC(MultiSceneTest);
virtual std::string title() const;
virtual std::string subtitle() const; virtual void restartCallback(Ref* sender);
virtual void nextCallback(Ref* sender);
virtual void backCallback(Ref* sender);
};
//第一个測试
class NewSpriteTest : public MultiSceneTest
{
public:
CREATE_FUNC(NewSpriteTest);
virtual std::string title() const;
virtual std::string subtitle() const;
virtual bool init();
void createSpriteTest();
void createNewSpriteTest();
void onTouchesEnded(const std::vector<Touch*>& touches,Event* event);
};
//第二个測试
class GroupCommandTest : public MultiSceneTest
{
public:
CREATE_FUNC(GroupCommandTest);
virtual bool init();
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
//第三个測试
class NewSpriteBatchTest : public MultiSceneTest
{
public:
CREATE_FUNC(NewSpriteBatchTest);
virtual bool init();
virtual std::string title() const override;
virtual std::string subtitle() const override; void onTouchesEnded(const std::vector<Touch*>& touches,Event* event);
void addNewSpriteWithCoords(Vec2 p);
};
//第四个測试
class NewClippingNodeTest : public MultiSceneTest
{
public:
CREATE_FUNC(NewClippingNodeTest);
virtual bool init();
virtual std::string title() const override;
virtual std::string subtitle() const override; void onTouchesBegan(const std::vector<Touch*>& touches,Event* event);
void onTouchesMoved(const std::vector<Touch*>& touches,Event* event);
void onTouchesEnded(const std::vector<Touch*>& touches,Event* event); protected:
bool _scrolling;
Vec2 _lastPoint;
}; //第五个測试
class NewDrawNodeTest : public MultiSceneTest
{
public:
CREATE_FUNC(NewDrawNodeTest);
virtual bool init();
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
//第六个測试
class NewCullingTest : public MultiSceneTest
{
public:
CREATE_FUNC(NewCullingTest);
virtual std::string title() const override;
virtual std::string subtitle() const override;
virtual bool init();
protected:
bool onTouchBegan(Touch*,Event* event);
void onTouchMoved(Touch* touch,Event* event);
Vec2 _lastPos;
};
//第七个測试
class VBOFullTest : public MultiSceneTest
{
public:
CREATE_FUNC(VBOFullTest);
virtual bool init();
virtual std::string title() const override;
virtual std::string subtitle() const override;
};
#include "NewRenderTest.h"
#include "HelloWorldScene.h"
//宏定义,实现类的创建
#define CL(__className__) [](){ return __className__::create();}
#define CLN(__className__) [](){ auto obj = new __className__(); obj->autorelease(); return obj; }
//基类Layer。实现关闭按钮、下一个測试、当前測试、下一个測试菜单项的布局与事件响应
bool BaseTest::init()
{
bool bRet = true;
do{
CC_BREAK_IF(!Layer::init()); Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin(); /////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it. // add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(BaseTest::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + visibleSize.height - closeItem->getContentSize().height/2)); // create menu, it's an autorelease object
auto menu1 = Menu::create(closeItem, NULL);
menu1->setPosition(Vec2::ZERO);
this->addChild(menu1, 1); std::string str = title();
const char * pTitle = str.c_str();
TTFConfig ttfConfig("tahoma.ttf", 35);
auto label = Label::createWithTTF(ttfConfig,pTitle);
addChild(label, 9999);
label->setPosition( Vec2(VisibleRect::center().x, VisibleRect::top().y - 30) ); std::string strSubtitle = subtitle();
if( ! strSubtitle.empty() )
{
ttfConfig.fontFilePath = "tahoma.ttf";
ttfConfig.fontSize = 30;
auto l = Label::createWithTTF(ttfConfig,strSubtitle.c_str());
addChild(l, 9999);
l->setPosition( Vec2(VisibleRect::center().x, VisibleRect::top().y - 100) );
} auto item1 = MenuItemFont::create("backCallback", CC_CALLBACK_1(BaseTest::backCallback, this) );
auto item2 = MenuItemFont::create("restartCallback", CC_CALLBACK_1(BaseTest::restartCallback, this) );
auto item3 = MenuItemFont::create("nextCallback", CC_CALLBACK_1(BaseTest::nextCallback, this) ); auto menu = Menu::create(item1, item2, item3, NULL); menu->setPosition(Vec2::ZERO);
item1->setPosition(Vec2(VisibleRect::center().x - item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));
item2->setPosition(Vec2(VisibleRect::center().x, VisibleRect::bottom().y+item2->getContentSize().height/2));
item3->setPosition(Vec2(VisibleRect::center().x + item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2)); addChild(menu, 9999); bRet = true;
}while(0);
return bRet;
} void BaseTest::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
//标题
std::string BaseTest::title() const
{
return "";
}
//副标题
std::string BaseTest::subtitle() const
{
return "";
}
//又一次运行当前test
void BaseTest::restartCallback(Ref* sender)
{
log("override restart!");
}
//下一个test
void BaseTest::nextCallback(Ref* sender)
{
log("override next!");
}
//上一个test
void BaseTest::backCallback(Ref* sender)
{
log("override back!");
}
//索引,用于推断上一个或下一个
static int sceneIdx = -1;
//Layer* nextSpriteTestAction();//下一个
//Layer* backSpriteTestAction();//上一个
//Layer* restartSpriteTestAction();//当前
//函数指针数组
static std::function<Layer*()> createFunctions[] =
{
CL(NewSpriteTest),
CL(NewSpriteBatchTest),
CL(GroupCommandTest),
CL(NewClippingNodeTest),
CL(NewDrawNodeTest),
CL(NewCullingTest),
CL(VBOFullTest),
};
//获取数组的大小
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
//下一个
Layer* nextTest()
{
sceneIdx++;
sceneIdx = sceneIdx % MAX_LAYER;//循环判定
auto layer = (createFunctions[sceneIdx])();//调用函数指针数组中sceneIdx的函数
// layer->autorelease();
return layer;
}
//同上
Layer* prevTest()
{
sceneIdx--;
int total = MAX_LAYER;
if(sceneIdx < 0)
{
sceneIdx += total;
}
auto layer = (createFunctions[sceneIdx])();
// layer->autorelease();
return layer;
}
//同上
Layer* restartTest()
{
auto layer = (createFunctions[sceneIdx])();
// layer->autorelease();
return layer;
}
//基类Scene,各DemoTest的Layer的容器,每一个DemoTest分别使用一个Scene
bool NewRendererDemo::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!Scene::init()); auto layer = nextTest();//首先运行第一个test
addChild(layer); bRet = true;
}while(0);
return bRet;
}
//基类Layer
std::string MultiSceneTest::title() const
{
return "New Renderer";
} std::string MultiSceneTest::subtitle() const
{
return "MultiSceneTest";
}
//事件回调
void MultiSceneTest::restartCallback(cocos2d::Ref *sender)
{
auto s = new NewRendererDemo();//首先创建一个Scene。然后将当前的test
s->addChild(restartTest());//Layer载入到当前Scene
Director::getInstance()->replaceScene(s);//场景切换,第一个场景切换的是HelloWorld中创建的Scene
s->release();
}
//事件回调,原理同上
void MultiSceneTest::nextCallback(cocos2d::Ref *sender)
{
auto s = new NewRendererDemo();
s->addChild(nextTest());
Director::getInstance()->replaceScene(s);
s->release();
}
//事件回调,原理同上
void MultiSceneTest::backCallback(cocos2d::Ref *sender)
{
auto s = new NewRendererDemo();
s->addChild(prevTest());
Director::getInstance()->replaceScene(s);
s->release();
}
//第一个test
bool NewSpriteTest::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!MultiSceneTest::init()); log("MAX_LAYER = %lu",MAX_LAYER);
auto touchListener = EventListenerTouchAllAtOnce::create();//事件监听
touchListener->onTouchesEnded = CC_CALLBACK_2(NewSpriteTest::onTouchesEnded,this);
createSpriteTest();
createNewSpriteTest(); bRet = true;
}while(0);
return bRet;
} void NewSpriteTest::createSpriteTest()
{
auto winSize = Director::getInstance()->getWinSize(); Sprite* parent = Sprite::create("grossini.png");
parent->setPosition(winSize.width/4,winSize.height/2);
Sprite* child1 = Sprite::create("grossinis_sister1.png");
child1->setPosition(0.0f,-20.0f);
Sprite* child2 = Sprite::create("grossinis_sister2.png");
child2->setPosition(20.0f,-20.0f);
Sprite* child3 = Sprite::create("grossinis_sister1.png");
child3->setPosition(40.0f,-20.0f);
Sprite* child4 = Sprite::create("grossinis_sister2.png");
child4->setPosition(60.0f,-20.0f);
Sprite* child5 = Sprite::create("grossinis_sister2.png");
child5->setPosition(80.0f,-20.0f);
Sprite* child6 = Sprite::create("grossinis_sister2.png");
child6->setPosition(100.0f,-20.0f);
Sprite* child7 = Sprite::create("grossinis_sister2.png");
child7->setPosition(120.0f,-20.0f);
Sprite* child8 = Sprite::create("grossinis_sister2.png");
child8->setPosition(140.0f,-20.0f); parent->addChild(child1);
parent->addChild(child2);
parent->addChild(child3);
parent->addChild(child4);
parent->addChild(child5);
parent->addChild(child6);
parent->addChild(child7);
parent->addChild(child8);
addChild(parent);
} void NewSpriteTest::createNewSpriteTest()
{
auto winSize = Director::getInstance()->getWinSize(); Sprite* parent = Sprite::create("grossini.png");
parent->setPosition(winSize.width*2/3, winSize.height/2); Sprite* child1 = Sprite::create("grossinis_sister1.png");
child1->setPosition(0.0f,-20.0f);
Sprite* child2 = Sprite::create("grossinis_sister2.png");
child2->setPosition(20.0f,-20.0f);
Sprite* child3 = Sprite::create("grossinis_sister1.png");
child3->setPosition(40.0f,-20.0f);
Sprite* child4 = Sprite::create("grossinis_sister2.png");
child4->setPosition(60.0f,-20.0f);
Sprite* child5 = Sprite::create("grossinis_sister2.png");
child5->setPosition(80.0f,-20.0f);
Sprite* child6 = Sprite::create("grossinis_sister2.png");
child6->setPosition(100.0f,-20.0f);
Sprite* child7 = Sprite::create("grossinis_sister2.png");
child7->setPosition(120.0f,-20.0f);
Sprite* child8 = Sprite::create("grossinis_sister2.png");
child8->setPosition(140.0f,-20.0f); parent->addChild(child1);
parent->addChild(child2);
parent->addChild(child3);
parent->addChild(child4);
parent->addChild(child5);
parent->addChild(child6);
parent->addChild(child7);
parent->addChild(child8);
addChild(parent);
} void NewSpriteTest::onTouchesEnded(const std::vector<Touch *> &touches, cocos2d::Event *event)
{ } std::string NewSpriteTest::title() const
{
return "Renderer";
} std::string NewSpriteTest::subtitle() const
{
return "SpriteTest";
}
//工具类,通过finename创建Sprite对象
class SpriteInGroupCommand : public Sprite
{
protected:
GroupCommand _spriteWrapperCommand;
public:
static SpriteInGroupCommand* create(const std::string filename);//创建函数
virtual void draw(Renderer* renderer,const Mat4 &transform,bool transformUpdated) override;
}; SpriteInGroupCommand* SpriteInGroupCommand::create(const std::string filename)
{
SpriteInGroupCommand* sprite = new SpriteInGroupCommand();
sprite->initWithFile(filename);
sprite->autorelease();
return sprite;
}
void SpriteInGroupCommand::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, bool transformUpdated)
{
CCASSERT(renderer, "Renderer is null");
_spriteWrapperCommand.init(_globalZOrder);
renderer->addCommand(&_spriteWrapperCommand);
renderer->pushGroup(_spriteWrapperCommand.getRenderQueueID());
Sprite::draw(renderer, transform, transformUpdated);
renderer->popGroup();
}
//第二个test
bool GroupCommandTest::GroupCommandTest::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!MultiSceneTest::init()); auto sprite = SpriteInGroupCommand::create("grossini.png");//使用工具类创建对象
Size winSize = Director::getInstance()->getWinSize();
sprite->setPosition(Vec2(winSize.width/2,winSize.height/2));
addChild(sprite);
bRet = true;
}while(0);
return bRet;
}
std::string GroupCommandTest::title() const
{
return "Renderer";
}
std::string GroupCommandTest::subtitle() const
{
return "GroupCommandTest: You should see a sprite";
}
//第三个test
bool NewSpriteBatchTest::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!MultiSceneTest::init());
//事件监听
auto touchListener = EventListenerTouchAllAtOnce::create();
touchListener->onTouchesEnded = CC_CALLBACK_2(NewSpriteBatchTest::onTouchesEnded,this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);//加入到事件分发器
auto batchNode = SpriteBatchNode::create("grossini_dance_atlas.png");//使用SpriteBatchNode创建对象
addChild(batchNode,0,kTagSpriteBatchNode);
bRet = true;
}while(0);
return bRet;
} std::string NewSpriteBatchTest::title() const
{
return "Renderer";
}
std::string NewSpriteBatchTest::subtitle() const
{
return "SpriteBatchTest";
}
void NewSpriteBatchTest::onTouchesEnded(const std::vector<Touch *> &touches, cocos2d::Event *event)
{
for(auto &touch : touches)
{
auto location = touch->getLocation();
addNewSpriteWithCoords(location);
}
} void NewSpriteBatchTest::addNewSpriteWithCoords(cocos2d::Vec2 p)
{
auto batchNode = static_cast<SpriteBatchNode*>(getChildByTag(kTagSpriteBatchNode)); int idx = (int)(CCRANDOM_0_1() * 1400 / 100);
log("idx = %d",idx);
int x = (idx%5)*85;
int y = (idx%5)*121; auto sprite = Sprite::createWithTexture(batchNode->getTexture(),Rect(x,y,85,121));//随机获取资源文件里的Sprite对象
batchNode->addChild(sprite);
sprite->setPosition(Vec2(p.x,p.y));
//随机运行动作
ActionInterval* action;
float random = CCRANDOM_0_1();
if(random < 0.20)
action = ScaleBy::create(3, 2);
else if (random < 0.40)
action = RotateBy::create(3, 360);
else if (random < 0.60)
action = Blink::create(1, 3);
else if (random < 0.8)
action = TintBy::create(2, 0, -255, -255);
else
action = FadeOut::create(2); auto action_back = action->reverse();
auto seq = Sequence::create(action,action_back, NULL);
sprite->runAction(RepeatForever::create(seq));
}
//第四个test
bool NewClippingNodeTest::init()
{
bool bRet;
do{
CC_BREAK_IF(!MultiSceneTest::init()); auto winSize = Director::getInstance()->getWinSize();
auto clipper = ClippingNode::create();//创建剪裁对象
clipper->setTag(kTagClipperNode);
clipper->setContentSize(Size(200, 200));//设置setContentSize(),点击该区域就可以实现content的移动,它的size与模板stencil的Size没有关系。仅仅要点击区域在clipper设置的ContentSize内就可以拖动content的图片,好像。设置的contentSize越大,content图片越小。 不知道为什么?
clipper->ignoreAnchorPointForPosition(false);
clipper->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
clipper->setPosition(Vec2(winSize.width/2,winSize.height/2)); clipper->runAction(RepeatForever::create(RotateBy::create(1, 45)));//旋转
addChild(clipper); clipper->setAlphaThreshold(0.05f);//属性设置
auto stencil = Sprite::create("grossini.png");
stencil->ignoreAnchorPointForPosition(false);
stencil->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
clipper->setStencil(stencil);//模板设置 //模板也可为DrawNode,可凝视掉上面stencil的定义,取消此处凝视。查看效果
// auto stencil = DrawNode::create();
// Vec2 rectangle[4];
// rectangle[0] = Vec2(0, 0);
// rectangle[1] = Vec2(clipper->getContentSize().width, 0);
// rectangle[2] = Vec2(clipper->getContentSize().width, clipper->getContentSize().height);
// rectangle[3] = Vec2(0, clipper->getContentSize().height);
//
// Color4F white(1, 1, 1, 1);
// stencil->drawPolygon(rectangle, 4, white, 1, white);
// clipper->setStencil(stencil); //设置剪裁检点要显示的内容
auto content = Sprite::create("HelloWorld.png");
content->setTag(kTagContentNode);
content->ignoreAnchorPointForPosition(false);
content->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
content->setPosition(Vec2(clipper->getContentSize().width/2,clipper->getContentSize().height/2));
clipper->addChild(content); _scrolling = false;
//此处为測试用的。与demo无关
auto sprite = Sprite::create("grossini.png");
sprite->setPosition(Vec2(winSize.width/4,winSize.height/2));
addChild(sprite);
//多点触摸事件处理
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(NewClippingNodeTest::onTouchesBegan,this);
listener->onTouchesMoved = CC_CALLBACK_2(NewClippingNodeTest::onTouchesMoved,this);
listener->onTouchesEnded = CC_CALLBACK_2(NewClippingNodeTest::onTouchesEnded,this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); bRet = true;
}while(0);
return bRet;
} void NewClippingNodeTest::onTouchesBegan(const std::vector<Touch *> &touches, cocos2d::Event *event)
{
Touch* touch = touches[0];
auto clipper = this->getChildByTag(kTagClipperNode);
Vec2 point = clipper->convertToNodeSpace(Director::getInstance()->convertToGL(touch->getLocation()));
log("point.w = %f,point.h = %f",point.x,point.y);
auto rect = Rect(0,0,clipper->getContentSize().width,clipper->getContentSize().height);
log("clipper.w = %f,clipper.h = %f",clipper->getContentSize().width,clipper->getContentSize().height);
_scrolling = rect.containsPoint(point);
_lastPoint = point;
}
//使content图片尾随触摸点移动
void NewClippingNodeTest::onTouchesMoved(const std::vector<Touch *> &touches, cocos2d::Event *event)
{
if(!_scrolling) return;
Touch* touch = touches[0];
auto clipper = this->getChildByTag(kTagClipperNode);
auto point = clipper->convertToNodeSpace(Director::getInstance()->convertToGL(touch->getLocationInView()));
Vec2 diff = point - _lastPoint;
auto content = clipper->getChildByTag(kTagContentNode);
content->setPosition(content->getPosition() + diff);
_lastPoint = point;
} void NewClippingNodeTest::onTouchesEnded(const std::vector<Touch *> &touches, cocos2d::Event *event)
{
if(!_scrolling) return;
_scrolling = false;
} std::string NewClippingNodeTest::title() const
{
return "New Renderer";
} std::string NewClippingNodeTest::subtitle() const
{
return "ClipNode";
} //第五个test
bool NewDrawNodeTest::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!MultiSceneTest::init()); auto winSize = Director::getInstance()->getWinSize();
auto parent = Node::create();
// parent->setPosition(winSize.width/2,winSize.height/2);
addChild(parent); auto rectNode = DrawNode::create();
Vec2 rectangle[4];
rectangle[0] = Vec2(-50,-50);
rectangle[1] = Vec2(50,-50);
rectangle[2] = Vec2(50,50);
rectangle[3] = Vec2(-50,50); Color4F white(1,1,1,1);
rectNode->drawPolygon(rectangle, 4, white, 1, white);//使用DrawNode创建对象
rectNode->setPosition(winSize.width/4,winSize.height/4);
parent->addChild(rectNode); bRet = true;
}while(0);
return bRet;
} std::string NewDrawNodeTest::title() const
{
return "New Renderer";
} std::string NewDrawNodeTest::subtitle() const
{
return "DrawNode";
}
//第六个test
bool NewCullingTest::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!MultiSceneTest::init()); auto winSize = Director::getInstance()->getWinSize();
auto sprite = Sprite::create("btn-about-normal-vertical.png");
sprite->setRotation(5);
sprite->setPosition(Vec2(winSize.width/2,winSize.height/3));
sprite->setScale(2);
addChild(sprite); auto sprite2 = Sprite::create("btn-about-normal-vertical.png");
sprite2->setRotation(-85);
sprite2->setPosition(Vec2(winSize.width/2,winSize.height*2/3));
sprite2->setScale(2);
addChild(sprite2);
//触摸事件处理
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(NewCullingTest::onTouchBegan,this);
listener->onTouchMoved = CC_CALLBACK_2(NewCullingTest::onTouchMoved, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
bRet = true;
}while(0);
return bRet;
} bool NewCullingTest::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
{
auto pos = touch->getLocation();
_lastPos = pos;
return true;
}
//移动Layer
void NewCullingTest::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event)
{
auto pos = touch->getLocation();
auto offset = pos - _lastPos;
auto layerPos = getPosition();
auto newPos = layerPos + offset;
setPosition(newPos);
_lastPos = pos;
} std::string NewCullingTest::title() const
{
return "New Render";
}
std::string NewCullingTest::subtitle() const
{
return "Drag the layer to test the result of culling";
}
//该測试没发现使用的意义
bool VBOFullTest::init()
{
bool bRet = false;
do{
CC_BREAK_IF(!MultiSceneTest::init()); auto winSize = Director::getInstance()->getWinSize();
Node* parent = Node::create();
parent->setPosition(Vec2(winSize.width/2, winSize.height/2));
addChild(parent);
log("Renderer::VBO_SIZE = %d",Renderer::VBO_SIZE);
for(int i = 0; i < Renderer::VBO_SIZE * 2; ++i)
{
Sprite* sprite = Sprite::create("grossini_dance_01.png");
sprite->setPosition(Vec2(i,0));
parent->addChild(sprite);
} bRet = true;
}while(0);
return bRet;
} std::string VBOFullTest::title() const
{
return "New Renderer";
} std::string VBOFullTest::subtitle() const
{
return "VBO full test,everything should render normally";
}
4、VisibleRect代码
该代码即testcppproject给出的代码。不再赘述。
Cocos2d-x3.1TestCpp之NewRenderTest Demo分析的更多相关文章
- qml demo分析(threadedanimation-线程动画)
一.效果预览 使用过qml的同学都知道,使用qml做动画效果是非常简单的,再也不需要像QWidget那样,自己模拟一个动画,费时又费力,往往还达不到效果.今天我们就来分析下qml的两种动画实现方式,如 ...
- qml demo分析(maskedmousearea-异形窗口)
一.效果展示 如本文的标题所示,这篇文章分析的demo是一个异形窗口,主要展示鼠标在和异形区域交互的使用,效果如图1所示,当鼠标移动到白云或者月亮上时,相应的物体会高亮,当鼠标按下时,物体会有一个放大 ...
- 【RxJava Demo分析】(二)Schedulers线程调度器 · Hans Zone
用Schedulers(调度器)实现多任务(并发,Concurrency)的例子 废话不多说我们看一下有关于RxJava的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- google closure--继承模块二:goog.base()demo分析
昨天已经讲到了goog.inherits(),主要负责通过为子构造函数原型对象通过原型链继承父构造函数的原型对象的方法,完成继承.这样继承只完成了原型对象的继承,看看之前的那张图: 是不是感觉父构造函 ...
- 阿里react整合库dva demo分析
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 24.0px "Helvetica Neue"; color: #404040 } p. ...
- qml demo分析(maroon-小游戏)
1.效果展示 这篇文章我还是分析一个qt源码中的qml程序,程序运行效果如下图所示. 图1 游戏开始 图2 游戏中 2.源码分析 这个游戏的源码文件比较多,为了能更清楚的了解整个代码,我先整体分析 ...
- qml demo分析(abstractitemmodel-数据分离)
一.概述 qt5之后qml也可以被用于桌面程序开发,今天我就拿出qt demo中的一个qml示例程序进行分析.这个demo主要是展示了qml数据和展示分离的使用方式,qml只专注于快速高效的绘制界面, ...
- (原创)cocos2dx-lua TableView官方demo分析
本来是想看看网上的教程文章,结果看了好几篇,复制代码各种报错,有很多不存在的类和变量,根本用不了. 所以干脆自己去看官方demo,经过自己分析测试,已经大概会用了,顺便记录一下. 以下是代码,复制粘贴 ...
- windows环境libevent搭建和demo分析
libevent框架之前有做过分析,这次是谈谈如何将libevent搭建在vs工作环境下, 并且编写一个demo进行测试.测试过程中会再一次带大家分析消息是怎么传递 的. 我的libevent版本li ...
随机推荐
- CRT/LCD/VGA Information and Timing【转】
转自:http://www.cnblogs.com/shangdawei/p/4760933.html 彩色阴极射线管的剖面图: 1. 电子QIANG Three Electron guns (for ...
- NEERC Southern Subregional 2012
NEERC Southern Subregional 2012 Problem B. Chess Championship 题目描述:有两个序列\(a, b\),两个序列都有\(n\)个数,并且这\( ...
- selenium用jquery改变元素属性
一.jQuery 语法 jQuery 语法是通过选取 HTML 元素,并对选取的元素执行某些操作. 1.基础语法: $(selector).action() 选择符(selector)即," ...
- Java中包的介绍
包的介绍: 未命名包 命名包 可以避免类名重复 为了更好地组织类,Java 提供了包机制,用于区别类名的命名空间. 包的作用 1.把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用. 2. ...
- Win7系统Chrome浏览器缓存查看技巧介绍(转)
1.Chrome下提供了一个命令chrome://cache,可以查看到保留下来的缓存; 2.但是,当你点击缓存文件,Chrome却并非打开缓存源文件,而是如图所示的二进制编码文件; 3.在Win7系 ...
- React 与 Redux 在生产环境中的实践总结
React 与 Redux 在生产环境中的实践总结 前段时间使用 React 与 Redux 重构了我们360netlab 的 开放数据平台.现将其中一些技术实践经验总结如下: Universal 渲 ...
- fiddler抓https 关于证书一项
由于我之前电脑装过fiddler然后这次弄证书也没有理解明白,导致失败了.然后就百度到了下面的教程. https://www.cnblogs.com/joshua317/p/8670923.html ...
- CentOS 7 之 Docker 安装及操作命令
Docker 安装 官方网站上有各种环境下的安装指南,比如:CentOS.Ubuntu 和 Debian 系列的安装. 而我们现在主要介绍的是基于 CentOS 7.x 上面的安装. 1.查看是否已经 ...
- H5的简介
1.H5的诞生 2.介绍 HTML5 将成为 HTML.XHTML 以及 HTML DOM 的新标准. HTML5 是 W3C 与 WHATWG 合作的结果. WHATWG 致力于 web 表单和应用 ...
- PHP函数之trigger_error
在程序开发中,如果我们编码不规范,比如调用不存在的变量.语法错误.少了个逗号,这些都会引起系统报错并进行提示,但是今天,突然发现PHP还有这样一个函数,用于自动触发一个报错提示,并且会将报错信息写入p ...