Ejemplo n.º 1
0
void Sprite::setTexture(Texture2D *texture)
{
    // accept texture==nil as argument
    FKAssert( !texture || dynamic_cast<Texture2D*>(texture), "setTexture expects a Texture2D. Invalid argument");

    if (texture == nullptr)
    {
        // Gets the texture by key firstly.
        texture = new Texture2D();
		//texture->initData(fk_2x2_white_image,16);

        // If texture wasn't in cache, create it from RAW data.
        if (texture == nullptr)
        {
            Image* image = new (std::nothrow) Image();
            bool isOK = image->initWithRawData(fk_2x2_white_image, sizeof(fk_2x2_white_image), 2, 2, 8);
            FK_UNUSED_PARAM(isOK);
            FKAssert(isOK, "The 2x2 empty texture was created unsuccessfully.");

            //texture = Director::getInstance()->getTextureCache()->addImage(image, FK_2x2_WHITE_IMAGE_KEY);
			delete(image );
            //FK_SAFE_RELEASE(image);
        }
    }

    if (_texture != texture)
    {
        FK_SAFE_RETAIN(texture);
        FK_SAFE_RELEASE(_texture);
        _texture = texture;
		
		updateColor();
        updateBlendFunc();
    }
}
Ejemplo n.º 2
0
void Sprite::reorderChild(Entity *child, int zOrder)
{
    FKAssert(child != nullptr, "child must be non null");
    FKAssert(children->containsObject(child), "child does not belong to this");

    /*if( _batchNode && ! _reorderChildDirty)
    {
        setReorderChildDirtyRecursively();
        _batchNode->reorderBatch(true);
    }*/

    Entity::reorderChild(child, zOrder);
}
Ejemplo n.º 3
0
bool Sprite::initWithTexture(Texture2D *texture)
{
    FKAssert(texture != nullptr, "Invalid texture for sprite");

    Rect rect = RectMake(0,0,0,0);
    rect.size = texture->getContentSize();

    return initWithTexture(texture, rect);
}
Ejemplo n.º 4
0
void Object::release(void)
{
    FKAssert(m_uReference > 0, "reference count should greater than 0");
    --m_uReference;

    if (m_uReference == 0)
    {
        delete this;
    }
}
Ejemplo n.º 5
0
	//--------------------------------------------------------------------
	void CButton::SetCaption( const std::wstring& p_szCaption )
	{
		FKAssert( m_pFont != NULL , "Button不允许字体为空" );

		IGUIControl::SetCaption( p_szCaption );

		// 居中显示
		int nLen = (int)m_szCaption.length();
		m_TextPos.x = ( m_nWidth - static_cast<int>(m_pFont->TextWidth( p_szCaption) ) ) / 2;
		m_TextPos.y = ( m_nHeight - static_cast<int>(m_pFont->Height()) ) / 2 ;
	}
