Skip to content

Commit 7d840ac

Browse files
ggggamerminggo
authored andcommitted
CameraBackgroundDepthBrush should handle opengl recreate event. (cocos2d#19037)
1 parent 16126f2 commit 7d840ac

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

cocos/2d/CCCameraBackgroundBrush.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,21 @@ CameraBackgroundDepthBrush::CameraBackgroundDepthBrush()
8888
, _vao(0)
8989
, _vertexBuffer(0)
9090
, _indexBuffer(0)
91+
#if CC_ENABLE_CACHE_TEXTURE_DATA
92+
, _backToForegroundListener(nullptr)
93+
#endif
9194
{
92-
95+
#if CC_ENABLE_CACHE_TEXTURE_DATA
96+
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*)
97+
{
98+
_vao = 0;
99+
_vertexBuffer = 0;
100+
_indexBuffer = 0;
101+
initBuffer();
102+
}
103+
);
104+
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
105+
#endif
93106
}
94107
CameraBackgroundDepthBrush::~CameraBackgroundDepthBrush()
95108
{
@@ -105,6 +118,9 @@ CameraBackgroundDepthBrush::~CameraBackgroundDepthBrush()
105118
glBindVertexArray(0);
106119
_vao = 0;
107120
}
121+
#if CC_ENABLE_CACHE_TEXTURE_DATA
122+
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
123+
#endif
108124
}
109125

110126
CameraBackgroundDepthBrush* CameraBackgroundDepthBrush::create(float depth)
@@ -142,6 +158,12 @@ bool CameraBackgroundDepthBrush::init()
142158
_quad.tl.texCoords = Tex2F(0,1);
143159
_quad.tr.texCoords = Tex2F(1,1);
144160

161+
initBuffer();
162+
return true;
163+
}
164+
165+
void CameraBackgroundDepthBrush::initBuffer()
166+
{
145167
auto supportVAO = Configuration::getInstance()->supportsShareableVAO();
146168
if (supportVAO)
147169
{
@@ -178,7 +200,6 @@ bool CameraBackgroundDepthBrush::init()
178200

179201
glBindBuffer(GL_ARRAY_BUFFER, 0);
180202
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
181-
return true;
182203
}
183204

184205
void CameraBackgroundDepthBrush::drawBackground(Camera* /*camera*/)

cocos/2d/CCCameraBackgroundBrush.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,13 @@ class CC_DLL CameraBackgroundDepthBrush : public CameraBackgroundBrush
152152
virtual ~CameraBackgroundDepthBrush();
153153

154154
virtual bool init() override;
155-
155+
156+
protected:
157+
#if CC_ENABLE_CACHE_TEXTURE_DATA
158+
EventListenerCustom* _backToForegroundListener;
159+
#endif
160+
void initBuffer();
161+
156162
protected:
157163
float _depth;
158164

@@ -194,7 +200,7 @@ class CC_DLL CameraBackgroundColorBrush : public CameraBackgroundDepthBrush
194200
* @param color Color used to clear the color buffer
195201
*/
196202
void setColor(const Color4F& color);
197-
203+
198204
CC_CONSTRUCTOR_ACCESS:
199205
CameraBackgroundColorBrush();
200206
virtual ~CameraBackgroundColorBrush();

0 commit comments

Comments
 (0)