コード例 #1
0
	void CCLabelTTF::setString(const char *label)
	{
        if (m_pString)
        {
            delete m_pString;
            m_pString = NULL;
        }
        m_pString = new std::string(label);

		CCTexture2D *texture;
		if( CCSize::CCSizeEqualToSize( m_tDimensions, CCSizeZero ) )
		{
			texture = new CCTexture2D();
			texture->initWithString(label, m_pFontName->c_str(), m_fFontSize);
		}
		else
		{
			texture = new CCTexture2D();
			texture->initWithString(label, m_tDimensions, m_eAlignment, m_pFontName->c_str(), m_fFontSize);
		}
		this->setTexture(texture);
		texture->release();

        CCRect rect = CCRectZero;
		rect.size = m_pobTexture->getContentSize();
		this->setTextureRect(rect);
	}
コード例 #2
0
ファイル: CCLabelTTF.cpp プロジェクト: geniikw/myFirst2DGame
	void CCLabelTTF::setString(const char *label)
	{
        if (strlen(label)==0)
        {
            return;
        }

		m_sString = string(label);
        
		CCTexture2D *texture;
		if( CGSize::CGSizeEqualToSize( m_tDimensions, CGSizeZero ) )
		{
			texture = new CCTexture2D();
			texture->initWithString(label, m_sFontName.c_str(), m_fFontSize);
		}
		else
		{
			texture = new CCTexture2D();
			texture->initWithString(label, m_tDimensions, m_eAlignment, m_sFontName.c_str(), m_fFontSize);
		}
		this->setTexture(texture);
		texture->release();

        CGRect rect = CGRectZero;
		rect.size = m_pobTexture->getContentSize();
		this->setTextureRect(rect);
	}
