cocos2dx GL 效果工具代码

cocos2dx GL 效果工具代码_第1张图片


#ifndef _PADDLE_H_
#define _PADDLE_H_

#include "cocos2d.h"

USING_NS_CC;

typedef enum tagPaddleState 
{
	kPaddleStateGrabbed,
	kPaddleStateUngrabbed
} PaddleState; 

class Paddle : public Sprite, public Clonable
{
	PaddleState        _state;

public:
	Paddle(void);
	virtual ~Paddle(void);

	Rect getRect();
	bool initWithTexture(Texture2D* aTexture);
	virtual void onEnter() override;
	virtual void onExit() override;
	bool containsTouchLocation(Touch* touch);
	bool onTouchBegan(Touch* touch, Event* event);
	void onTouchMoved(Touch* touch, Event* event);
	void onTouchEnded(Touch* touch, Event* event);
	virtual Paddle* clone() const;

	static Paddle* createWithTexture(Texture2D* aTexture);

	Paddle* muxPaddle;
	void setMuxPaddle( Paddle* paddle);
};

#endif

#include "Paddle.h"

Paddle::Paddle(void)
{
}

Paddle::~Paddle(void)
{
}

Rect Paddle::getRect()
{
	auto s = getTexture()->getContentSize();
	return Rect(-s.width / 2, -s.height / 2, s.width, s.height);
}

Paddle* Paddle::createWithTexture(Texture2D* aTexture)
{
	Paddle* pPaddle = new Paddle();
	pPaddle->initWithTexture(aTexture);
	pPaddle->autorelease();

	return pPaddle;
}

bool Paddle::initWithTexture(Texture2D* aTexture)
{
	if( Sprite::initWithTexture(aTexture) )
	{
		_state = kPaddleStateUngrabbed;
	}

	return true;
}

void Paddle::onEnter()
{
	Sprite::onEnter();

	// Register Touch Event
	auto listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);

	listener->onTouchBegan = CC_CALLBACK_2(Paddle::onTouchBegan, this);
	listener->onTouchMoved = CC_CALLBACK_2(Paddle::onTouchMoved, this);
	listener->onTouchEnded = CC_CALLBACK_2(Paddle::onTouchEnded, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}

void Paddle::onExit()
{
	//    auto director = Director::getInstance();
	//    director->getTouchDispatcher()->removeDelegate(this);
	Sprite::onExit();
}    

bool Paddle::containsTouchLocation(Touch* touch)
{
	return getRect().containsPoint(convertTouchToNodeSpaceAR(touch));
}

