예제 #1
0
void Sprite::setTexture(Texture2D *texture)
{
    // If batchnode, then texture id should be the same
    CCASSERT(! _batchNode || texture->getName() == _batchNode->getTexture()->getName(), "CCSprite: Batched sprites should use the same texture as the batchnode");
    // accept texture==nil as argument
    CCASSERT( !texture || dynamic_cast<Texture2D*>(texture), "setTexture expects a Texture2D. Invalid argument");

    if (texture == nullptr)
    {
        // Gets the texture by key firstly.
        texture = Director::getInstance()->getTextureCache()->getTextureForKey(CC_2x2_WHITE_IMAGE_KEY);

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

            texture = Director::getInstance()->getTextureCache()->addImage(image, CC_2x2_WHITE_IMAGE_KEY);
            CC_SAFE_RELEASE(image);
        }
    }

    if (!_batchNode && _texture != texture)
    {
        CC_SAFE_RETAIN(texture);
        CC_SAFE_RELEASE(_texture);
        _texture = texture;
        updateBlendFunc();
    }
}
예제 #2
0
파일: Sprite.cpp 프로젝트: sainthsu/Flakor
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();
    }
}
예제 #3
0
bool CCSpriteBatchNode::initWithTexture(CCTexture2D *tex, unsigned int capacity, CCTextureAtlas* atlas)
{
    m_blendFunc.src = CC_BLEND_SRC;
    m_blendFunc.dst = CC_BLEND_DST;
    m_pobTextureAtlas = atlas ? (atlas->retain(), atlas) : new CCTextureAtlas();

    if (0 == capacity)
    {
        capacity = kDefaultSpriteBatchCapacity;
    }
    
    m_pobTextureAtlas->initWithTexture(tex, capacity);

    updateBlendFunc();

    // no lazy alloc in this node
    m_pChildren = new CCArray();
    m_pChildren->initWithCapacity(capacity);

    m_pobDescendants = new CCArray();
    m_pobDescendants->initWithCapacity(capacity);

    setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
    return true;
}
예제 #4
0
/*
* init with Texture2D
*/
bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity/* = DEFAULT_CAPACITY*/)
{
    CCASSERT(capacity>=0, "Capacity must be >= 0");
    
    _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
    if(!tex->hasPremultipliedAlpha())
    {
        _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
    }
    _textureAtlas = new (std::nothrow) TextureAtlas();

    if (capacity == 0)
    {
        capacity = DEFAULT_CAPACITY;
    }
    
    _textureAtlas->initWithTexture(tex, capacity);

    updateBlendFunc();

    _children.reserve(capacity);

    _descendants.reserve(capacity);
    
    setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
    return true;
}
예제 #5
0
void TextureSprite::setTexture(CCTexture2D *texture)
{
    if (texture != mTexture2D) {
        CC_SAFE_RETAIN(texture);
        CC_SAFE_RELEASE(mTexture2D);
        mTexture2D = texture;
        /// Since we use multi-resolution we need to get the texture size in points (not pixels)
        /// mTexSize = mTexture2D->getContentSizeInPixels();
        mTexSize = mTexture2D->getContentSize();
        updateBlendFunc();
    }
}
void BillboardParticleSystem::setTextureWithRect(Texture2D *texture, const Rect& rect)
{
    // Only update the texture if is different from the current one
    if( !_texture || texture->getName() != _texture->getName() )
    {
        if (_texture != texture)
        {
            CC_SAFE_RETAIN(texture);
            CC_SAFE_RELEASE(_texture);
            _texture = texture;
            updateBlendFunc();
        }
    }
    this->initTexCoordsWithRect(rect);
}
예제 #7
0
	/*
	* init with CCTexture2D
	*/
	bool CCSpriteBatchNode::initWithTexture(cocos2d::CCTexture2D *tex, unsigned int capacity)
	{
		m_blendFunc.src = CC_BLEND_SRC;
		m_blendFunc.dst = CC_BLEND_DST;
		m_pobTextureAtlas = new CCTextureAtlas();
		m_pobTextureAtlas->initWithTexture(tex, capacity);

		updateBlendFunc();

		// no lazy alloc in this node
		m_pChildren = new NSMutableArray<CCNode*>();
		m_pobDescendants = new NSMutableArray<CCSprite*>();

		return true;
	}
