当我退出应用程序并重新输入时,出现错误:
libEGL call to OpenGL ES API with no current context (logged once per thread) (red warning text)
libc Fatal signal 11 (SIGSEGV) at 0x5f012000 (code=2) (black text)
错误文件是:(不知道在哪里,但如果我不使用这个文件就可以)
。H:
#ifndef __CCMaskLayer__LAYER_H__
#define __CCMaskLayer__LAYER_H__
#include "cocos2d.h"
using namespace cocos2d;
class CCMaskLayer : public cocos2d::CCLayer
{
public:
CCMaskLayer();
~CCMaskLayer();
virtual bool initWithRGBASizeCenterPosition(float red, float green, float blue, float alpha, CCSize s, CCPoint p);
virtual void onExit();
virtual void onEnterTransitionDidFinish();
LAYER_NODE_FUNC(CCMaskLayer);
static CCMaskLayer* layerWithCCColor4FSizeCenterPosition(ccColor4F c, CCSize s, CCPoint p);
static CCMaskLayer* layerWithRGBASizeCenterPosition(float red, float green, float blue, float alpha, CCSize s, CCPoint p);
void scratchOffCircle(CCPoint center, float radius);
void scratchOffRect(CCRect rect);
CC_SYNTHESIZE_RETAIN(CCRenderTexture* , masked, masked);
CC_SYNTHESIZE(float, r, r);
CC_SYNTHESIZE(float, g, g);
CC_SYNTHESIZE(float, b, b);
CC_SYNTHESIZE(float, a, a);
private:
void setClear(float red, float green, float blue, float alpha);
};
#endif // __CCMaskLayer__LAYER_H__
.cpp:
#include "CCMaskLayer.h"
using namespace cocos2d;
CCMaskLayer::CCMaskLayer()
{
}
CCMaskLayer::~CCMaskLayer()
{
masked->release();
}
bool CCMaskLayer::initWithRGBASizeCenterPosition(float red, float green, float blue, float alpha, CCSize s, CCPoint p) {
if ( !CCLayer::init() )
{
return false;
}
this->setClear(red, green, blue, alpha);
CCSize size = s;
masked = CCRenderTexture::renderTextureWithWidthAndHeight(size.width, size.height);
masked->getSprite()->setBlendFunc((ccBlendFunc){GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA});
masked->setPosition(p);
this->addChild(masked);
this->setIsTouchEnabled(true);
return true;
}
void CCMaskLayer::onEnterTransitionDidFinish()
{
CCLayer::onEnterTransitionDidFinish();
}
void CCMaskLayer::onExit()
{
CCLayer::onExit();
}
CCMaskLayer* CCMaskLayer::layerWithCCColor4FSizeCenterPosition(ccColor4F c, CCSize s, CCPoint p) {
return CCMaskLayer::layerWithRGBASizeCenterPosition(c.r, c.g, c.b, c.a, s, p);
}
CCMaskLayer* CCMaskLayer::layerWithRGBASizeCenterPosition(float r, float g, float b, float a, CCSize s, CCPoint p) {
CCMaskLayer* layer = new CCMaskLayer;
layer->initWithRGBASizeCenterPosition(r, g, b, a, s, p);
layer->autorelease();
return layer;
}
void CCMaskLayer::setClear(float red, float green, float blue, float alpha) {
r = red;
g = green;
b = blue;
a = alpha;
}
void CCMaskLayer::scratchOffCircle(CCPoint center, float radius) {
masked->beginWithClear(r, g, b, a);
glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glColorMask(0.0f, 0.0f, 0.0f, 1.0f);
glPointSize(radius * 2 * CC_CONTENT_SCALE_FACTOR());
glEnable(GL_POINT_SMOOTH);
glVertexPointer(2, GL_FLOAT, 0, ¢er);
glDrawArrays(GL_POINTS, 0, 1);
glColorMask(1.0f, 1.0f, 1.0f, 1.0f);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
masked->end();
}
void CCMaskLayer::scratchOffRect(CCRect rect) {
masked->beginWithClear(r, g, b, a);
glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glColorMask(0.0f, 0.0f, 0.0f, 1.0f);
CCPoint vertices[4] = {
rect.origin, // bottom, left
CCPointMake(rect.origin.x, rect.origin.y + rect.size.height), // top, left
CCPointMake(rect.origin.x + rect.size.width, rect.origin.y), // bottom, right
CCPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height), // top, right
};
glVertexPointer(2, GL_FLOAT, 0, &vertices);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glColorMask(1.0f, 1.0f, 1.0f, 1.0f);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
masked->end();
}
请注意,它适用于 iOS,但适用于 android 时崩溃。
编辑::
如果我注释掉圆形/矩形方法的最后一行,则不会绘制任何内容,但不会崩溃。所以问题出在这条线上: masked->end() ?