cocos2dx新手引导类封装

cocos2dx新手引导类封装,第1张

概述做游戏新手引导式必不可少的一部分,使用opengl封装一个新手引导类,可以通用,代码如下: .h文件 #ifndef __GUIDE_H__#define __GUIDE_H__#include "cocos2d.h"#include "cocos-ext.h"#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID#include "gles/gl.

做游戏新手引导式必不可少的一部分,使用opengl封装一个新手引导类,可以通用,代码如下:

.h文件

#ifndef __GUIDE_H__#define __GUIDE_H__#include "cocos2d.h"#include "cocos-ext.h"#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID#include "gles/gl.h"#endif#include "global.h"using namespace cocos2d::extension;  USING_NS_CC;struct Rect{	float rx,ry,rwIDth,rheight;	Rect(float x,float y,float wIDth,float height)	{		rx=x;		ry=y;		rwIDth=wIDth;		rheight=height;	}	bool operator< (const Rect& r) const{return rx<r.rx;}	bool containsPoint(const CCPoint& point) const	{		bool bRet = false;		if (point.x >= rx && point.x <=rx+rwIDth			&& point.y >= ry && point.y <=ry+rheight)		{			bRet = true;		}		return bRet;	}};class GuIDe : public cocos2d::cclayer{public:	~GuIDe();	voID clear();	static voID reset();	static gluint vertexBuffer;	static gluint indexBuffer;	float gx,gy,m_wIDth,m_height;	float cd,m_precd;	bool bsrcclick;	static int param;	static GuIDe* gthis;	CCRect m_src;	int m_type;	float sumlen,curlen;	std::string talkstr;	cclabelTTF* m_talk;	float tscalex,tscaley,transval;	static CCGLProgram * pg;	static voID sreset();	virtual bool init(); 	voID Settransparent(float val);	voID update(float delta);	std::map<Rect,CCCallFuncND*> m_monitor,m_premonitor;	std::map<Rect,CCCallFuncND*> m_strike,m_prestrike;	voID delayjoin1(float dt);	voID delayjoin2(float dt);	voID AddMonitor(Rect rc,CCCallFuncND* cn);	voID AddPIErce( Rect rc,CCCallFuncND* cn);	char* AlignTxt(char* text,float wIDth);	voID onEnter();	CREATE_FUNC(GuIDe);	voID SetFocus(float x,float height,bool flag=true,int type=0,CCTexture2D* tex=0);//type:0椭圆 1矩形 2图片形状 	CCNode* CreateLabel(char* text,float x,float wIDth=0);	CCNode* CreateArrow(float x,float angle=0);	static CCRenderTexture * m_circle;	static CCRenderTexture * m_arrow;	static CCRenderTexture * m_rc;	CCTexture2D* texture2d;//做剪裁形状	static voID drawcircle();	static voID drawarrow();	static voID drawrc();	virtual voID draw();	voID onDraw();	virtual bool cctouchBegan(CCtouch *ptouch,CCEvent *pEvent);	virtual voID cctouchmoved(CCtouch *ptouch,CCEvent *pEvent);	virtual voID cctouchended(CCtouch *ptouch,CCEvent *pEvent);	virtual voID cctouchCancelled(CCtouch *ptouch,CCEvent *pEvent);	voID createHand(int dir,CCPoint pt=ccp(-1,-1));	voID createTutor(char *txt,bool ani=false);	static CCScale9Sprite* CreateTextSprite(char *text,float wIDth=0);	static cclabelTTF* GetFitLabel(char *text,float wIDth);};#endif // __GUIDE_H__

.cpp文件