bool Paddle::onTouchBegan(Touch* touch, Event* event)
{
	CCLOG("Paddle::onTouchBegan id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);

	this->setZOrder(1);
	muxPaddle->setZOrder(0);
	if (_state != kPaddleStateUngrabbed) return false;
	if ( !containsTouchLocation(touch) ) return false;

	_state = kPaddleStateGrabbed;
	CCLOG("return true");
	return true;
}

void Paddle::onTouchMoved(Touch* touch, Event* event)
{
	// If it weren't for the TouchDispatcher, you would need to keep a reference
	// to the touch from touchBegan and check that the current touch is the same
	// as that one.
	// Actually, it would be even more complicated since in the Cocos dispatcher
	// you get Sets instead of 1 UITouch, so you'd need to loop through the set
	// in each touchXXX method.

	CCLOG("Paddle::onTouchMoved id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);

	CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");    

	auto touchPoint = touch->getLocation();

	setPosition( Point(touchPoint.x,touchPoint.y) );
}

Paddle* Paddle::clone() const
{
	Paddle* ret = Paddle::createWithTexture(_texture);
	ret->_state = _state;
	ret->setPosition(getPosition());
	ret->setAnchorPoint(getAnchorPoint());
	return ret;
}

void Paddle::onTouchEnded(Touch* touch, Event* event)
{
	CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");    

	_state = kPaddleStateUngrabbed;
} 

void Paddle::setMuxPaddle( Paddle* paddle )
{
	muxPaddle = paddle;
}

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "Paddle.h"
class HelloWorld : public cocos2d::Layer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* scene();
    
    // a selector callback
    void menuCloseCallback(Ref* sender);
    
    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
	void menuCallbackBall1S(Ref * sender);
	void menuCallbackBall1D(Ref * sender);
	void menuCallbackBall2S(Ref * sender);
	void menuCallbackBall2D(Ref * sender);
	int ball1S;
	int ball1D;
	int ball2S;
	int ball2D;

	Paddle* ball1;
	Paddle* ball2;

	Menu* _itemMenuB1S;
	Menu* _itemMenuB1D;
	Menu* _itemMenuB2S;
	Menu* _itemMenuB2D;
};

#endif // __HELLOWORLD_SCENE_H__

#include "HelloWorldScene.h"


USING_NS_CC;
#define LINE_SPACE          15
typedef struct _Controller{
	const char *test_name;
	int gl;
} Controller;
Controller g_aTestNames[] = {
	{ "GL_NOR", GL_NOR  },
	{ "GL_ZERO", GL_ZERO  },
	{ "GL_ONE", GL_ONE },

	{ "GL_DST_COLOR", GL_DST_COLOR },
	{ "GL_SRC_COLOR", GL_SRC_COLOR },

	{ "GL_ONE_MINUS_DST_COLOR", GL_ONE_MINUS_DST_COLOR },
	{ "GL_ONE_MINUS_SRC_COLOR", GL_ONE_MINUS_SRC_COLOR },

	{ "GL_DST_ALPHA", GL_DST_ALPHA },
	{ "GL_SRC_ALPHA", GL_SRC_ALPHA },

	{ "GL_SRC_ALPHA_SATURATE", GL_SRC_ALPHA_SATURATE },

	{ "GL_ONE_MINUS_DST_ALPHA", GL_ONE_MINUS_DST_ALPHA },
	{ "GL_ONE_MINUS_SRC_ALPHA", GL_ONE_MINUS_SRC_ALPHA },

};

static int g_testCount = sizeof(g_aTestNames) / sizeof(g_aTestNames[0]);
Scene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *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;
    }
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    auto 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
	Sprite* bg = Sprite::create("UI_frame_1.png");
	bg->setRotation(90);
	bg->setScale(0.4);
	bg->setPosition(ccp(visibleSize.width/2,visibleSize.height/3));
	this->addChild(bg);
    auto closeItem = MenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
    
    closeItem->setPosition(origin + Point(visibleSize) - Point(closeItem->getContentSize() / 2));

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu, 1);


	_itemMenuB1S = Menu::create();
	for (int i = 0; i < g_testCount; ++i)
	{
		// #if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
		//         auto label = LabelBMFont::create(g_aTestNames[i].c_str(),  "fonts/arial16.fnt");
		// #else
		auto label = LabelTTF::create( g_aTestNames[i].test_name, "Arial", 10);
		// #endif        
		auto menuItem = MenuItemLabel::create(label, CC_CALLBACK_1(HelloWorld::menuCallbackBall1S, this));
		if (g_aTestNames[i].test_name=="GL_SRC_ALPHA")
		{
			menuItem->setColor(Color3B(255,100,100));
		}
		_itemMenuB1S->addChild(menuItem, i + 10000,i + 10000);
		menuItem->setPosition( Point(100, (Director::getInstance()->getWinSize().height - (i + 1) * LINE_SPACE) ));
	}

	//_itemMenu->setContentSize(Size(VisibleRect::getVisibleRect().size.width, (g_testCount + 1) * (LINE_SPACE)));
	_itemMenuB1S->setPosition(Point(0,0));;
	addChild(_itemMenuB1S);




	_itemMenuB1D = Menu::create();
	for (int i = 0; i < g_testCount; ++i)
	{
		// #if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
		//         auto label = LabelBMFont::create(g_aTestNames[i].c_str(),  "fonts/arial16.fnt");
		// #else
		auto label = LabelTTF::create( g_aTestNames[i].test_name, "Arial", 10);
		// #endif        
		auto menuItem = MenuItemLabel::create(label, CC_CALLBACK_1(HelloWorld::menuCallbackBall1D, this));
		if (g_aTestNames[i].test_name=="GL_ONE_MINUS_SRC_ALPHA")
		{
			menuItem->setColor(Color3B(255,100,100));
		}
		_itemMenuB1D->addChild(menuItem, i + 10000,i + 10000);
		menuItem->setPosition( Point(300, (Director::getInstance()->getWinSize().height - (i + 1) * LINE_SPACE) ));
	}

	//_itemMenu->setContentSize(Size(VisibleRect::getVisibleRect().size.width, (g_testCount + 1) * (LINE_SPACE)));
	_itemMenuB1D->setPosition(Point(0,0));;
	addChild(_itemMenuB1D);

	_itemMenuB2S = Menu::create();
	for (int i = 0; i < g_testCount; ++i)
	{
		// #if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
		//         auto label = LabelBMFont::create(g_aTestNames[i].c_str(),  "fonts/arial16.fnt");
		// #else
		auto label = LabelTTF::create( g_aTestNames[i].test_name, "Arial", 10);
		// #endif        
		auto menuItem = MenuItemLabel::create(label, CC_CALLBACK_1(HelloWorld::menuCallbackBall2S, this));
		if (g_aTestNames[i].test_name=="GL_SRC_ALPHA")
		{
			menuItem->setColor(Color3B(255,100,100));
		}
		_itemMenuB2S->addChild(menuItem, i + 10000,i + 10000);
		menuItem->setPosition( Point(500, (Director::getInstance()->getWinSize().height - (i + 1) * LINE_SPACE) ));
	}

	//_itemMenu->setContentSize(Size(VisibleRect::getVisibleRect().size.width, (g_testCount + 1) * (LINE_SPACE)));
	_itemMenuB2S->setPosition(Point(0,0));;
	addChild(_itemMenuB2S);

	_itemMenuB2D = Menu::create();
	for (int i = 0; i < g_testCount; ++i)
	{
		// #if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
		//         auto label = LabelBMFont::create(g_aTestNames[i].c_str(),  "fonts/arial16.fnt");
		// #else
		auto label = LabelTTF::create( g_aTestNames[i].test_name, "Arial", 10);
		
		
		// #endif        
		auto menuItem = MenuItemLabel::create(label, CC_CALLBACK_1(HelloWorld::menuCallbackBall2D, this));
		if (g_aTestNames[i].test_name=="GL_ONE_MINUS_SRC_ALPHA")
		{
			menuItem->setColor(Color3B(255,100,100));
		}
		_itemMenuB2D->addChild(menuItem, i + 10000,i + 10000);
		menuItem->setPosition( Point(700, (Director::getInstance()->getWinSize().height - (i + 1) * LINE_SPACE) ));
	}

	//_itemMenu->setContentSize(Size(VisibleRect::getVisibleRect().size.width, (g_testCount + 1) * (LINE_SPACE)));
	_itemMenuB2D->setPosition(Point(0,0));;
	addChild(_itemMenuB2D);

	
	BlendFunc b ={GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA};
    ball1S = GL_SRC_ALPHA;
	ball1D = GL_ONE_MINUS_SRC_ALPHA;
	ball2S = GL_SRC_ALPHA;
	ball2D = GL_ONE_MINUS_SRC_ALPHA;

	Texture2D* ball1Texture =Director::getInstance()->getTextureCache()->addImage("r1.png");
	ball1 = Paddle::createWithTexture(ball1Texture);
	ball1->setScale(3);
	ball1->setBlendFunc(b);
	ball1->setPosition(CCPoint(100,100));

	Texture2D* ball2Texture =Director::getInstance()->getTextureCache()->addImage("r2.png");
	ball2 = Paddle::createWithTexture(ball2Texture);
	ball2->setScale(3);
	ball2->setBlendFunc(b);
	ball2->setPosition(CCPoint(300,100));

	ball1->setMuxPaddle(ball2);
	ball2->setMuxPaddle(ball1);
	this->addChild(ball1);
	this->addChild(ball2);
    
    return true;
}