예제 #8
0
void GAFSprite::setTexture(cocos2d::Texture2D *texture)
{
    // If batchnode, then texture id should be the same
    CCAssert(!_batchNode || texture->getName() == _batchNode->getTexture()->getName(), "cocos2d::Sprite: Batched sprites should use the same texture as the batchnode");
    // accept texture==nil as argument
    CCAssert(!texture || dynamic_cast<cocos2d::Texture2D*>(texture), "setTexture expects a cocos2d::Texture2D. Invalid argument");

    if (!_batchNode && _texture != texture)
    {
        CC_SAFE_RETAIN(texture);
        CC_SAFE_RELEASE(_texture);
        _texture = texture;
        updateBlendFunc();
    }
}
예제 #9
0
void PRFilledPolygon::setTexture(CCTexture2D* texture2D) {
    
    
    CCAssert(texture2D, "NO TEXTURE SET");
    CC_SAFE_RELEASE(texture);
	
    texture = texture2D;
    CC_SAFE_RETAIN(texture);
    
	ccTexParams texParams = { GL_NEAREST, GL_NEAREST, GL_REPEAT, GL_REPEAT };
	texture->setTexParameters(&texParams);
    
    updateBlendFunc();
	calculateTextureCoordinates();
    
}
	/*
	* init with CCTexture2D
	*/
	bool CCSpriteBatchNode::initWithTexture(CCTexture2D *tex, unsigned int capacity)
	{
		m_blendFunc.src = CC_BLEND_SRC;
		m_blendFunc.dst = CC_BLEND_DST;
		m_pobTextureAtlas = new CCTextureAtlas();
		m_pobTextureAtlas->initWithTexture(tex, capacity);

		updateBlendFunc();

		// no lazy alloc in this node
        m_pChildren = CCArray::array();
		m_pobDescendants = CCArray::array();
        m_pChildren->retain();
        m_pobDescendants->retain();

		return true;
	}
예제 #11
0
bool CABatchView::initWithImage(CAImage *image, unsigned int capacity)
{
    m_blendFunc.src = CC_BLEND_SRC;
    m_blendFunc.dst = CC_BLEND_DST;
    m_pobImageAtlas = new CAImageAtlas();

    if (0 == capacity)
    {
        capacity = kDefaultSpriteBatchCapacity;
    }
    
    m_pobImageAtlas->initWithImage(image, capacity);

    updateBlendFunc();

    setShaderProgram(CAShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
    return true;
}
예제 #12
0
void EasyPolygon::setTexture(Texture2D* texture2D) {
    
    
    CCAssert(texture2D, "NO TEXTURE SET");
    CC_SAFE_RELEASE(texture);
	
    texture = texture2D;
    CC_SAFE_RETAIN(texture);
    
    textureWidth = texture->getPixelsWide();
    
	cocos2d::Texture2D::TexParams texParams = { GL_NEAREST, GL_NEAREST, GL_REPEAT, GL_REPEAT };
    texture->setTexParameters(texParams);
    
    updateBlendFunc();
	calculateTextureCoordinates();
    
}
예제 #13
0
void CCSprite::setTexture(CCTexture2D *texture)
{
	// CCSprite: setTexture doesn't work when the sprite is rendered using a CCSpriteSheet
	assert(! m_bUsesBatchNode);

	// we can not use RTTI, so we do not known the type of object
	// accept texture==nil as argument
	/*assert((! texture) || dynamic_cast<CCTexture2D*>(texture));*/

	CCX_SAFE_RELEASE(m_pobTexture);

	m_pobTexture = texture;
	if (texture)
	{
	    texture->retain();
	}

	updateBlendFunc();
}
/*
 * init with CCTexture2D
 */
bool CCParticleBatchNode::initWithTexture(CCTexture2D *tex, unsigned int capacity)
{



    m_tBlendFunc.src = CC_BLEND_SRC;
    m_tBlendFunc.dst = CC_BLEND_DST;
    m_pTextureAtlas = new CCTextureAtlas();
    m_pTextureAtlas->initWithTexture(tex, capacity);
    updateBlendFunc();
    //CCAssert(false, "Not implemented.");
    //// no lazy alloc in this node
    m_pChildren = new CCArray();
    m_pChildren->initWithCapacity(capacity);
    m_pChildren->retain();



    //setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));

    return true;
}
예제 #15
0
/*
* init with Texture2D
*/
bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity)
{
    CCASSERT(capacity>=0, "Capacity must be >= 0");
    
    _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
    _textureAtlas = new TextureAtlas();

    if (capacity == 0)
    {
        capacity = DEFAULT_CAPACITY;
    }
    
    _textureAtlas->initWithTexture(tex, capacity);

    updateBlendFunc();

    _children.reserve(capacity);

    _descendants.reserve(capacity);
    
    setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
    return true;
}
예제 #16
0
void CAView::setImage(CAImage* image)
{
    if (!m_pobBatchView)
    {
        CC_SAFE_RETAIN(image);
        CC_SAFE_RELEASE(m_pobImage);
        m_pobImage = image;
        if (image)
        {
            if (image->getPixelFormat() == CAImage::PixelFormat_A8)
            {
                this->setShaderProgram(CAShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureA8Color));
            }
            else
            {
                this->setShaderProgram(CAShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
            }
        }
        
        updateBlendFunc();
        this->updateDraw();
    }
}
예제 #17
0
void CABatchView::setImage(CAImage *image)
{
    m_pobImageAtlas->setImage(image);
    updateBlendFunc();
}
예제 #18
0
void SpriteBatchNode::setTexture(Texture2D *texture)
{
    _textureAtlas->setTexture(texture);
    updateBlendFunc();
}
예제 #19
0
	void CCSpriteBatchNode::setTexture(CCTexture2D *texture)
	{
		m_pobTextureAtlas->setTexture(texture);
		updateBlendFunc();
	}
    bool Scale9Sprite::updateWithSprite(Sprite* sprite, const Rect& textureRect, bool rotated, const Vec2 &offset, const Size &originalSize, const Rect& capInsets)
    {
        GLubyte opacity = getOpacity();
        Color3B color = getColor();
        
        // Release old sprites
        this->cleanupSlicedSprites();
        _protectedChildren.clear();
        
        updateBlendFunc(sprite?sprite->getTexture():nullptr);

        if(nullptr != sprite)
        {
            if (nullptr == sprite->getSpriteFrame())
            {
                return false;
            }
            
            if (nullptr == _scale9Image)
            {
                _scale9Image = sprite;
                _scale9Image->retain();
            }
            else
            {
                _scale9Image->setSpriteFrame(sprite->getSpriteFrame());
            }
        }
        
        if (!_scale9Image)
        {
            return false;
        }
        
        SpriteFrame *spriteFrame = _scale9Image->getSpriteFrame();
        
        if (!spriteFrame)
        {
            return false;
        }
        
        Rect rect(textureRect);
        Size size(originalSize);
        
        _capInsets = capInsets;
        
        // If there is no given rect
        if ( rect.equals(Rect::ZERO) )
        {
            // Get the texture size as original
            Size textureSize = _scale9Image->getTexture()->getContentSize();
            
            rect = Rect(0, 0, textureSize.width, textureSize.height);
        }
        
        if( size.equals(Size::ZERO) )
        {
            size = rect.size;
        }
        
        // Set the given rect's size as original size
        _spriteRect = rect;
        _offset = offset;
        _spriteFrameRotated = rotated;
        _originalSize = size;
        _preferredSize = size;
        _capInsetsInternal = capInsets;
        
        if (_scale9Enabled)
        {
            this->createSlicedSprites();
        }
        
        applyBlendFunc();
        
        this->setContentSize(size);
        
        if (_spritesGenerated)
        {
            // Restore color and opacity
            this->setOpacity(opacity);
            this->setColor(color);
        }
        _spritesGenerated = true;
        
        return true;
    }