コード例 #3
0
// Helper
void CCLabelTTF::updateTexture()
{
    CCTexture2D *tex;
    if (m_tDimensions.width == 0 || m_tDimensions.height == 0)
    {
        tex = new CCTexture2D();
        tex->initWithString(m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()) ;
    }
    else
    {
        tex = new CCTexture2D();
        tex->initWithString(m_string.c_str(),
                            CC_SIZE_POINTS_TO_PIXELS(m_tDimensions),
                            m_hAlignment,
                            m_vAlignment,
                            m_pFontName->c_str(),
                            m_fFontSize * CC_CONTENT_SCALE_FACTOR());
    }

    // iPad ?
    //if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
    if (CCApplication::sharedApplication().isIpad())
    {
        if (CC_CONTENT_SCALE_FACTOR() == 2)
        {
            tex->setResolutionType(kCCResolutioniPadRetinaDisplay);
        }
        else
        {
            tex->setResolutionType(kCCResolutioniPad);
        }
    }
    // iPhone ?
    else
    {
        if (CC_CONTENT_SCALE_FACTOR() == 2)
        {
            tex->setResolutionType(kCCResolutioniPhoneRetinaDisplay);
        }
        else
        {
            tex->setResolutionType(kCCResolutioniPhone);
        }
    }

    this->setTexture(tex);
    tex->release();

    CCRect rect = CCRectZero;
    rect.size = m_pobTexture->getContentSize();
    this->setTextureRect(rect);
}
コード例 #4
0
ファイル: CCLabelTTF.cpp プロジェクト: HotWordland/cocos2d-x
// Helper
bool CCLabelTTF::updateTexture()
{
    CCTexture2D *tex;
    
    // let system compute label's width or height when its value is 0
    // refer to cocos2d-x issue #1430
    tex = new CCTexture2D();
    if (!tex)
    {
		return false;
    }
    
    tex->initWithString( m_string.c_str(),
                        m_pFontName->c_str(),
                        m_fFontSize * CC_CONTENT_SCALE_FACTOR(),
                        CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), 
                        m_hAlignment,
                        m_vAlignment);
	
    this->setTexture(tex);
    tex->release();
	
	CCRect rect = CCRectZero;
    rect.size = m_pobTexture->getContentSize();
    this->setTextureRect(rect);

    return true;
}
コード例 #5
0
ファイル: CCLabelTTF.cpp プロジェクト: Aaron-Teng/AnalyticX
void CCLabelTTF::setString(const char *label)
{
    if (m_pString)
    {
        delete m_pString;
        m_pString = NULL;
    }
    m_pString = new std::string(label);
    
    CCTexture2D *texture;
    if( CCSize::CCSizeEqualToSize( m_tDimensions, CCSizeZero ) )
    {
        texture = new CCTexture2D();
        texture->initWithString(label, m_pFontName->c_str(), m_fFontSize);
    }
    else
    {
        texture = new CCTexture2D();
        texture->initWithString(label, m_tDimensions, m_eAlignment, m_pFontName->c_str(), m_fFontSize);
    }

// TODO
// #ifdef __CC_PLATFORM_IOS
//     // iPad ?
//     if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
//         if( CC_CONTENT_SCALE_FACTOR() == 2 )
//             [tex setResolutionType:kCCResolutioniPadRetinaDisplay];
//         else
//             [tex setResolutionType:kCCResolutioniPad];
//     }
//     // iPhone ?
//     else
//     {
//         if( CC_CONTENT_SCALE_FACTOR() == 2 )
//             [tex setResolutionType:kCCResolutioniPhoneRetinaDisplay];
//         else
//             [tex setResolutionType:kCCResolutioniPhone];
//     }
// #end
    this->setTexture(texture);
    texture->release();

    CCRect rect = CCRectZero;
    rect.size = m_pobTexture->getContentSize();
    this->setTextureRect(rect);
}
コード例 #6
0
ファイル: CCLabelTTF.cpp プロジェクト: caoguoping/warCraft
// Helper
bool CCLabelTTF::updateTexture()
{
    CCTexture2D *tex;
    tex = new CCTexture2D();
    
    if (!tex)
        return false;
    
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    
        ccFontDefinition texDef = _prepareTextDefinition(true);
        tex->initWithString( m_string.c_str(), &texDef );
    
    #else
    
        tex->initWithString( m_string.c_str(),
                            m_pFontName->c_str(),
                            m_fFontSize * CC_CONTENT_SCALE_FACTOR(),
                            CC_SIZE_POINTS_TO_PIXELS(m_tDimensions),
                            m_hAlignment,
                            m_vAlignment);
    
    #endif
    
    // set the texture
    this->setTexture(tex);
    // release it
    tex->release();
    
    // set the size in the sprite
    CCRect rect =CCRectZero;
    rect.size   = m_pobTexture->getContentSize();
    this->setTextureRect(rect);
    
    //ok
    return true;
}
コード例 #7
0
ファイル: CtrlEdit.cpp プロジェクト: twkevin/ddz
CCSize CCtrlEdit::GetTextSize(const char * text)
{
	CCSize textSize(0.0f,0.0f);

	if (NULL == text)
	{
		return textSize;
	}

	do
	{
		CCTexture2D *tex = new CCTexture2D();
		CC_BREAK_IF(!tex);

		float fContentScaleFactor= CCDirector::sharedDirector()->getContentScaleFactor();
		bool bRet = tex->initWithString( text,
			m_strFontName.c_str(),
			m_nFontSize * fContentScaleFactor,
			CCSizeZero,
			m_pTextField->getHorizontalAlignment(),
			m_pTextField->getVerticalAlignment());

		if (bRet)
		{
			float fOffsetX = tex->getPixelsWide();
			fOffsetX /= fContentScaleFactor;
			textSize.width += fOffsetX;

			float fOffsetY = tex->getPixelsHigh();
			fOffsetY /= fContentScaleFactor;
			textSize.height += fOffsetY;
		}

		tex->release();
	}while(false);

	return textSize;
}
コード例 #8
0
// Helper
bool CCLabelTTF::updateTexture()
{
	//if(this->isFlipY())
	//	this->setFlipY(false);
    CCTexture2D *tex;
    tex = new CCTexture2D();
    
    if (!tex)
        return false;

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    
        ccFontDefinition texDef = _prepareTextDefinition(true);
        tex->initWithString( m_string.c_str(), &texDef );
    
    #else
    
        tex->initWithString( m_string.c_str(),
                            m_pFontName->c_str(),
                            m_fFontSize * CC_CONTENT_SCALE_FACTOR(),
                            CC_SIZE_POINTS_TO_PIXELS(m_tDimensions),
                            m_hAlignment,
                            m_vAlignment);
    
    #endif
    
    // set the texture
    this->setTexture(tex);
    // release it
    tex->release();
    
    // set the size in the sprite
    CCRect rect =CCRectZero;
    rect.size   = m_pobTexture->getContentSize();
    this->setTextureRect(rect);

	/***************************给文字描黑边begin*********************************/

	CCPoint prePos = this->getPosition();
	ccColor3B color1 = this->getColor();
    float strokeValue = 2;
	CCSize textureSize = this->getContentSize();
    textureSize.width += 2 * strokeValue;
    textureSize.height += 2 * strokeValue;
     
    /* 监测OpenGl的错误状态 */
    glGetError();
     
    /* 创建一张纹理画布 */
    CCRenderTexture *rt = CCRenderTexture::create(textureSize.width, textureSize.height);
    if(!rt)
    {
        CCLog("create render texture failed !!!!");
        //addChild(label);
        return 0;
    }
     
    /* 设置描边的颜色 */
    this->setColor(ccBLACK);
     
    /* 
     *拿到源文字的混色机制,存储以备恢复,并设置新的目标混色机制
     *混色机制设为:源颜色透明度(影响亮度)和目标颜色(影响颜色)
     */
    ccBlendFunc originalBlend = this->getBlendFunc();
    ccBlendFunc func = { GL_SRC_ALPHA, GL_ONE};
    this->setBlendFunc(func);
     CCPoint anchPoint = this->getAnchorPoint();
	 
    /* 这是自定义的一些调整,倾斜了一点 */
    this->setAnchorPoint(CCPoint(0.5, 0.5));
    //label->setRotationX(15);
     
    /* 张开画布,开始绘画 */
    rt->begin();
	this->setFlipY(true);
    for(int i = 0; i < 360; i += 5)//每变化5度绘制一张
    {
        float r = CC_DEGREES_TO_RADIANS(i); //度数格式的转换
        this->setPosition(CCPoint(textureSize.width * 0.5f + sin(r) * strokeValue,textureSize.height * 0.5f + cos(r) * strokeValue));
 
        /* CCRenderTexture的用法,在begin和end之间visit的纹理,都会画在CCRenderTexture里面 */
        this->visit();//画了一次该label
    }
 
    /* 恢复原始的label并绘制在最上层 */
    this->setColor(color1);
    this->setBlendFunc(originalBlend);
    this->setPosition(CCPoint(textureSize.width * 0.5f, textureSize.height * 0.5f));
    this->visit();
	this->setFlipY(false);
    /* 在画布上绘制结束,此时会生成一张纹理 */
    rt->end();
     
    /* 取出生成的纹理,添加抗锯齿打磨,并返回 */
    CCTexture2D *texture = rt->getSprite()->getTexture();
    texture->setAntiAliasTexParameters();// setAliasTexParameters();
	this->setTexture(texture);
	// release it
	rt->release();

	//set the size in the sprite
		rect =CCRectZero;
	rect.size   = m_pobTexture->getContentSize();

	//this->setFlipY(true);
	//this->setFlipX(true);
	this->setTextureRect(rect);
	//if(!this->isFlipY())
	//	this->setFlipY(true);
	this->setAnchorPoint(anchPoint);
	this->setColor(color1);
	this->setPosition(prePos);

	/***************************给文字描黑边end*********************************/
    //ok
    return true;
}
コード例 #9
0
ファイル: HelloWorldScene.cpp プロジェクト: mikesimb/Classes
void HelloWorld::draw()
{
    //这里需要绘制格子了。。
    CCPoint pt1;
    CCPoint pt2;
    for (int i = 0 ; i < 16 ; i++)
    {
        for (int j =0 ; j < 8; j++) {
            int x = m_CurrentMapStartPos.y +j;
            int y = m_CurrentMapStartPos.x +i;
            if (map[x][y] != 1) {
                ccDrawColor4B(255, 0, 0, 125);
                
            }
            else
            {
                if(map[x][y] == 2){
                    ccDrawColor4B(0, 255, 255, 125);
                }
                else
                {
                    ccDrawColor4B(0, 255, 0, 125);
                }
                
            }
            pt1.x = i*64 ;
            pt1.y = j*64+m_border;
            pt2.x = (i+1)*64;
            pt2.y = (j+1)*64+m_border;
            ccDrawRect(pt1, pt2);
            
            {
               char  num[20];
               CCTexture2D* tex = new CCTexture2D();
               sprintf(num, "%d,%d",(int)m_CurrentMapStartPos.x + i,(int)m_CurrentMapStartPos.y +j);
                tex->initWithString(num, "Arial", 15);
               CCRect rect = CCRectMake(pt1.x,pt1.y,30,30);
               tex->drawInRect(rect);
               tex->autorelease();
            }
            
        }
    }
    
    char  PlayerPos[20];
    CCTexture2D* tex = new CCTexture2D();
    sprintf(PlayerPos, "playerPos:%d,%d",(int)m_CurrentPos.x,(int)m_CurrentPos.y);
    tex->initWithString(PlayerPos, "Arial", 15);
    CCRect rect = CCRectMake(10,700,100,30);
    tex->drawInRect(rect);
    tex->autorelease();
    
    char  CurrentmapstartPos[20];
    CCTexture2D* tex1 = new CCTexture2D();
    sprintf(CurrentmapstartPos, "CurrentmapstartPos:%d,%d",(int)m_CurrentMapStartPos.x,(int)m_CurrentMapStartPos.y);
    tex1->initWithString(CurrentmapstartPos, "Arial", 15);
    CCRect rect1 = CCRectMake(10,730,100,30);
    tex1->drawInRect(rect1);
    tex1->autorelease();
    


    CCLayer::draw();
    
    
}