void HelloWorld::menuCallbackBall1S(Ref * sender)
{
	Director::getInstance()->purgeCachedData();
	
	// get the userdata, it's the index of the menu item clicked
	for (int i = 0; i < g_testCount; ++i)
	{
		MenuItemLabel* menuChild = static_cast<MenuItemLabel*>(_itemMenuB1S->getChildByTag(i + 10000));
		menuChild->setColor(Color3B(255,255,255));
	}

	auto menuItem = static_cast<MenuItem *>(sender);

	

	menuItem->setColor(Color3B(255,100,100));
	int idx = menuItem->getLocalZOrder() - 10000;

	// create the test scene and run it
	ball1S = g_aTestNames[idx].gl;
	ccBlendFunc blendfunc = {ball1S,ball1D};
	ball1->setBlendFunc(blendfunc);
	
}
void HelloWorld::menuCallbackBall1D( Ref * sender )
{
	Director::getInstance()->purgeCachedData();

	// get the userdata, it's the index of the menu item clicked
	auto menuItem = static_cast<MenuItem *>(sender);

	for (int i = 0; i < g_testCount; ++i)
	{
		MenuItemLabel* menuChild = static_cast<MenuItemLabel*>(_itemMenuB1D->getChildByTag(i + 10000));
		menuChild->setColor(Color3B(255,255,255));
	}
	menuItem->setColor(Color3B(255,100,100));
	int idx = menuItem->getLocalZOrder() - 10000;

	// create the test scene and run it
	ball1D = g_aTestNames[idx].gl;
	ccBlendFunc blendfunc = {ball1S,ball1D};
	ball1->setBlendFunc(blendfunc);
}

