void Sprite::setTextureRect(const Rect& rect, bool rotated, const Size& untrimmedSize)
{
    _rectRotated = rotated;

    setContentSize(untrimmedSize);
    setVertexRect(rect);
    setTextureCoords(rect);

    float relativeOffsetX = _unflippedOffsetPositionFromCenter.x;
    float relativeOffsetY = _unflippedOffsetPositionFromCenter.y;

    // issue #732
    if (_flippedX)
    {
        relativeOffsetX = -relativeOffsetX;
    }
    if (_flippedY)
    {
        relativeOffsetY = -relativeOffsetY;
    }

    _offsetPosition.x = relativeOffsetX + (_contentSize.width - _rect.size.width) / 2;
    _offsetPosition.y = relativeOffsetY + (_contentSize.height - _rect.size.height) / 2;

    // rendering using batch node
    if (_batchNode)
    {
        // update dirty_, don't update recursiveDirty_
        setDirty(true);
    }
    else
    {
        // self rendering

        // Atlas: Vertex
        float x1 = 0.0f + _offsetPosition.x;
        float y1 = 0.0f + _offsetPosition.y;
        float x2 = x1 + _rect.size.width;
        float y2 = y1 + _rect.size.height;

        // Don't update Z.
        _quad.bl.vertices.set(x1, y1, 0.0f);
        _quad.br.vertices.set(x2, y1, 0.0f);
        _quad.tl.vertices.set(x1, y2, 0.0f);
        _quad.tr.vertices.set(x2, y2, 0.0f);
    }

    _polyInfo.setQuad(&_quad);
}
Exemple #2
0
bool GameScene::init() {
    Scene = this;
    
    ShaderLayer::init("shaders/vignette.glsl");
    
    rendTexSprite->getGLProgramState()->setUniformVec2("darkness", Vec2(1,1));
    
    mGame = new GameLogic(this);
    mGame->mWinGameEvent = [this]{onWinGame();};
    createControlPad();
    createMenuButtons();
    
#if 0
    Rect r = VisibleRect::getVisibleRect();
    
    Size sz(r.size);
    auto testsp = Sprite::create();
    testsp->setContentSize(sz);
    testsp->setTag(1024);
    
    testsp->setTexture(renderTextureBlur->getSprite()->getTexture());
    addChild(testsp,5000);
    
    r.size = sz;
    testsp->setTextureRect(r);
    
    auto ruv = r;
    ruv.size = renderTexture->getSprite()->getTexture()->getContentSizeInPixels() / 4;
    testsp->setPosition(VisibleRect::center());
    testsp->setFlippedY(true);
    testsp->setTextureCoords(ruv);
    //testsp->addEffect(EffectBloom::create(), 1);
    //testsp->addEffect(EffectBlur::create(), 2);
    
    testsp->setBlendFunc({GL_ONE,GL_ONE});
    //auto visibleRect = VisibleRect::getVisibleRect();
    //testsp->setScale(visibleRect.size.width / sz.width, visibleRect.size.height / sz.height);
#endif
    
    return true;
}
Exemple #3
0
void Sprite::setTextureRect(const Rect& rect, bool rotated, const Size& untrimmedSize)
{
    _rectRotated = rotated;

    
    setContentSize(untrimmedSize);
    setVertexRect(rect);
    setTextureCoords(rect);


    _offsetPosition.x = (contentSize.width - _rect.size.width) / 2;
    _offsetPosition.y = (contentSize.height - _rect.size.height) / 2;

    // self rendering
        
    // Atlas: Vertex
    float x1 = 0 + _offsetPosition.x;
    float y1 = 0 + _offsetPosition.y;
    float x2 = x1 + _rect.size.width;
    float y2 = y1 + _rect.size.height;
    
	FKLOG("Sprite vertexs:x1 %.4f,y1 %.4f,x2 %.4f,y2 %.4f",x1,y1,x2,y2);
	/*
	* _____(x2,y2)
	  |          | 
	  |(x1,y1)___|	

	   3_____4
	  |      |
	  |1____2|	
	*/

	//leftB rightTop;
	float vertexs[] = {x1,y1,0,
					   x2,y1,0,
					   x1,y2,0,
					   x2,y2,0};
	//{ -0.5,-0.5,0,0.5,-0.5,0,-0.5,0.5,0,0.5,0.5,0};
	_vbo->updateData(VERTEX_INDEX,3,vertexs);
	
}
void Sprite::updateTransform()
{
    CCASSERT(_batchNode, "updateTransform is only valid when Sprite is being rendered using an SpriteBatchNode");

    // recalculate matrix only if it is dirty
    if( isDirty() ) {

        // If it is not visible, or one of its ancestors is not visible, then do nothing:
        if( !_visible || ( _parent && _parent != _batchNode && static_cast<Sprite*>(_parent)->_shouldBeHidden) )
        {
            _quad.br.vertices.setZero();
            _quad.tl.vertices.setZero();
            _quad.tr.vertices.setZero();
            _quad.bl.vertices.setZero();
            _shouldBeHidden = true;
        }
        else
        {
            _shouldBeHidden = false;

            if( ! _parent || _parent == _batchNode )
            {
                _transformToBatch = getNodeToParentTransform();
            }
            else
            {
                CCASSERT( dynamic_cast<Sprite*>(_parent), "Logic error in Sprite. Parent must be a Sprite");
                const Mat4 &nodeToParent = getNodeToParentTransform();
                Mat4 &parentTransform = static_cast<Sprite*>(_parent)->_transformToBatch;
                _transformToBatch = parentTransform * nodeToParent;
            }

            //
            // calculate the Quad based on the Affine Matrix
            //

            Size &size = _rect.size;

            float x1 = _offsetPosition.x;
            float y1 = _offsetPosition.y;

            float x2 = x1 + size.width;
            float y2 = y1 + size.height;

            float x = _transformToBatch.m[12];
            float y = _transformToBatch.m[13];

            float cr = _transformToBatch.m[0];
            float sr = _transformToBatch.m[1];
            float cr2 = _transformToBatch.m[5];
            float sr2 = -_transformToBatch.m[4];
            float ax = x1 * cr - y1 * sr2 + x;
            float ay = x1 * sr + y1 * cr2 + y;

            float bx = x2 * cr - y1 * sr2 + x;
            float by = x2 * sr + y1 * cr2 + y;

            float cx = x2 * cr - y2 * sr2 + x;
            float cy = x2 * sr + y2 * cr2 + y;

            float dx = x1 * cr - y2 * sr2 + x;
            float dy = x1 * sr + y2 * cr2 + y;

            _quad.bl.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(ax), SPRITE_RENDER_IN_SUBPIXEL(ay), _positionZ);
            _quad.br.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(bx), SPRITE_RENDER_IN_SUBPIXEL(by), _positionZ);
            _quad.tl.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(dx), SPRITE_RENDER_IN_SUBPIXEL(dy), _positionZ);
            _quad.tr.vertices.set(SPRITE_RENDER_IN_SUBPIXEL(cx), SPRITE_RENDER_IN_SUBPIXEL(cy), _positionZ);
            setTextureCoords(_rect);
        }

        // MARMALADE CHANGE: ADDED CHECK FOR nullptr, TO PERMIT SPRITES WITH NO BATCH NODE / TEXTURE ATLAS
        if (_textureAtlas)
        {
            _textureAtlas->updateQuad(&_quad, _atlasIndex);
        }

        _recursiveDirty = false;
        setDirty(false);
    }

    Node::updateTransform();
}
Exemple #5
0
/**
 * 设置数据
 */
 void MapCoordLine::setTextureRect(const CCRect& rect, bool rotated, const CCSize& untrimmedSize)
 {
	m_bRectRotated = rotated;

    setContentSize(untrimmedSize);
    setVertexRect(rect);
    setTextureCoords(rect);  // 设置纹理坐标

    CCPoint relativeOffset = m_obUnflippedOffsetPositionFromCenter;

    // issue #732
    if (m_bFlipX)
    {
        relativeOffset.x = -relativeOffset.x;
    }
    if (m_bFlipY)
    {
        relativeOffset.y = -relativeOffset.y;
    }

    m_obOffsetPosition.x = relativeOffset.x + (m_tContentSize.width - m_obRect.size.width) / 2;
    m_obOffsetPosition.y = relativeOffset.y + (m_tContentSize.height - m_obRect.size.height) / 2;

    // rendering using batch node
    if (m_pobBatchNode)
    {
        // update dirty_, don't update recursiveDirty_
        setDirty(true);
    }
    else
    {
        // self rendering
        
        // Atlas: Vertex
        float x = 0 + m_obOffsetPosition.x;
        float y = 0 + m_obOffsetPosition.y;
		// Atlas: Color
		ccColor4B tmpColor = { 67,67, 76, 255 };
	
		// 设置顶点坐标
        // Don't update Z.
  
		int eachWidth = _eachWidth;
		int eachHeight = _eachHeight;
		if (eachWidth > _width) eachWidth = _width;
		if (eachHeight > _height) eachHeight = _height;

		std::vector<ccV3F_C4B_T2F> tpoints;
		int widthSize =	_width / eachWidth ;
		int heightSize = _height/ eachHeight;
		for (int i = 0; i < widthSize + 1;i++)
		{ // 生成横向点
			ccV3F_C4B_T2F point;
			point.vertices = vertex3(i * eachWidth + x,0,0); // y,z 均为0
			tpoints.push_back(point);
			point.vertices = vertex3(i * eachWidth + x,_height,0); // y 为最高值z 均为0
			tpoints.push_back(point);
		}
		for (int j = 0; j < heightSize + 1;j++)
		{
			ccV3F_C4B_T2F point;
			point.vertices = vertex3(0,j * eachHeight + y,0); // y,z 均为0
			tpoints.push_back(point);
			point.vertices = vertex3(_width,j * eachHeight + y,0); // y 为最高值z 均为0
			tpoints.push_back(point);
		} // 生成顶点集合
		for (int i = 0; i < tpoints.size()/2;i++)
		{
			ccV3F_C4B_T2F p;
			static int indexs[] = {0,1}; // 固态的指定渲染的引索号 
			static float textures[] = {0,1,0,0,1,1,1,0}; // 纹理映射点
			for (int z = 0; z < 2;z++)
			{
				p.vertices = tpoints[i*2+indexs[z]].vertices;
				p.colors = tmpColor;
				p.texCoords.u = textures[indexs[z]*2];
				p.texCoords.v = textures[indexs[z]*2+1];
				points.push_back(p);
			}
		}
	}
 }