您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx3.0图像拼接(像素匹配)

2014-05-04 23:45 375 查看


主逻辑类

#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();
 
    CREATE_FUNC(HelloWorld);
  	void menuCallbackMatching(Ref * sender);
	void createMenuControl( );

	std::vector<std::string> itemTextVector;
	std::vector<MenuItemLabel*> menuItemVector;
  	Menu* _matchingMenu;
	Paddle* sp1;
	Paddle* sp2;
	int width1 ;
	int height1;
	int width2 ;
	int height2;
	int x1;
	int y1;
	int x2;
	int y2;

};

#endif // __HELLOWORLD_SCENE_H__


#include "HelloWorldScene.h"
#include <functional>

USING_NS_CC;
#define  LINE_SPACE 40

//int HelloWorld::num = 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();

	CCImage * image1 = new CCImage();
	//加载 图片
	image1->initWithImageFile("image.png");
	width1 = image1->getWidth();
	height1 = image1->getHeight();
	unsigned char *data_1=image1->getData();
	unsigned int *pixelOrign1 = (unsigned int *)data_1;
	unsigned int *pixel1 = NULL;

	CCImage * image2 = new CCImage();
	//加载 图片
	image2->initWithImageFile("image2.png");
	width2 = image2->getWidth();
	height2 = image2->getHeight();
	unsigned char *data_2=image2->getData();
	unsigned int *pixelOrign2 = (unsigned int *)data_2;
	unsigned int *pixel2 = NULL;

	for (int i = 0; i< width1* height1; i++)
	{
		pixel1 = pixelOrign1 +i;        
		for (int j = 0; j< width2* height2; j++)
		{
			pixel2 = pixelOrign2 +j;        

			if (*pixel1 == *pixel2&&*(pixel1+1) == *(pixel2+1)&&*(pixel1+width1) == *(pixel2+width2))
			{
				x1 = i%width1;
				y1 = i/width1;

				x2 = j%width2;
				y2 = j/width2;
				log("%d, %d", i, j);
				goto Break;
			}
			
		}
	}
Break:

 	Texture2D* imageTexture =  TextureCache::getInstance()->addImage("image.png");
 	Texture2D* imageTexture2 =  TextureCache::getInstance()->addImage("image2.png");

	Size winSize = Director::getInstance()->getWinSize();
	sp1 = Paddle::createWithTexture(imageTexture);
	sp1->setAnchorPoint(Point(0,0));
	sp1->setPosition(Point(winSize.width/2, winSize.height/2));
	
	

	sp2 = Paddle::createWithTexture(imageTexture2);
	sp2->setAnchorPoint(Point(0,0));
	sp2->setPosition(Point(winSize.width/2, winSize.height/2));

	sp1->setMuxPaddle(sp2);
	sp2->setMuxPaddle(sp1);

	this->addChild(sp1);
	this->addChild(sp2);

	itemTextVector.push_back("matching");

 	createMenuControl();
    return true;
}

void HelloWorld::createMenuControl()
{
	_matchingMenu = Menu::create();
	for (int i = 0; i < itemTextVector.size(); ++i)
	{
		auto label = LabelTTF::create( itemTextVector[i].c_str(), "Arial", 40);     
		auto menuItem = MenuItemLabel::create(label,std::bind(&HelloWorld::menuCallbackMatching,this, std::placeholders::_1));
// 		if (i == 0)
// 		{
// 			menuItem->setColor(Color3B(255,100,100));
// 		}
		menuItemVector.push_back(menuItem);
		_matchingMenu->addChild(menuItem, i + 10000,i + 10000);
		menuItem->setAnchorPoint(Point(0,0));
		menuItem->setPosition( Point(10, (Director::getInstance()->getWinSize().height - (i+ 1) * LINE_SPACE) ));
		
	}

	_matchingMenu->setPosition(Point(0,0));
	_matchingMenu->setZOrder(100);

	this->addChild(_matchingMenu);
}
// 
void HelloWorld::menuCallbackMatching(Ref * sender)
{
	Director::getInstance()->purgeCachedData();

	auto menuItem = static_cast<MenuItem *>(sender);
	
//	menuItem->setColor(Color3B(255,100,100));
	int idx = menuItem->getLocalZOrder() - 10000;
	
	if (idx == 0)
	{
		sp2->runAction(MoveTo::create(0.5f,sp1->getPosition()+Point(x1,height1 - y1)-Point(x2,height2 - y2)));
	}
	else if(idx == 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);

	Point oldP;
};

#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->setContentSize(aTexture->getContentSize());
	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) - Point(this->getContentSize().width/2, this->getContentSize().height/2));
}

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

	
	if (_state != kPaddleStateUngrabbed) return false;
	if ( !containsTouchLocation(touch) ) return false;

	this->setZOrder(1);
	muxPaddle->setZOrder(0);
	oldP = touch->getLocation();
	_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( this->getPosition()+touchPoint-oldP );
	oldP = touchPoint;
}

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: