Skip to content

Commit 4ea88f5

Browse files
committed
CCProgressNode will now properly update vertices
CCProgressNode only properly updated visually when the progress percentage or type was changed and ignored doing the same if the sprite (#698) or direction changed. flags were added with intention for batching the differences to avoid redundant calculations and will update if the node is visible and dirty before drawing rather than when set. and some other minor changes. on a side note, there are a few other possible improvements to be made, but those are for another day.
1 parent 28c3c71 commit 4ea88f5

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

cocos2d/CCProgressNode.m

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,19 @@ -(id) init
8282
-(id)initWithSprite:(CCSprite*) sprite
8383
{
8484
if(( self = [super init] )){
85+
_type = CCProgressNodeTypeRadial;
86+
_reverseDirection = NO;
8587
_percentage = 0.f;
8688
_vertexData = NULL;
8789
_vertexDataCount = 0;
90+
8891
self.anchorPoint = ccp(0.5f,0.5f);
89-
self.type = CCProgressNodeTypeRadial;
90-
self.reverseDirection = NO;
91-
self.midpoint = ccp(.5f, .5f);
92-
self.barChangeRate = ccp(1,1);
92+
self.midpoint = ccp(0.5f, 0.5f);
93+
self.barChangeRate = ccp(1, 1);
9394
self.sprite = sprite;
95+
96+
_dirtyVertexData = NO;
97+
_needsUpdateProgress = YES;
9498

9599
// shader program
96100
self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTextureColor];
@@ -105,11 +109,14 @@ -(void)dealloc
105109
}
106110
}
107111

108-
-(void)setPercentage:(float) percentage
112+
-(void)setPercentage:(float)percentage
109113
{
110114
if(_percentage != percentage) {
111-
_percentage = clampf( percentage, 0, 100);
112-
[self updateProgress];
115+
_percentage = clampf(percentage, 0, 100);
116+
117+
// only flag update progress here, let the progress type handle
118+
// whether it needs to rebuild the vertex data
119+
_needsUpdateProgress = YES;
113120
}
114121
}
115122

@@ -119,27 +126,18 @@ -(void)setSprite:(CCSprite *)newSprite
119126
_sprite = newSprite;
120127
self.contentSize = _sprite.contentSize;
121128

122-
// Everytime we set a new sprite, we free the current vertex data
123-
if(_vertexData){
124-
free(_vertexData);
125-
_vertexData = NULL;
126-
_vertexDataCount = 0;
127-
}
129+
_dirtyVertexData = YES;
130+
_needsUpdateProgress = YES;
128131
}
129132
}
130133

131134
-(void)setType:(CCProgressNodeType)newType
132135
{
133136
if (newType != _type) {
134-
135-
// release all previous information
136-
if(_vertexData){
137-
free(_vertexData);
138-
_vertexData = NULL;
139-
_vertexDataCount = 0;
140-
}
141137
_type = newType;
142-
[self updateProgress];
138+
139+
_dirtyVertexData = YES;
140+
_needsUpdateProgress = YES;
143141
}
144142
}
145143

@@ -148,12 +146,8 @@ -(void)setReverseDirection:(BOOL)reverse
148146
if( _reverseDirection != reverse ) {
149147
_reverseDirection = reverse;
150148

151-
// release all previous information
152-
if(_vertexData){
153-
free(_vertexData);
154-
_vertexData = NULL;
155-
_vertexDataCount = 0;
156-
}
149+
_dirtyVertexData = YES;
150+
_needsUpdateProgress = YES;
157151
}
158152
}
159153

@@ -225,15 +219,25 @@ -(void)updateColor
225219

226220
-(void)updateProgress
227221
{
222+
if (_dirtyVertexData){
223+
// remove the vertex data if the type, direction, or sprite have changed
224+
if (_vertexData) {
225+
free(_vertexData);
226+
_vertexData = NULL;
227+
_vertexDataCount = 0;
228+
}
229+
_dirtyVertexData = NO;
230+
}
231+
228232
switch (_type) {
229233
case CCProgressNodeTypeRadial:
230234
[self updateRadial];
231-
break;
235+
return;
232236
case CCProgressNodeTypeBar:
233237
[self updateBar];
234-
break;
238+
return;
235239
default:
236-
break;
240+
return;
237241
}
238242
}
239243

@@ -503,7 +507,12 @@ -(CGPoint)boundaryTexCoord:(char)index
503507

504508
-(void) draw
505509
{
506-
if( ! _vertexData || ! _sprite)
510+
if (_needsUpdateProgress) {
511+
[self updateProgress];
512+
_needsUpdateProgress = NO;
513+
}
514+
515+
if (!_vertexData || !_sprite)
507516
return;
508517

509518
CC_NODE_DRAW_SETUP();

0 commit comments

Comments
 (0)