#include "GuIDe.h"const char* shader_frag="\n#ifdef GL_ES\n\precision lowp float;\n#endif\n\uniform sampler2D CC_Texture0;\uniform float tval;\varying vec4 v_fragmentcolor;\varying vec2 v_texCoord;\voID main()\{\vec4 color=texture2D(CC_Texture0,v_texCoord);\	gl_Fragcolor =vec4(0.0,0.0,(1.0-color.a)*tval);\}";const char* shader_vert="attribute vec4 a_position;\attribute vec2 a_texCoord;\attribute vec4 a_color;\n#ifdef GL_ES\n\varying lowp vec4 v_fragmentcolor;\n#else\n\varying vec4 v_fragmentcolor;\n#endif\n\varying vec2 v_texCoord;\voID main()\{\	gl_position = CC_MVPMatrix * a_position;\	v_fragmentcolor = a_color;\	v_texCoord = a_texCoord;\}";unsigned char utf8_look_for_table[] = {	1,1,2,3,4,5,6,1};#define UTFLEN(x)  utf8_look_for_table[(x)]int getUtf8Length(char *str){	int clen = strlen(str);	int len = 0;	for(char *ptr = str;*ptr!=0&&len<clen;len++,ptr+=UTFLEN((unsigned char)*ptr));	return len;}char* subUtfString(const char *str,unsigned int start,unsigned int end){	int len = getUtf8Length((char*)str);	if(start >= len) return NulL;	if(end > len) end = len;	char *sptr = (char*)str;	for(int i = 0; i < start; ++i,sptr+=UTFLEN((unsigned char)*sptr));	char *eptr = sptr;	for(int i = start; i < end; ++i,eptr += UTFLEN((unsigned char)*eptr));	int retLen = eptr - sptr;	char *retStr = (char*)malloc(retLen+1);	memcpy(retStr,sptr,retLen);	retStr[retLen] = 0;	return retStr;}CCGLProgram *GuIDe::pg=0;CCRenderTexture *GuIDe::m_circle=0;CCRenderTexture *GuIDe::m_arrow=0;CCRenderTexture *GuIDe::m_rc=0;gluint GuIDe::vertexBuffer=0;gluint GuIDe::indexBuffer=0;voID GuIDe::clear(){/*	std::map<Rect,CCCallFuncND*>::iterator it;	for(it=m_monitor.begin();it!=m_monitor.end();++it)	{		if(it->second->isSingleReference())		  it->second->release();	}	for(it=m_strike.begin();it!=m_strike.end();++it)	{		if(it->second->isSingleReference())		 it->second->release();	}	m_monitor.clear();	m_strike.clear();*/}GuIDe::~GuIDe(){	/*gluint garr[]={vertexBuffer,indexBuffer};	glDeleteBuffers(2,garr);*/	gthis=0;	std::map<Rect,CCCallFuncND*>::iterator it;	for(it=m_monitor.begin();it!=m_monitor.end();++it)	{		if(it->second->isSingleReference())			it->second->release();	}	for(it=m_strike.begin();it!=m_strike.end();++it)	{		if(it->second->isSingleReference())			it->second->release();	}	m_monitor.clear();	m_strike.clear();}voID GuIDe::drawcircle(){	return;	int seg=20;	float a=180,b=180,xoff=200,yoff=200,scope=20;	a+=scope; b+=scope;	float gap=2*a/seg;	int k=2*a/gap+1;	int sumct1=2*k-1;	a-=scope; b-=scope;	gap=2*a/seg;	k=2*a/gap+1;	int sumct=2*k;	Vertex *vt=new Vertex[sumct+sumct1];	glushort* cindex=new glushort[sumct1*2];	vt[0].position[0]=xoff;	vt[0].position[1]=yoff;	vt[0].position[2]=0;	vt[0].color[0]=1;	vt[0].color[1]=1;	vt[0].color[2]=0;	vt[0].color[3]=1;	int count=1;	for(float i=a+xoff;i>=-a+xoff;i-=gap)	{		vt[count].position[0]=i;		vt[count].position[1]=sqrt(1-pow((i-xoff)/a,2))*b+yoff;		vt[count].position[2]=0;		vt[count].color[0]=0;		vt[count].color[1]=1;		vt[count].color[2]=0;		vt[count].color[3]=0.9;		++count;	}	for(float i=-a+xoff+gap;i<=a+xoff;i+=gap)	{		vt[count].position[0]=i;		vt[count].position[1]=-sqrt(1-pow((i-xoff)/a,2))*b+yoff;		vt[count].position[2]=0;		vt[count].color[0]=0;		vt[count].color[1]=1;		vt[count].color[2]=0;		vt[count].color[3]=0.9;		++count;	}	a+=scope; b+=scope; gap=2*a/seg;	for(float i=a+xoff;i>=-a+xoff;i-=gap)	{		vt[count].position[0]=i;		vt[count].position[1]=sqrt(1-pow((i-xoff)/a,2))*b+yoff;		vt[count].position[2]=0;		vt[count].color[0]=0;		vt[count].color[1]=1;		vt[count].color[2]=0;		vt[count].color[3]=0;		++count;	}	for(float i=-a+xoff+gap;i<=a+xoff;i+=gap)	{		vt[count].position[0]=i;		vt[count].position[1]=-sqrt(1-pow((i-xoff)/a,2))*b+yoff;		vt[count].position[2]=0;		vt[count].color[0]=0;		vt[count].color[1]=1;		vt[count].color[2]=0;		vt[count].color[3]=0;		++count;	}	int ct1=1,ct2=sumct;	for(int i=0;i<sumct1*2;++i)	{		if(i%2==0)			cindex[i]=ct1++;		else		{			cindex[i]=ct2++;		}	}	CCGLProgram *tpg=CCshadercache::sharedshadercache()->programForKey(kCCShader_positioncolor);	tpg->use();	tpg->setUniformsForBuiltins();	glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);	glBufferData(GL_ARRAY_BUFFER,sizeof(Vertex)*(sumct+sumct1),vt,GL_STATIC_DRAW);	Glint _positionLocation = glGetAttribLocation(tpg->getProgram(),"a_position");	Glint _colorLocation = glGetAttribLocation(tpg->getProgram(),"a_color");	glEnabLevertexAttribarray(_positionLocation);	glEnabLevertexAttribarray(_colorLocation);	glVertexAttribPointer(_positionLocation,GL_float,GL_FALSE,sizeof(Vertex),(GLvoID*)offsetof(Vertex,position));	glVertexAttribPointer(_colorLocation,color));	glEnable(GL_BLEND);	glBlendFunc(GL_SRC_Alpha,GL_ONE_MINUS_SRC_Alpha);	glEnable(GL_DEPTH_TEST);	glDepthFunc(GL_LEQUAL);	glEnable(GL_POINT_SMOOTH);  	glHint(GL_POINT_SMOOTH,GL_NICEST);  	glEnable(GL_liNE_SMOOTH);  	glHint(GL_liNE_SMOOTH,GL_NICEST);  	glDrawArrays(GL_TRIANGLE_FAN,sumct);	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBuffer);	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(glushort)*sumct1*2,cindex,GL_STATIC_DRAW);	glDrawElements(GL_TRIANGLE_STRIP,sumct1*2,GL_UNSIGNED_SHORT,0);	delete []vt;	delete []cindex;	gldisable(GL_DEPTH_TEST);	glBindBuffer(GL_ARRAY_BUFFER,0);	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);}voID GuIDe::drawarrow(){	return;	Vertex vt[]={		{{25,0},{1,1},1}},{{75,75,{{100,{{50,100,{{3,{{25,1}}	};	glushort index[]={		0,5	};	CCGLProgram *tpg=CCshadercache::sharedshadercache()->programForKey(kCCShader_positioncolor);	tpg->use();	tpg->setUniformsForBuiltins();	glEnable(GL_DEPTH_TEST);	glDepthFunc(GL_LEQUAL);	glEnable(GL_liNE_SMOOTH);  	glHint(GL_liNE_SMOOTH,GL_NICEST);  	glBindBuffer(GL_ARRAY_BUFFER,sizeof(vt),GL_STATIC_DRAW);	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,sizeof(index),index,"a_color");	glEnabLevertexAttribarray(_positionLocation);	glEnabLevertexAttribarray(_colorLocation);	glVertexAttribPointer(_positionLocation,color));	glDrawElements(GL_TRIANGLES,9,0);	for(int i=0;i<7;++i)	{		vt[i].color[0]=0;		vt[i].color[1]=0;		vt[i].color[2]=0; vt[i].color[3]=1;	}	glBindBuffer(GL_ARRAY_BUFFER,GL_STATIC_DRAW);	gllinewidth(1);	glDrawArrays(GL_liNE_LOOP,7);	gldisable(GL_DEPTH_TEST);	glBindBuffer(GL_ARRAY_BUFFER,0);}voID GuIDe::drawrc(){	return;	Vertex vt[]={		{{2,{0,{{398,{{2,398,};	glushort lindex[]={		0,2	};	CCGLProgram *tpg=CCshadercache::sharedshadercache()->programForKey(kCCShader_positioncolor);	tpg->use();	tpg->setUniformsForBuiltins();	glBindBuffer(GL_ARRAY_BUFFER,color));	glDrawArrays(GL_TRIANGLE_STRIP,4);	for(int i=0;i<4;++i)	{		vt[i].color[0]=0.3;		vt[i].color[1]=0.6;		vt[i].color[2]=0.2;		vt[i].color[3]=1;	}	glBindBuffer(GL_ARRAY_BUFFER,sizeof(lindex),lindex,GL_STATIC_DRAW);	gllinewidth(1);	glDrawElements(GL_liNE_LOOP,0);	gldisable(GL_DEPTH_TEST);	glBindBuffer(GL_ARRAY_BUFFER,0);}cclabelTTF* GuIDe::GetFitLabel(char *text,float wIDth){	cclabelTTF* pLabel = cclabelTTF::create(text,"Arial",24);		std::string tempstr=std::string(text),srcstr(text),rs;	int flag=0,flag1=0,curlen=0,slen=0,offset=0;	while(1)	{		int tplen=getUtf8Length((char*)tempstr.c_str());		if(pLabel->getContentSize().wIDth>=wIDth&&tplen>0)		{			if(flag1==1)			{				flag=flag1=0;				slen=slen-curlen;				for(int i=1;i<curlen;++i)				{					slen+=1;					tempstr=subUtfString(srcstr.c_str(),offset,offset+slen);					pLabel->setString(tempstr.c_str());					if(pLabel->getContentSize().wIDth>wIDth)					{						slen-=1;						break;					}				}				tempstr=subUtfString(srcstr.c_str(),offset+slen);				rs.append(tempstr);				rs.append("\n");				offset+=slen;				tempstr=subUtfString(srcstr.c_str(),-1);				pLabel->setString(tempstr.c_str());				continue;			}			curlen=getUtf8Length((char*)tempstr.c_str())/2;			tempstr=subUtfString(srcstr.c_str(),offset+curlen);			pLabel->setString(tempstr.c_str());			flag=1;			continue;		}		if(pLabel->getContentSize().wIDth<wIDth&&tplen>0&&flag==1)		{			if(curlen<=1)				curlen=1;			else				curlen=curlen/2;			slen=tplen+curlen;			tempstr=subUtfString(srcstr.c_str(),offset+slen);			pLabel->setString(tempstr.c_str());			flag1=1;			continue;		}		if(pLabel->getContentSize().wIDth<wIDth&&tplen>0)		{			tempstr=subUtfString(srcstr.c_str(),-1);			rs.append(tempstr);			break;		}	}	pLabel->setString(rs.c_str());	pLabel->setAnchorPoint(CCPointZero);	pLabel->setHorizontalAlignment(kCCTextAlignmentleft);	return pLabel;}CCScale9Sprite* GuIDe::CreateTextSprite(char *text,float wIDth){	CCTexture2D *tx=m_rc->getSprite()->getTexture();	CCSize sz=tx->getContentSize();	//CCScale9Sprite* sp = CCScale9Sprite::createWithSpriteFrame(	//	CCSpriteFrame::createWithTexture(tx,CCRect(0,sz.wIDth,sz.height)));	char bkname[32]="guIDe/22.png";	if(wIDth>=960)		strcpy(bkname,"guIDe/222.png");	CCScale9Sprite* sp = CCScale9Sprite::create(bkname);	sp->setAnchorPoint(ccp(0,0));	if(wIDth<960)	  sp->setopacity(200);	sp->setInsetBottom(9);	sp->setInsetRight(9);	sp->setInsettop(9);	sp->setInsetleft(9);	CCSize bksize=sp->getContentSize(); 	if(wIDth==0)	{		wIDth=bksize.wIDth;	}	cclabelTTF* pLabel=GetFitLabel(text,wIDth);		CCSize extenSize = CCSizeMake(pLabel->getContentSize().wIDth+20,pLabel->getContentSize().height+20);	sp->setContentSize(extenSize);	sp->addChild(pLabel,10);	pLabel->setposition(ccpAdd(sp->getposition(),ccp(10,10)));	return sp;}voID GuIDe::draw(){	cclayer::draw();	onDraw();	if(curlen<sumlen)	{		curlen+=0.2;		char *sub = subUtfString(talkstr.c_str(),curlen);		m_talk->setString(sub);	}}voID GuIDe::update(float delta){	cd+=delta;	if(cd>3)	{		 		unscheduleUpdate();		 	}}voID GuIDe::onDraw(){   	return ;	CCDirector* director = CCDirector::sharedDirector();	CCSize sz=director->getWinSize();	CCSize psz=texture2d->getContentSize();	glEnable(GL_BLEND);	glBlendFunc(GL_SRC_Alpha,GL_ONE_MINUS_SRC_Alpha);	glEnable(GL_DEPTH_TEST);	glDepthFunc(GL_LEQUAL);	float w1=psz.wIDth,h1=psz.height,w=sz.wIDth,h=sz.height;	pg->use();	pg->setUniformsForBuiltins();	Glint _positionLocation = glGetAttribLocation(pg->getProgram(),"a_position");	Glint _colorLocation = glGetAttribLocation(pg->getProgram(),"a_color");	Glint _textureLocation = glGetAttribLocation(pg->getProgram(),"a_texCoord");	Glint _textureUniform = glGetUniformlocation(pg->getProgram(),"CC_Texture0");	Glint tval = glGetUniformlocation(pg->getProgram(),"tval");	gluniform1f(tval,transval);	float tx=gx-w1/2,ty=gy-w1/2;	tx-=tscalex;ty-=tscaley; w1+=tscalex*2; h1+=tscaley*2; 	float tw=w1,th=h1;	Vertex ttt1[] = {		// Front		{{0,{-tx/tw,1+ty/th}},{{w,{1+(w-w1-tx)/tw,{{0,h,-(h-ty-h1)/th}},-(h-ty-h1)/th}}	};	glBindBuffer(GL_ARRAY_BUFFER,sizeof(ttt1),ttt1,GL_STATIC_DRAW);	glEnabLevertexAttribarray(_positionLocation);	glEnabLevertexAttribarray(_colorLocation);	glEnabLevertexAttribarray(_textureLocation);	glVertexAttribPointer(_positionLocation,color));	glVertexAttribPointer(_textureLocation,TexCoord));	////set sampler	ccGLBindTexture2DN(0,texture2d->getname());	gluniform1i(_textureUniform,0); // unnecc in practice	glEnable(GL_liNE_SMOOTH);  	glHint(GL_liNE_SMOOTH,GL_NICEST);  	//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_border);	//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_border);	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_liNEAR);	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_liNEAR);	float clr[]={0,0};	//glTexParameterfv( GL_TEXTURE_2D,GL_TEXTURE_border_color,clr);   	glDrawArrays(GL_TRIANGLE_STRIP,4);	gldisable(GL_DEPTH_TEST);	glBindBuffer(GL_ARRAY_BUFFER,0);}voID GuIDe::delayjoin1(float dt){	std::map<Rect,CCCallFuncND*>::iterator it;	for(it=m_monitor.begin();it!=m_monitor.end();++it)	{		if(it->second->isSingleReference())			it->second->release();	}	m_monitor.clear();	for(it=m_strike.begin();it!=m_strike.end();++it)	{		if(it->second->isSingleReference())			it->second->release();	}	m_strike.clear();	m_monitor.insert(m_premonitor.begin(),m_premonitor.end());	m_premonitor.clear();}voID GuIDe::delayjoin2(float dt){	std::map<Rect,CCCallFuncND*>::iterator it;	for(it=m_monitor.begin();it!=m_monitor.end();++it)	{		if(it->second->isSingleReference())			it->second->release();	}	m_monitor.clear();	for(it=m_strike.begin();it!=m_strike.end();++it)	{		if(it->second->isSingleReference())			it->second->release();	}	m_strike.clear();	m_strike.insert(m_prestrike.begin(),m_prestrike.end());	m_prestrike.clear();}voID GuIDe::AddMonitor(Rect rc,CCCallFuncND* cn){	if(cn)	cn->retain();	m_premonitor[rc]=cn;	scheduleOnce(schedule_selector(GuIDe::delayjoin1),0.1);}voID GuIDe::AddPIErce(Rect rc,CCCallFuncND* cn){	if(cn)	cn->retain();	m_prestrike[rc]=cn;	scheduleOnce(schedule_selector(GuIDe::delayjoin2),0.1);}char* GuIDe::AlignTxt(char* text,float wIDth){	static char str1[256];	memset(str1,256);	std::string str;	cclabelTTF* pLabel1 = cclabelTTF::create(text,24);	cclabelTTF* pLabel2 = cclabelTTF::create(" ",24);	float  wd1=pLabel1->getContentSize().wIDth;	float  wd2=pLabel2->getContentSize().wIDth;	int sct=(wIDth-wd1)/wd2;	str+="\n";	for(int i=0;i<sct/2;++i)	{		str+=" ";	}	str+=text;	for(int i=0;i<sct/2+4;++i)	{		str+=" ";	}	strcpy(str1,str.c_str());	return str1;}bool GuIDe::cctouchBegan(CCtouch *ptouch,CCEvent *pEvent){	if(isVisible()==false)		return true;	if(cd<m_precd)		return true;	std::map<Rect,CCCallFuncND*>::iterator pit;	for(pit=m_strike.begin();pit!=m_strike.end();++pit)	{		if(pit->first.containsPoint(ptouch->getLocation()))		{			if(pit->second)			{				pit->second->setTarget(this);				pit->second->execute();			}			return false;		}	}	std::map<Rect,CCCallFuncND*>::iterator it;	for(it=m_monitor.begin();it!=m_monitor.end();++it)	{		if(it->first.containsPoint(ptouch->getLocation()))		{			if(it->second)			{				it->second->setTarget(this);				it->second->execute();			}			return true;		}	}	if(m_src.containsPoint(ptouch->getLocation()))	{		if(bsrcclick)		{			removeFromParentAndCleanup(true);			return false;		}	}	return true;}voID GuIDe::cctouchmoved(CCtouch *ptouch,CCEvent *pEvent){}voID GuIDe::cctouchended(CCtouch *ptouch,CCEvent *pEvent){}voID GuIDe::cctouchCancelled(CCtouch *ptouch,CCEvent *pEvent){}voID GuIDe::SetFocus(float x,bool flag,int type,CCTexture2D* tex){	gx=x;	gy=y;	m_wIDth=wIDth;	m_height=height;	m_type=type;	bsrcclick=flag;	m_src=CCRect(gx-m_wIDth/2,gy-height/2,m_height);	switch (type)	{	case 0:		texture2d=m_circle->getSprite()->getTexture();		break;	case 1:		texture2d=m_rc->getSprite()->getTexture();		break;	case 2:		texture2d=tex;		break;	}	CCSize sz=texture2d->getContentSize();	tscalex=(m_wIDth-sz.wIDth)/2;	tscaley=(m_height-sz.height)/2;}CCNode* GuIDe::CreateLabel(char* text,float wIDth){	CCNode* plabel=CreateTextSprite(text,wIDth);	plabel->setposition(x,y);	addChild(plabel);	return plabel;}CCNode* GuIDe::CreateArrow(float x,float angle){	//CCSprite *spr=CCSprite::createWithTexture(m_arrow->getSprite()->getTexture());	CCSprite *spr=CCSprite::create("guIDe/11.png");	spr->setposition(ccp(x,y));	spr->setRotation(angle);	addChild(spr,10);	return spr;}voID GuIDe::onEnter(){	cclayer::onEnter();	this->settouchEnabled(true);	CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,-1000000,true);}voID GuIDe::Settransparent(float val){	transval=val;}voID GuIDe::reset(){	if(vertexBuffer==0)		glGenBuffers( 1,&vertexBuffer );	if(indexBuffer==0)		glGenBuffers( 1,&indexBuffer );	if(pg==0)	{		pg=new CCGLProgram();		pg->initWithVertexShaderByteArray(shader_vert,shader_frag);		pg->link();		pg->updateUniforms();	}	if(m_circle==0)	{		m_circle=CCRenderTexture::create(400,400);		m_circle->begin();		drawcircle();		m_circle->end(); 		m_circle->retain();		m_circle->getSprite()->getTexture()->setAntiAliasTexParameters();	}	if(m_arrow==0)	{		m_arrow=CCRenderTexture::create(100,100);		m_arrow->begin();		drawarrow();		m_arrow->end(); 		m_arrow->retain();		m_arrow->getSprite()->getTexture()->setAntiAliasTexParameters();	}	if(m_rc==0)	{		m_rc=CCRenderTexture::create(400,400);		m_rc->begin();		drawrc();		m_rc->end(); 		m_rc->retain();		m_rc->getSprite()->getTexture()->setAntiAliasTexParameters();	}}voID GuIDe::sreset(){	if(pg)	{		pg->reset();		pg->initWithVertexShaderByteArray(shader_vert,shader_frag);		pg->addAttribute(kCCAttributenameposition,kCCVertexAttrib_position);		pg->addAttribute(kCCAttributenamecolor,kCCVertexAttrib_color);		pg->addAttribute(kCCAttributenameTexCoord,kCCVertexAttrib_TexCoords);		pg->link();		pg->updateUniforms();	}}voID GuIDe::createHand(int dir,CCPoint pt){	CCPoint temp;	if(dir==0)//左边	{		temp.x=200;  temp.y=270;		if(pt.x!=-1) temp=pt;		CCSprite* spr=CCSprite::create("circle.png");		CCSprite* spr1=CCSprite::create("thumb1.png");		spr->runAction(CCRepeatForever::create(CCSequence::create(CCScaleto::create(0.6,1.5),CCScaleto::create(0.6,1.0),0)));		spr->setposition(ccp(temp.x,temp.y));		spr1->setFlipX(true);		spr1->setposition(ccp(temp.x-30,temp.y-45));		spr1->runAction(CCRepeatForever::create(CCSequence::create(CCMoveBy::create(0.6,ccp(15,15)),CCMoveBy::create(0.6,ccp(-15,-15)),0)));		addChild(spr,10);		addChild(spr1,10);	}	else if(dir==1)//右边	{		temp.x=800;  temp.y=270;		if(pt.x!=-1) temp=pt;		CCSprite* spr=CCSprite::create("circle.png");		CCSprite* spr1=CCSprite::create("thumb1.png");		spr->runAction(CCRepeatForever::create(CCSequence::create(CCScaleto::create(0.6,temp.y));		spr1->setposition(ccp(temp.x+30,10);	}}voID GuIDe::createTutor(char *txt,bool ani){	CCSprite* spr1=CCSprite::create("guIDe/tutor.png");	spr1->setposition(ccp(465,90));	addChild(spr1);		m_talk=GetFitLabel(txt,960-180);	curlen=sumlen=getUtf8Length((char*)m_talk->getString());	if(ani)	{		curlen=1;	}	talkstr=m_talk->getString();	char *sub = subUtfString(talkstr.c_str(),curlen);	m_talk->setString(sub);	m_talk->setAnchorPoint(ccp(0,1));	m_talk->setposition(ccp(330,150));	addChild(m_talk);}int GuIDe::param=0;GuIDe* GuIDe::gthis=0;bool GuIDe::init(){  	cclog("------------------------ GuIDe::init()----------------------");	gthis=this;	cd=0;	m_precd=0.2;	gx=0;	gy=0;	m_wIDth=0;	m_height=0;	scheduleUpdate();	transval=0.6;	if(vertexBuffer==0)	glGenBuffers( 1,&vertexBuffer );	if(indexBuffer==0)	glGenBuffers( 1,400);		m_circle->begin();		drawcircle();		m_circle->end(); 		m_circle->retain();		m_circle->getSprite()->getTexture()->setAntiAliasTexParameters();	}		if(m_arrow==0)	{		m_arrow=CCRenderTexture::create(100,100);		m_arrow->begin();		drawarrow();		m_arrow->end(); 		m_arrow->retain();		m_arrow->getSprite()->getTexture()->setAntiAliasTexParameters();	}		if(m_rc==0)	{		m_rc=CCRenderTexture::create(400,400);		m_rc->begin();		drawrc();		m_rc->end(); 		m_rc->retain();		m_rc->getSprite()->getTexture()->setAntiAliasTexParameters();	}		texture2d =m_circle->getSprite()->getTexture();	return true;}
总结

以上是内存溢出为你收集整理的cocos2dx新手引导类封装全部内容,希望文章能够帮你解决cocos2dx新手引导类封装所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1060936.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存