From 7e4597508c709ff58d6de238fee7e99d33d91ea4 Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Thu, 15 May 2014 17:22:53 -0700 Subject: [PATCH] Does not crash on Mac if the .ttf file is not found --- cocos/2d/CCLabel.cpp | 6 +-- cocos/renderer/ccShader_Label_df.frag | 9 ++-- cocos/renderer/ccShader_Label_df_glow.frag | 9 ++-- cocos/renderer/ccShader_Label_normal.frag | 7 +-- cocos/renderer/ccShader_Label_outline.frag | 9 ++-- .../Classes/LabelTest/LabelTestNew.cpp | 52 ++++++++++++------- 6 files changed, 55 insertions(+), 37 deletions(-) diff --git a/cocos/2d/CCLabel.cpp b/cocos/2d/CCLabel.cpp index 0202070200..64439726b2 100644 --- a/cocos/2d/CCLabel.cpp +++ b/cocos/2d/CCLabel.cpp @@ -349,20 +349,20 @@ void Label::updateShaderProgram() break; case cocos2d::LabelEffect::OUTLINE: setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_LABEL_OUTLINE)); - _uniformEffectColor = glGetUniformLocation(getGLProgram()->getProgram(), "v_effectColor"); + _uniformEffectColor = glGetUniformLocation(getGLProgram()->getProgram(), "u_effectColor"); break; case cocos2d::LabelEffect::GLOW: if (_useDistanceField) { setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_LABEL_DISTANCEFIELD_GLOW)); - _uniformEffectColor = glGetUniformLocation(getGLProgram()->getProgram(), "v_effectColor"); + _uniformEffectColor = glGetUniformLocation(getGLProgram()->getProgram(), "u_effectColor"); } break; default: return; } - _uniformTextColor = glGetUniformLocation(getGLProgram()->getProgram(), "v_textColor"); + _uniformTextColor = glGetUniformLocation(getGLProgram()->getProgram(), "u_textColor"); } void Label::setFontAtlas(FontAtlas* atlas,bool distanceFieldEnabled /* = false */, bool useA8Shader /* = false */) diff --git a/cocos/renderer/ccShader_Label_df.frag b/cocos/renderer/ccShader_Label_df.frag index 56b1beed9e..cb3dac1122 100644 --- a/cocos/renderer/ccShader_Label_df.frag +++ b/cocos/renderer/ccShader_Label_df.frag @@ -5,8 +5,9 @@ precision lowp float; \n#endif\n varying vec4 v_fragmentColor; -varying vec2 v_texCoord; -uniform vec4 v_textColor; +varying vec2 v_texCoord; + +uniform vec4 u_textColor; void main() { @@ -19,7 +20,7 @@ void main() //float width = fwidth(dist); \n //assign width for constant will lead to a little bit fuzzy,it's temporary measure.\n float width = 0.04; - float alpha = smoothstep(0.5-width, 0.5+width, dist) * v_textColor.a; - gl_FragColor = v_fragmentColor * vec4(v_textColor.rgb,alpha); + float alpha = smoothstep(0.5-width, 0.5+width, dist) * u_textColor.a; + gl_FragColor = v_fragmentColor * vec4(u_textColor.rgb,alpha); } ); diff --git a/cocos/renderer/ccShader_Label_df_glow.frag b/cocos/renderer/ccShader_Label_df_glow.frag index 51eaed548d..b33b3e3d4d 100644 --- a/cocos/renderer/ccShader_Label_df_glow.frag +++ b/cocos/renderer/ccShader_Label_df_glow.frag @@ -5,9 +5,10 @@ precision lowp float; \n#endif\n varying vec4 v_fragmentColor; -varying vec2 v_texCoord; -uniform vec4 v_effectColor; -uniform vec4 v_textColor; +varying vec2 v_texCoord; + +uniform vec4 u_effectColor; +uniform vec4 u_textColor; void main() { @@ -19,7 +20,7 @@ void main() float alpha = smoothstep(0.5-width, 0.5+width, dist); //glow \n float mu = smoothstep(0.5, 1.0, sqrt(dist)); - vec4 color = v_effectColor*(1.0-alpha) + v_textColor*alpha; + vec4 color = u_effectColor*(1.0-alpha) + u_textColor*alpha; gl_FragColor = v_fragmentColor * vec4(color.rgb, max(alpha,mu)*color.a); } ); diff --git a/cocos/renderer/ccShader_Label_normal.frag b/cocos/renderer/ccShader_Label_normal.frag index 6034615401..1e0fc57d5e 100644 --- a/cocos/renderer/ccShader_Label_normal.frag +++ b/cocos/renderer/ccShader_Label_normal.frag @@ -30,12 +30,13 @@ precision lowp float; varying vec4 v_fragmentColor; varying vec2 v_texCoord; -uniform vec4 v_textColor; + +uniform vec4 u_textColor; void main() { - gl_FragColor = v_fragmentColor * vec4( v_textColor.rgb,// RGB from uniform\n - v_textColor.a * texture2D(CC_Texture0, v_texCoord).a// A from texture & uniform\n + gl_FragColor = v_fragmentColor * vec4(u_textColor.rgb,// RGB from uniform\n + u_textColor.a * texture2D(CC_Texture0, v_texCoord).a// A from texture & uniform\n ); } ); diff --git a/cocos/renderer/ccShader_Label_outline.frag b/cocos/renderer/ccShader_Label_outline.frag index 958d716802..4395e90910 100644 --- a/cocos/renderer/ccShader_Label_outline.frag +++ b/cocos/renderer/ccShader_Label_outline.frag @@ -7,9 +7,10 @@ precision lowp float; \n#endif\n varying vec4 v_fragmentColor; -varying vec2 v_texCoord; -uniform vec4 v_effectColor; -uniform vec4 v_textColor; +varying vec2 v_texCoord; + +uniform vec4 u_effectColor; +uniform vec4 u_textColor; void main() { @@ -17,7 +18,7 @@ void main() float fontAlpha = sample.a; float outlineAlpha = sample.r; if (outlineAlpha > 0.0){ - vec4 color = v_textColor * fontAlpha + v_effectColor * (1.0 - fontAlpha); + vec4 color = u_textColor * fontAlpha + u_effectColor * (1.0 - fontAlpha); gl_FragColor = v_fragmentColor * vec4( color.rgb,max(fontAlpha,outlineAlpha)*color.a); } else { diff --git a/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp b/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp index 1d8ee072f9..ea8aac728c 100644 --- a/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp +++ b/tests/cpp-tests/Classes/LabelTest/LabelTestNew.cpp @@ -607,8 +607,11 @@ LabelTTFUnicodeChinese::LabelTTFUnicodeChinese() // like "Error 3 error C2146: syntax error : missing ')' before identifier 'label'"; TTFConfig ttfConfig("fonts/wt021.ttf",28,GlyphCollection::CUSTOM, "美好的一天啊"); auto label = Label::createWithTTF(ttfConfig,"美好的一天啊", TextHAlignment::CENTER, size.width); - label->setPosition(Vec2(size.width / 2, size.height /2)); - this->addChild(label); + + if(label) { + label->setPosition(Vec2(size.width / 2, size.height /2)); + this->addChild(label); + } } std::string LabelTTFUnicodeChinese::title() const @@ -966,8 +969,10 @@ LabelTTFLargeText::LabelTTFLargeText() TTFConfig ttfConfig("fonts/wt021.ttf",18,GlyphCollection::DYNAMIC); std::string text = FileUtils::getInstance()->getStringFromFile("commonly_used_words.txt"); auto label = Label::createWithTTF(ttfConfig,text, TextHAlignment::CENTER, size.width); - label->setPosition( Vec2(size.width/2, size.height/2) ); - addChild(label); + if(label) { + label->setPosition( Vec2(size.width/2, size.height/2) ); + addChild(label); + } } std::string LabelTTFLargeText::title() const @@ -1090,23 +1095,30 @@ LabelTTFCJKWrappingTest::LabelTTFCJKWrappingTest() TTFConfig ttfConfig("fonts/wt021.ttf", 25, GlyphCollection::DYNAMIC); auto label1 = Label::createWithTTF(ttfConfig, "你好,Cocos2d-x v3的New Label.", TextHAlignment::LEFT, size.width * 0.75); - label1->setTextColor(Color4B(128, 255, 255, 255)); - label1->setPosition(Vec2(size.width * 0.1, size.height * 0.6)); - label1->setAnchorPoint(Vec2(0, 0.5)); - this->addChild(label1); - + if(label1) { + label1->setTextColor(Color4B(128, 255, 255, 255)); + label1->setPosition(Vec2(size.width * 0.1, size.height * 0.6)); + label1->setAnchorPoint(Vec2(0, 0.5)); + this->addChild(label1); + } + auto label2 = Label::createWithTTF(ttfConfig, "早上好,Cocos2d-x v3的New Label.", TextHAlignment::LEFT, size.width * 0.75); - label2->setTextColor(Color4B(255, 128, 255, 255)); - label2->setPosition(Vec2(size.width * 0.1, size.height * 0.4)); - label2->setAnchorPoint(Vec2(0, 0.5)); - this->addChild(label2); + if(label2) { + label2->setTextColor(Color4B(255, 128, 255, 255)); + label2->setPosition(Vec2(size.width * 0.1, size.height * 0.4)); + label2->setAnchorPoint(Vec2(0, 0.5)); + this->addChild(label2); + } + auto label3 = Label::createWithTTF(ttfConfig, "美好的一天啊美好的一天啊美好的一天啊", TextHAlignment::LEFT, size.width * 0.75); - label3->setTextColor(Color4B(255, 255, 128, 255)); - label3->setPosition(Vec2(size.width * 0.1, size.height * 0.2)); - label3->setAnchorPoint(Vec2(0, 0.5)); - this->addChild(label3); + if(label3) { + label3->setTextColor(Color4B(255, 255, 128, 255)); + label3->setPosition(Vec2(size.width * 0.1, size.height * 0.2)); + label3->setAnchorPoint(Vec2(0, 0.5)); + this->addChild(label3); + } } std::string LabelTTFCJKWrappingTest::title() const @@ -1153,8 +1165,10 @@ LabelTTFUnicodeNew::LabelTTFUnicodeNew() ttfConfig.glyphs = GlyphCollection::CUSTOM; ttfConfig.customGlyphs = chinese.c_str(); auto label3 = Label::createWithTTF(ttfConfig,chinese, TextHAlignment::CENTER,size.width); - label3->setPosition( Vec2(size.width/2, vSize - (vStep * 6.5)) ); - addChild(label3); + if(label3) { + label3->setPosition( Vec2(size.width/2, vSize - (vStep * 6.5)) ); + addChild(label3); + } } std::string LabelTTFUnicodeNew::title() const