예제 #21
0
    bool Scale9Sprite::updateWithSprite(Sprite* sprite,
                                        const Rect& textureRect,
                                        bool rotated,
                                        const Vec2 &offset,
                                        const Size &originalSize,
                                        const Rect& capInsets)
    {
        // Release old sprites
        this->cleanupSlicedSprites();

        updateBlendFunc(sprite?sprite->getTexture():nullptr);

        if(nullptr != sprite)
        {
            if (nullptr == sprite->getSpriteFrame())
            {
                return false;
            }

            if (nullptr == _scale9Image)
            {
                _scale9Image = sprite;
                _scale9Image->retain();
            }
            else
            {
                _scale9Image->setSpriteFrame(sprite->getSpriteFrame());
            }
        }
        else
        {
            CC_SAFE_RELEASE_NULL(_scale9Image);
        }

        if (!_scale9Image)
        {
            return false;
        }

        Rect rect(textureRect);
        Size size(originalSize);
        
        // If there is no given rect
        if ( rect.equals(Rect::ZERO) )
        {
            // Get the texture size as original
            Size textureSize = _scale9Image->getTexture()->getContentSize();

            rect = Rect(0, 0, textureSize.width, textureSize.height);
        }

        if( size.equals(Size::ZERO) )
        {
            size = rect.size;
        }

        // Set the given rect's size as original size
        _spriteRect = rect;
        _spriteFrameRotated = rotated;
        _originalSize = size;
        _preferredSize = size;
        _offset = offset;

        _capInsetsInternal = capInsets;

        if (_scale9Enabled)
        {
            _scale9Image->setAnchorPoint(Vec2::ZERO);
            _scale9Image->setPosition(Vec2::ZERO);
            _sliceSpriteDirty = true;
        }

        applyBlendFunc();
        if (getGLProgramState()) {
            _scale9Image->setGLProgramState(getGLProgramState());
        } else {
            this->setState(_brightState);
        }
        if(this->_isPatch9)
        {
            size.width = size.width - 2;
            size.height = size.height - 2;
        }
        this->setContentSize(size);
        
        return true;
    }