bool Label::setTTFConfig(const TTFConfig& ttfConfig) { FontAtlas *newAtlas = FontAtlasCache::getFontAtlasTTF(ttfConfig); if (!newAtlas) { reset(); return false; } _systemFontDirty = false; _currentLabelType = LabelType::TTF; setFontAtlas(newAtlas,ttfConfig.distanceFieldEnabled,true); _fontConfig = ttfConfig; if (_fontConfig.outlineSize > 0) { _fontConfig.distanceFieldEnabled = false; _useDistanceField = false; _useA8Shader = false; _currLabelEffect = LabelEffect::OUTLINE; updateShaderProgram(); } else { _currLabelEffect = LabelEffect::NORMAL; updateShaderProgram(); if(ttfConfig.distanceFieldEnabled) { this->setFontScale(1.0f * ttfConfig.fontSize / DistanceFieldFontSize); } } return true; }
void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = -1 */) { CCASSERT(_currentLabelType == LabelType::STRING_TEXTURE || _currentLabelType == LabelType::TTF, "Only supported system font and TTF!"); _effectColor = outlineColor; _effectColorF.r = _effectColor.r / 255.0f; _effectColorF.g = _effectColor.g / 255.0f; _effectColorF.b = _effectColor.b / 255.0f; _effectColorF.a = _effectColor.a / 255.0f; if (outlineSize > 0) { _outlineSize = outlineSize; if (_currentLabelType == LabelType::TTF) { if (_fontConfig.outlineSize != outlineSize) { auto config = _fontConfig; config.outlineSize = outlineSize; setTTFConfig(config); updateShaderProgram(); } } _currLabelEffect = LabelEffect::OUTLINE; _contentDirty = true; } }
void Label::enableOutline(const Color4B& outlineColor,int outlineSize /* = -1 */) { _effectColor = outlineColor; _effectColorF.r = _effectColor.r / 255.0f; _effectColorF.g = _effectColor.g / 255.0f; _effectColorF.b = _effectColor.b / 255.0f; _effectColorF.a = _effectColor.a / 255.0f; if (outlineSize > 0) { if (_currentLabelType == LabelType::TTF) { if (_fontConfig.outlineSize != outlineSize) { auto config = _fontConfig; config.outlineSize = outlineSize; setTTFConfig(config); updateShaderProgram(); } } _fontDefinition._stroke._strokeEnabled = true; _fontDefinition._stroke._strokeSize = outlineSize; _fontDefinition._stroke._strokeColor = Color3B(outlineColor.r,outlineColor.g,outlineColor.b); _currLabelEffect = LabelEffect::OUTLINE; _contentDirty = true; } }
void ShaderEffectItem::renderEffect(QPainter *painter, const QMatrix4x4 &matrix) { if (!painter || !painter->device()) return; if (!m_program.isLinked() || m_program_dirty) updateShaderProgram(); m_program.bind(); QMatrix4x4 combinedMatrix; combinedMatrix.scale(2.0 / painter->device()->width(), -2.0 / painter->device()->height(), 1.0); combinedMatrix.translate(-painter->device()->width() / 2.0, -painter->device()->height() / 2.0 ); combinedMatrix *= matrix; updateEffectState(combinedMatrix); for (int i = 0; i < m_attributeNames.size(); ++i) { m_program.enableAttributeArray(m_geometry.attributes()[i].position); } bindGeometry(); // Optimization, disable depth test when we know we don't need it. if (m_defaultVertexShader) { glDepthMask(false); glDisable(GL_DEPTH_TEST); } else { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GREATER); glDepthMask(true); #if defined(QT_OPENGL_ES) glClearDepthf(0); #else glClearDepth(0); #endif glClearColor(0, 0, 0, 0); glClear(GL_DEPTH_BUFFER_BIT); } if (m_blending){ glEnable(GL_BLEND); glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } else { glDisable(GL_BLEND); } if (m_geometry.indexCount()) glDrawElements(m_geometry.drawingMode(), m_geometry.indexCount(), m_geometry.indexType(), m_geometry.indexData()); else glDrawArrays(m_geometry.drawingMode(), 0, m_geometry.vertexCount()); glDepthMask(false); glDisable(GL_DEPTH_TEST); for (int i = 0; i < m_attributeNames.size(); ++i) m_program.disableAttributeArray(m_geometry.attributes()[i].position); }
void Label::setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false */, bool useA8Shader /* = false */) { if (atlas == _fontAtlas) { FontAtlasCache::releaseFontAtlas(atlas); return; } if (_fontAtlas) { FontAtlasCache::releaseFontAtlas(_fontAtlas); _fontAtlas = nullptr; } _fontAtlas = atlas; if (_textureAtlas) { _textureAtlas->setTexture(_fontAtlas->getTexture(0)); } else { SpriteBatchNode::initWithTexture(_fontAtlas->getTexture(0), 30); } if (_reusedLetter == nullptr) { _reusedLetter = Sprite::create(); _reusedLetter->setOpacityModifyRGB(_isOpacityModifyRGB); _reusedLetter->retain(); _reusedLetter->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); } _reusedLetter->setBatchNode(this); if (_fontAtlas) { _commonLineHeight = _fontAtlas->getCommonLineHeight(); _contentDirty = true; } #if CC_TARGET_PLATFORM != CC_PLATFORM_WP8 _useDistanceField = distanceFieldEnabled; #else // some older Windows Phones cannot run the ccShader_Label_df.frag program // so we must disable distance field _useDistanceField = false; #endif _useA8Shader = useA8Shader; if (_currentLabelType != LabelType::TTF) { _currLabelEffect = LabelEffect::NORMAL; updateShaderProgram(); } }
void Label::enableGlow(const Color4B& glowColor) { if(! _useDistanceField) return; _currLabelEffect = LabelEffect::GLOW; _effectColor = glowColor; _effectColorF.r = _effectColor.r / 255.0f; _effectColorF.g = _effectColor.g / 255.0f; _effectColorF.b = _effectColor.b / 255.0f; _effectColorF.a = _effectColor.a / 255.0f; updateShaderProgram(); }
void Label::setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false */, bool useA8Shader /* = false */) { if (atlas == _fontAtlas) { FontAtlasCache::releaseFontAtlas(atlas); return; } if (_fontAtlas) { FontAtlasCache::releaseFontAtlas(_fontAtlas); _fontAtlas = nullptr; } _fontAtlas = atlas; if (_textureAtlas) { _textureAtlas->setTexture(_fontAtlas->getTexture(0)); } else { SpriteBatchNode::initWithTexture(_fontAtlas->getTexture(0), 30); } if (_reusedLetter == nullptr) { _reusedLetter = Sprite::createWithTexture(_fontAtlas->getTexture(0)); _reusedLetter->setOpacityModifyRGB(_isOpacityModifyRGB); _reusedLetter->retain(); _reusedLetter->setAnchorPoint(Point::ANCHOR_TOP_LEFT); _reusedLetter->setBatchNode(this); } else { _reusedLetter->setTexture(_fontAtlas->getTexture(0)); } if (_fontAtlas) { _commonLineHeight = _fontAtlas->getCommonLineHeight(); _contentDirty = true; } _useDistanceField = distanceFieldEnabled; _useA8Shader = useA8Shader; if (_currentLabelType != LabelType::TTF) { _currLabelEffect = LabelEffect::NORMAL; updateShaderProgram(); } }
void Label::disableEffect(LabelEffect effect) { switch (effect) { case cocos2d::LabelEffect::NORMAL: break; case cocos2d::LabelEffect::OUTLINE: if (_currLabelEffect == LabelEffect::OUTLINE) { if (_currentLabelType == LabelType::TTF) { _fontConfig.outlineSize = 0; setTTFConfig(_fontConfig); } _currLabelEffect = LabelEffect::NORMAL; _contentDirty = true; } break; case cocos2d::LabelEffect::SHADOW: if (_shadowEnabled) { _shadowEnabled = false; if (_shadowNode) { Node::removeChild(_shadowNode, true); _shadowNode = nullptr; } } break; case cocos2d::LabelEffect::GLOW: if (_currLabelEffect == LabelEffect::GLOW) { _currLabelEffect = LabelEffect::NORMAL; updateShaderProgram(); } break; case LabelEffect::ALL: { disableEffect(LabelEffect::SHADOW); disableEffect(LabelEffect::GLOW); disableEffect(LabelEffect::OUTLINE); } break; default: break; } }
void Label::disableEffect() { if (_currLabelEffect == LabelEffect::OUTLINE) { _fontConfig.outlineSize = 0; setTTFConfig(_fontConfig); } _currLabelEffect = LabelEffect::NORMAL; updateShaderProgram(); _contentDirty = true; _shadowEnabled = false; if (_shadowNode) { Node::removeChild(_shadowNode,true); _shadowNode = nullptr; } }
void Label::enableGlow(const Color4B& glowColor) { if (_currentLabelType == LabelType::TTF) { if (_fontConfig.distanceFieldEnabled == false) { auto config = _fontConfig; config.outlineSize = 0; config.distanceFieldEnabled = true; setTTFConfig(config); _contentDirty = true; } _currLabelEffect = LabelEffect::GLOW; _effectColorF.r = glowColor.r / 255.0f; _effectColorF.g = glowColor.g / 255.0f; _effectColorF.b = glowColor.b / 255.0f; _effectColorF.a = glowColor.a / 255.0f; updateShaderProgram(); } }
void Label::setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false */, bool useA8Shader /* = false */) { if (atlas == _fontAtlas) { FontAtlasCache::releaseFontAtlas(atlas); return; } if (_fontAtlas) { _batchNodes.clear(); FontAtlasCache::releaseFontAtlas(_fontAtlas); _fontAtlas = nullptr; } _fontAtlas = atlas; if (_reusedLetter == nullptr) { _reusedLetter = Sprite::create(); _reusedLetter->setOpacityModifyRGB(_isOpacityModifyRGB); _reusedLetter->retain(); _reusedLetter->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT); } if (_fontAtlas) { _lineHeight = _fontAtlas->getLineHeight(); _contentDirty = true; } _useDistanceField = distanceFieldEnabled; _useA8Shader = useA8Shader; if (_currentLabelType != LabelType::TTF) { _currLabelEffect = LabelEffect::NORMAL; updateShaderProgram(); } }
void ShaderEffectItem::renderEffect(QPainter *painter, const QMatrix4x4 &matrix) { if (!painter || !painter->device()) return; if (!m_program || !m_program->programId()) { // Deleted due to deactivation, to save GPU memory, // or invalidated due to GL context change. delete m_program; if (QGLContext::currentContext()) m_program = new QGLShaderProgram(this); if (!m_program) qWarning() << "ShaderEffectItem::renderEffect - Creating QGLShaderProgram failed!"; } if (!m_program) return; if (!m_program->isLinked() || m_program_dirty) updateShaderProgram(); m_program->bind(); QMatrix4x4 combinedMatrix; combinedMatrix.scale(2.0 / painter->device()->width(), -2.0 / painter->device()->height(), 1.0); combinedMatrix.translate(-painter->device()->width() / 2.0, -painter->device()->height() / 2.0 ); combinedMatrix *= matrix; updateEffectState(combinedMatrix); for (int i = 0; i < m_attributeNames.size(); ++i) { m_program->enableAttributeArray(m_geometry.attributes()[i].position); } bindGeometry(); // Optimization, disable depth test when we know we don't need it. if (m_defaultVertexShader) { glDepthMask(false); glDisable(GL_DEPTH_TEST); } else { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GREATER); glDepthMask(true); #if defined(QT_OPENGL_ES) glClearDepthf(0); #else glClearDepth(0); #endif glClearColor(0, 0, 0, 0); glClear(GL_DEPTH_BUFFER_BIT); } if (m_blending) { glEnable(GL_BLEND); glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } else { glDisable(GL_BLEND); } if (m_geometry.indexCount()) glDrawElements(m_geometry.drawingMode(), m_geometry.indexCount(), m_geometry.indexType(), m_geometry.indexData()); else glDrawArrays(m_geometry.drawingMode(), 0, m_geometry.vertexCount()); glDepthMask(false); glDisable(GL_DEPTH_TEST); for (int i = 0; i < m_attributeNames.size(); ++i) m_program->disableAttributeArray(m_geometry.attributes()[i].position); }