void HelloWorld::menuCallbackBall2S( Ref * sender )
{
	Director::getInstance()->purgeCachedData();

	// get the userdata, it's the index of the menu item clicked
	auto menuItem = static_cast<MenuItem *>(sender);

	for (int i = 0; i < g_testCount; ++i)
	{
		MenuItemLabel* menuChild = static_cast<MenuItemLabel*>(_itemMenuB2S->getChildByTag(i + 10000));
		menuChild->setColor(Color3B(255,255,255));
	}
	menuItem->setColor(Color3B(255,100,100));
	int idx = menuItem->getLocalZOrder() - 10000;

	// create the test scene and run it
	ball2S = g_aTestNames[idx].gl;
	ccBlendFunc blendfunc = {ball2S,ball2D};
	ball2->setBlendFunc(blendfunc);
}

void HelloWorld::menuCallbackBall2D( Ref * sender )
{
	Director::getInstance()->purgeCachedData();

	// get the userdata, it's the index of the menu item clicked
	auto menuItem = static_cast<MenuItem *>(sender);

	for (int i = 0; i < g_testCount; ++i)
	{
		MenuItemLabel* menuChild = static_cast<MenuItemLabel*>(_itemMenuB2D->getChildByTag(i + 10000));
		menuChild->setColor(Color3B(255,255,255));
	}
	menuItem->setColor(Color3B(255,100,100));
	int idx = menuItem->getLocalZOrder() - 10000;

	// create the test scene and run it
	ball2D = g_aTestNames[idx].gl;
	ccBlendFunc blendfunc = {ball2S,ball2D};
	ball2->setBlendFunc(blendfunc);
}
void HelloWorld::menuCloseCallback(Ref* sender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}


你可能感兴趣的:(cocos2dx GL 效果工具代码)