void DynamicSprite::updateQuads() { auto tex = getTexture(); if (!tex) { _quads->clear(); return; } float atlasWidth = (float)tex->getPixelsWide(); float atlasHeight = (float)tex->getPixelsHigh(); cocos2d::Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity ); if (_opacityModifyRGB) { color4.r *= _displayedOpacity/255.0f; color4.g *= _displayedOpacity/255.0f; color4.b *= _displayedOpacity/255.0f; } _quads->resize(1); if (!_normalized) { _quads->setTextureRect(0, _textureRect, atlasWidth, atlasHeight, _flippedX, _flippedY, _rotated); _quads->setGeometry(0, _textureOrigin, _textureSize, _positionZ); } else { _quads->setNormalizedGeometry(0, cocos2d::Vec2::ZERO, _positionZ, _textureRect, atlasWidth, atlasHeight, _flippedX, _flippedY, _rotated); } _quads->setColor(0, color4); }
void RepeatableSprite::initTextureRectAndPosition() { setAnchorPoint({0, 0}); setPosition({0, 0}); auto size = cocos2d::EGLView::getInstance()->getDesignResolutionSize(); setTextureRect({0, 0, size.width, size.height}); auto tex = getTexture(); if (tex) { if (tex->getPixelsWide() != cocos2d::ccNextPOT(tex->getPixelsWide()) || tex->getPixelsHigh() != cocos2d::ccNextPOT(tex->getPixelsHigh())) { throw std::invalid_argument("RepeatableSprite Texture has to be a power of two!"); } tex->setTexParameters({ GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT }); } }
void DynamicSprite::setTextureRect(const cocos2d::Rect &rect) { auto tex = getTexture(); if (rect.equals(cocos2d::Rect::ZERO) && tex) { _textureRect = cocos2d::Rect(0, 0, tex->getPixelsHigh(), tex->getPixelsWide()); _contentSizeDirty = true; } else { if (!_textureRect.equals(rect)) { _textureRect = rect; _contentSizeDirty = true; } } if (_autofit == Autofit::None) { setContentSize(cocos2d::Size(_textureRect.size.width / _density, _textureRect.size.height / _density)); } }
void Image::drawRenderer(bool updateSize) { if (m_parsing)return; Size _rendererSize = m_renderer->getContentSize(); Size _size; _size.width = m_givenSize.width < 0 ? _rendererSize.width : m_givenSize.width; _size.height = m_givenSize.height < 0 ? _rendererSize.height : m_givenSize.height; Rect _textureRect = m_renderer->getTextureRect(); if (m_repeatType == RepeatType::SCALE) { m_renderer->setScaleX(_size.width / _textureRect.size.width); m_renderer->setScaleY(_size.height / _textureRect.size.height); } else { _textureRect.size.width = _size.width; _textureRect.size.height = _size.height; auto _texture = m_renderer->getTexture(); m_renderer->setTextureRect(_textureRect); if (((_texture->getPixelsWide() % 8) == 0) && ((_texture->getPixelsHigh() % 8) == 0)) { m_renderer->getTexture()->setTexParameters({ GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT }); } } switch (m_filter) { case tina::Image::FilterType::NONE: initNormalGLprogram(); break; case tina::Image::FilterType::GRAY: initGrayGLprogram(); break; case tina::Image::FilterType::BLUR: initBlurGLprogram(); break; case tina::Image::FilterType::RADIAL_BLUR: initRadialBlurGLprogram(); break; default: break; } if (updateSize) updateSizeFromContent(_size); }
void Scale9Sprite::updateSprites() { updateRects(); float contentScaleX = 1.0f; float contentScaleY = 1.0f; if (_contentSize.width < _minContentSize.width) { contentScaleX = _contentSize.width / _minContentSize.width; } if (_contentSize.height < _minContentSize.height) { contentScaleY = _contentSize.height / _minContentSize.height; } int i, j; bool visible; float scaleX, scaleY; cocos2d::Rect texRect; cocos2d::Vec2 spritePos; auto tex = getTexture(); float atlasWidth = (float)tex->getPixelsWide(); float atlasHeight = (float)tex->getPixelsHigh(); cocos2d::Color4B color4( _displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity ); if (_opacityModifyRGB) { color4.r *= _displayedOpacity/255.0f; color4.g *= _displayedOpacity/255.0f; color4.b *= _displayedOpacity/255.0f; } size_t quadsCount = 9; size_t quadId = 0; if (contentScaleY < 1.0f && contentScaleX < 1.0f) { quadsCount = 4; } else if (contentScaleY < 1.0f || contentScaleX < 1.0f) { quadsCount = 6; } _quads->resize(quadsCount); for (int tag = 0; tag < 9; tag++) { visible = true; i = tag % 3; j = tag / 3; scaleX = contentScaleX; scaleY = contentScaleY; if (i == 1) { if (contentScaleY < 1.0f) { visible = false; } else { scaleY = _drawRect.size.height / _insetRect.size.height; } } if (j == 1) { if (contentScaleX < 1.0f) { visible = false; } else { scaleX = _drawRect.size.width / _insetRect.size.width; } } if (visible) { texRect = textureRectForGrid(i, j); spritePos = texturePositionForGrid(i, j, contentScaleX, contentScaleY); _quads->setTextureRect(quadId, texRect, atlasWidth, atlasHeight, _flipX, _flipY); _quads->setGeometry(quadId, spritePos, cocos2d::Size(texRect.size.width * scaleX, texRect.size.height * scaleY), _positionZ); _quads->setColor(quadId, color4); quadId ++; } } _quads->shrinkToFit(); }