// on "init" you need to initialize your instance
bool HelloWorld::init()
{
  if (!CCLayer::init()) {
    return false;
  }
  
  CCSize s = CCDirector::sharedDirector()->getWinSize();
  render_texture_ = CCRenderTexture::create(s.width, s.height,
                                            kCCTexture2DPixelFormat_RGBA8888);
  render_texture_->retain();
  render_texture_->setPosition(ccp(s.width / 2, s.height / 2));
  render_texture_->clear(1.0, 1.0, 1.0, 1.0);
  this->addChild(render_texture_);

  std::string text = "0123456789";
  for (int i = 0; i < 8; i++) {
    text += text;
  }
  CCLabelTTF* label = new CCLabelTTF;
  label->initWithString(text.c_str(), "Arial", 18,
                        CCSizeMake(1000, 0), kCCTextAlignmentLeft);
  label->setPosition(ccp(10, 760));
  label->setAnchorPoint(ccp(0, 1));
  label->setColor(ccc3(0, 0, 0));
  render_texture_->begin();
  label->visit();
  render_texture_->end();
  label->release();
  
  return true;
}
string SpriteWordAndImage::FillLineBySpace(string lineStr)
{
    CCLabelTTF * tl = CCLabelTTF::create("", m_fontName, m_fontSize);
    float width = 0;
    while (width<m_width) {
        lineStr += " ";
        tl->setString(lineStr.c_str());
        width = tl->getContentSize().width;
    }
    lineStr = lineStr.substr(0,lineStr.length()-1);
    tl->release();
    return lineStr;
}
Beispiel #3
0
int Bitmap::handler_method_drawtext( int ptr1,void* ptr2 )
{
 	Bitmap* bitmap = (Bitmap*)ptr1;
	if (NULL==bitmap->p->m_emuBitmap)
		return -1;

	bool firstdraw = false;
	CCRenderTexture* fontRender = (CCRenderTexture*)bitmap->p->m_fontRender;
	if (NULL==fontRender)
	{
		fontRender = CCRenderTexture::create(bitmap->p->m_width,bitmap->p->m_height);
		bitmap->getEmuBitmap()->addChild(fontRender);
		fontRender->setPosition(ccp(bitmap->p->m_width/2,bitmap->p->m_height/2));
		bitmap->p->m_fontRender = fontRender;
		fontRender->retain();
		firstdraw = true;
		//fontRender->getSprite()->getTexture()->setAliasTexParameters();
	}

	DrawtextStruct* ptr2struct = (DrawtextStruct*)ptr2;
	string tmpdrawchar = ptr2struct->str;
// 	int checknum = atoi(tmpdrawchar.c_str());
// 	if (checknum!=0)
// 	{
// 		char tmp[20]={0};
// 		sprintf(tmp,"%d",checknum);
// 		tmpdrawchar = tmp;
// 	}

	CCLabelTTF* label = CCLabelTTF::create(tmpdrawchar.c_str(),ptr2struct->font->getName(),ptr2struct->font->getSize());
	//label->getTexture()->setAliasTexParameters();
	if (ptr2struct->font)
	{
		Font* f = ptr2struct->font; 
		label->setFontName(f->getName());
		label->setFontSize(f->getSize());
		label->setColor(ccc3(f->getColor()->red*f->getColor()->alpha/255,
			f->getColor()->green*f->getColor()->alpha/255,
			f->getColor()->blue*f->getColor()->alpha/255));
		//label->setOpacity(f->getColor()->alpha);
	}
	label->setAnchorPoint(ccp(0,1));
	label->setDimensions(CCSizeMake(ptr2struct->rect.w,ptr2struct->rect.h));
	label->setPosition(ccp(ptr2struct->rect.x,rgss_y_to_cocos_y(ptr2struct->rect.y,bitmap->p->m_height)));
	label->setVerticalAlignment(kCCVerticalTextAlignmentCenter);

	if (ptr2struct->align == Bitmap::Center)
		label->setHorizontalAlignment(kCCTextAlignmentCenter);
	else if(ptr2struct->align == Bitmap::Right)
		label->setHorizontalAlignment(kCCTextAlignmentRight);
	else if (ptr2struct->align == Bitmap::Left)
		label->setHorizontalAlignment(kCCTextAlignmentLeft);

// 	CCLayerColor* masklayer = CCLayerColor::create(ccc4(255,255,255,255));
// 	masklayer->setContentSize(label->getContentSize());
// 	masklayer->setPosition(ccp(ptr2struct->rect.x,rgss_y_to_cocos_y(ptr2struct->rect.y,bitmap->m_height)-masklayer->getContentSize().height));
// 	ccBlendFunc fun = {GL_ZERO,GL_ZERO};
// 	masklayer->setBlendFunc(fun);

	fontRender->begin();
	//masklayer->visit();
	label->visit();
	fontRender->end();
	label->release();

	delete ptr2struct;
	return 0;
}
void SpriteWordAndImage::AddLabel()
{
    for (int i=0; i<15; i++) {
        m_Imgs[i].clear();        
    }
    
    CCLabelTTF * tl = CCLabelTTF::create("", m_fontName, m_fontSize);
    //生成代替图片的空格
    string imgWord = " ";
    tl->setString(imgWord.c_str());
    float imgWidth = tl->getContentSize().width;
    while (imgWidth < image_width) {
        imgWord += " ";
        tl->setString(imgWord.c_str());
        imgWidth = tl->getContentSize().width;
    }
    string oriStr = m_label;
    string outStr = "";
    string lineStr = "";
    BYTE wordBegin = 0;
    BYTE imgIndex = 0;
    BYTE lineIndex = 1;
    float width = 0;
    float height = 0;
    float lineHeight = line_height;
    bool isImage = false;
    
    int count = CommonFunction::calcCharCount(oriStr.c_str());
    for (int i=0; i<count; i++) {
        string word = CommonFunction::subString(oriStr, i,1);
        
        if (word.c_str()[0] == '[' && i+2<count) {
            string nimg;
            int end = i+1;
            while (CommonFunction::subString(oriStr, end,1).c_str()[0] != ']' && end < count) {
                end++;
            }
            if (CommonFunction::subString(oriStr, end,1).c_str()[0] == ']') {
                m_Imgs[imgIndex].nUsed = true;
                m_Imgs[imgIndex].nImg = CommonFunction::subString(oriStr,i+1,end-i-1);
                word = imgWord;
                i = end;
                isImage = true;
            }
        }
        
        lineStr += word;
        tl->setString(lineStr.c_str());
        if (colorType == CT_Black) {
            tl->setColor(CC_Black);
        }
        width = tl->getContentSize().width;
        if (width > m_width) { //判断是否要换行
            
            CCLabelTTF * l = CCLabelTTF::create(outStr.c_str(), m_fontName, m_fontSize);
            l->setAnchorPoint(ccp(0,0.5));
            if (colorType == CT_Black) {
                l->setColor(CC_Black);
            }
            l->setPosition(ccp(0, height - lineHeight*0.5));
            addChild(l);
            height -= lineHeight;
            lineHeight = line_height;
            
            outStr = word;
            lineStr = word;
            lineIndex++;
        }
        else
        {
            outStr += word;
        }
        wordBegin = i;
        
        if (isImage) { //计算图片位置 图片索引累加
            lineHeight = image_height>lineHeight?image_height:lineHeight;
            m_Imgs[imgIndex].nWidth= width > m_width?imgWidth*0.5:width-imgWidth*0.5;
            m_Imgs[imgIndex].nHeight = height - lineHeight*0.5;
            imgIndex++;
        }
        isImage = false;

    }
    
    CCLabelTTF * l = CCLabelTTF::create(outStr.c_str(), m_fontName, m_fontSize);
    l->setAnchorPoint(ccp(0,0.5));
    if (colorType == CT_Black) {
        l->setColor(CC_Black);
    }
    l->setPosition(ccp(0, height- lineHeight*0.5));
    countHeight = (height-lineHeight)*-1; //算总高度
    addChild(l);
    AddImages();
    tl->release();
}