Ejemplo n.º 6
0
//--------------------------------------------------------------------
void CIniFile::Load( CFileDataStream* p_pFile )
{
    FKAssert( p_pFile != NULL, "INI文件不允许读取空文件流" );

    std::wstring::size_type Pos, Pos2;
    SSectionInfo si;

    // 逐行读取文件,放置入链表
    std::wstring szLineBuffer;
    while( ! (szLineBuffer = p_pFile->GetLine()).empty() )
    {
        m_lData.push_back( szLineBuffer );
    }

    // 读取文件名
    m_szFileName = p_pFile->GetName();

    // 删除文件
    if( p_pFile != NULL )
    {
        delete p_pFile;
        p_pFile = NULL;
    }

    // 记录所有段位置
    for( ListIter it = m_lData.begin(); it != m_lData.end(); ++it )
    {
        // 去掉前导空白
        Pos = ( *it ).find_first_not_of( ' ' );

        // 空白行,直接跳过
        if( Pos == std::wstring::npos )
        {
            continue;
        }

        // 查找段
        if( (*it)[Pos] == '[' )
        {
            Pos2 = (*it).find( ']', Pos );

            // 找到第一个段
            if( Pos2 != std::wstring::npos )
            {
                si.m_szName	= (*it).substr( Pos + 1, Pos2 - Pos - 1 );
                si.m_ItePos = it;
                si.m_ItePos++;
                m_vecSection.push_back( si );
            }
        }
    }

    m_nCurSection = m_vecSection.size() > 0 ? 0 : -1;
}
Ejemplo n.º 7
0
//--------------------------------------------------------------------
void CIniFile::WriteString(const std::wstring& szKey, const std::wstring& szValue)
{
    FKAssert( m_nCurSection >= 0 , L"WriteString不允许写入空字符串" );

    ListIter it = m_vecSection[ m_nCurSection ].m_ItePos;
    std::wstring strWrite = szKey;
    strWrite += L"=";
    strWrite += szValue;

    while( it != m_lData.end() )
    {
        std::wstring::size_type Pos;

        // 去掉前导空白
        Pos = (*it).find_first_not_of( ' ' );

        // 空白行,直接跳过
        if( Pos == std::wstring::npos )
        {
            ++it;
            continue;
        }

        // 已到下个段,查找失败
        if( (*it)[Pos] == '[' )
        {
            // 新建一个段,返回
            m_lData.insert( m_vecSection[m_nCurSection].m_ItePos, strWrite );
            return;
        }

        // 查找键名
        if( (*it).substr( Pos, szKey.length() ) == szKey )
        {
            Pos += szKey.length();

            // 去掉等号前空白
            Pos = (*it).find_first_not_of( ' ', Pos );

            // 等号存在,已经找到指定键
            if(( Pos != std::wstring::npos ) && ( (*it)[Pos] == '=' ))
            {
                (*it) = strWrite;
                return;
            }
        }

        ++it;
    }

    // 到链尾也没找到,新建一个键
    m_lData.insert( m_vecSection[m_nCurSection].m_ItePos, strWrite );
}
Ejemplo n.º 8
0
bool Sprite::initWithFile(const std::string &filename, const Rect& rect)
{
    FKAssert(filename.size()>0, "Invalid filename");

    Texture2D *texture = new Texture2D();
	Image* image = dynamic_cast<Image*>(ResourceManager::thisManager()->createResource(filename.c_str(),ResourceManager::IMAGE_NAME));
	image->load(false);
	texture->initWithImage(image);

    if (texture)
    {
        return initWithTexture(texture, rect);
    }

    // don't release here.
    // when load texture failed, it's better to get a "transparent" sprite then a crashed program
    // this->release();
    return false;
}
Ejemplo n.º 9
0
void Sprite::addChild(Entity *child, int zOrder, int tag)
{
    FKAssert(child != nullptr, "Argument must be non-nullptr");

	/*
    if (_batchNode)
    {
        Sprite* childSprite = dynamic_cast<Sprite*>(child);
        FKAssert( childSprite, "Sprite only supports Sprites as children when using SpriteBatchNode");
        FKAssert(childSprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "");
        //put it in descendants array of batch node
        _batchNode->appendChild(childSprite);

        if (!_reorderChildDirty)
        {
            setReorderChildDirtyRecursively();
        }
    }*/
 
    //Entity already sets isReorderChildDirty_ so this needs to be after batchNode check
    Entity::addChild(child, zOrder, tag);
}
Ejemplo n.º 10
0
bool Sprite::initWithFile(const std::string& filename)
{
    FKAssert(filename.size()>0, "Invalid filename for sprite");

    Texture2D *texture = new Texture2D();
		FKLOG("create image here");
	Image* image = dynamic_cast<Image*>(ResourceManager::thisManager()->createResource(filename.c_str(),ResourceManager::IMAGE_NAME));
	image->load(false);

	texture->initWithImage(image);
    if (texture)
    {
        Rect rect = RectZero;
        rect.size = texture->getContentSize();
        FKLOG("Sprite size:w %f,h %f",rect.size.width,rect.size.height);
        return initWithTexture(texture, rect);
    }

    // don't release here.
    // when load texture failed, it's better to get a "transparent" sprite then a crashed program
    // this->release();
    return false;
}
Ejemplo n.º 11
0
void Object::retain(void)
{
    FKAssert(m_uReference > 0, "reference count should greater than 0");

    ++m_uReference;
}
Ejemplo n.º 12
0
//--------------------------------------------------------------------
std::wstring CIniFile::ReadString(const std::wstring& szKey, const std::wstring& szDefault)
{
    FKAssert( m_nCurSection >= 0, L"不能对空文件读取字符串" );

    std::wstring str;
    ListIter it = m_vecSection[m_nCurSection].m_ItePos;

    while( it != m_lData.end() )
    {
        std::wstring::size_type Pos;

        // 去掉前导空白
        Pos = ( *it ).find_first_not_of( ' ' );

        // 空白行,直接跳过
        if( Pos == std::wstring::npos )
        {
            ++it;
            continue;
        }

        // 查找段
        if( (*it)[Pos] == '[' )
        {
            return szDefault;
        }

        // 查找键名
        if( (*it).substr( Pos, szKey.length() ) == szKey )
        {
            Pos += szKey.length();

            // 去掉等号前空白
            Pos = (*it).find_first_not_of( ' ', Pos );

            // 等号存在,已找到指定键
            if( (Pos != std::wstring::npos) && ((*it)[Pos] == '=') )
            {
                ++Pos;

                if( (*it)[Pos] != '\0' )
                {
                    // 去掉等号后空白
                    Pos = (*it).find_first_not_of( ' ', Pos );

                    if( Pos != std::wstring::npos )
                    {
                        str = (*it).substr( Pos );

                        // 去掉字符串尾部空白
                        str.erase( find_if( str.rbegin(), str.rend(),
                                            std::ptr_fun( IsNotSpace )).base(), str.end() );

                        return str;
                    }
                }

                str = szDefault;
                return str;
            }
        }

        ++it;
    }

    str = szDefault;
    return str;
}