@@ -80,15 +80,14 @@ bool ProgressTimer::initWithSprite(Sprite* sp)
80
80
vertexLayout.setAtrribute (" a_color" , 2 , backend::VertexFormat::UBYTE_R8G8B8A8, colorOffset, true );
81
81
vertexLayout.setLayout (totalSize, backend::VertexStepMode::VERTEX);
82
82
83
- _customCommand.setDrawType (CustomCommand::DrawType::ARRAY );
84
- _customCommand.setPrimitiveType (CustomCommand::PrimitiveType::TRIANGLE_STRIP );
83
+ _customCommand.setDrawType (CustomCommand::DrawType::ELEMENT );
84
+ _customCommand.setPrimitiveType (CustomCommand::PrimitiveType::TRIANGLE );
85
85
86
86
return true ;
87
87
}
88
88
89
89
ProgressTimer::~ProgressTimer (void )
90
90
{
91
- CC_SAFE_FREE (_vertexData);
92
91
CC_SAFE_RELEASE (_sprite);
93
92
}
94
93
@@ -121,27 +120,32 @@ void ProgressTimer::setSprite(Sprite *sprite)
121
120
setContentSize (_sprite->getContentSize ());
122
121
123
122
// Every time we set a new sprite, we free the current vertex data
124
- if (_vertexData)
123
+ if (! _vertexData. empty () )
125
124
{
126
- CC_SAFE_FREE (_vertexData);
127
- _vertexDataCount = 0 ;
125
+ _vertexData.clear ();
128
126
updateProgress ();
129
127
}
130
128
}
131
129
}
132
130
133
131
void ProgressTimer::setType (Type type)
134
132
{
133
+
134
+ if (type == Type::RADIAL)
135
+ {
136
+ _customCommand.setDrawType (CustomCommand::DrawType::ELEMENT);
137
+ _customCommand.setPrimitiveType (CustomCommand::PrimitiveType::TRIANGLE);
138
+ }
139
+ else
140
+ {
141
+ _customCommand.setPrimitiveType (CustomCommand::PrimitiveType::TRIANGLE_STRIP);
142
+ _customCommand.setDrawType (CustomCommand::DrawType::ARRAY);
143
+ }
144
+
135
145
if (type != _type)
136
146
{
137
147
// release all previous information
138
- if (_vertexData)
139
- {
140
- CC_SAFE_FREE (_vertexData);
141
- _vertexData = nullptr ;
142
- _vertexDataCount = 0 ;
143
- }
144
-
148
+ _vertexData.clear ();
145
149
_type = type;
146
150
}
147
151
}
@@ -150,10 +154,8 @@ void ProgressTimer::setReverseDirection(bool reverse)
150
154
{
151
155
if ( _reverseDirection != reverse ) {
152
156
_reverseDirection = reverse;
153
-
154
157
// release all previous information
155
- CC_SAFE_FREE (_vertexData);
156
- _vertexDataCount = 0 ;
158
+ _vertexData.clear ();
157
159
}
158
160
}
159
161
@@ -197,32 +199,31 @@ void ProgressTimer::updateColor()
197
199
if (!_sprite)
198
200
return ;
199
201
200
- if (_vertexData)
202
+ if (! _vertexData. empty () )
201
203
{
202
204
const Color4B& sc = _sprite->getQuad ().tl .colors ;
203
- for (int i = 0 ; i < _vertexDataCount ; ++i)
205
+ for (int i = 0 ; i < _vertexData. size () ; ++i)
204
206
{
205
207
_vertexData[i].colors = sc;
206
208
}
207
- _customCommand.updateVertexBuffer (_vertexData, sizeof (_vertexData[0 ]) * _vertexDataCount );
209
+ _customCommand.updateVertexBuffer (_vertexData. data () , sizeof (_vertexData[0 ]) * _vertexData. size () );
208
210
}
209
211
}
210
212
211
213
void ProgressTimer::updateProgress ()
212
214
{
213
- // switch (_type)
214
- // {
215
- // case Type::RADIAL:
216
- // updateRadial();
217
- // break;
218
- // case Type::BAR:
219
- // updateBar();
220
- // break;
221
- // default:
222
- // break;
223
- // }
224
-
225
- updateBar ();
215
+ switch (_type)
216
+ {
217
+ case Type::RADIAL:
218
+ updateRadial ();
219
+ break ;
220
+ case Type::BAR:
221
+ updateBar ();
222
+ break ;
223
+ default :
224
+ break ;
225
+ }
226
+ // updateBar();
226
227
}
227
228
228
229
void ProgressTimer::setAnchorPoint (const Vec2& anchorPoint)
@@ -356,21 +357,18 @@ void ProgressTimer::updateRadial(void)
356
357
// the 3 is for the _midpoint, 12 o'clock point and hitpoint position.
357
358
358
359
bool sameIndexCount = true ;
359
- if (_vertexDataCount != index + 3 )
360
+ if (_vertexData. size () != index + 3 )
360
361
{
361
362
sameIndexCount = false ;
362
- CC_SAFE_FREE (_vertexData);
363
- _vertexDataCount = 0 ;
363
+ _vertexData.resize (index + 3 );
364
364
}
365
365
366
-
367
- if (!_vertexData)
366
+ if (_indexData.size () != 3 + 3 * index )
368
367
{
369
- _vertexDataCount = index + 3 ;
370
- _vertexData = (V2F_C4B_T2F*)malloc (_vertexDataCount * sizeof (V2F_C4B_T2F));
371
- CCASSERT ( _vertexData, " CCProgressTimer. Not enough memory" );
368
+ _indexData.resize (3 + 3 * index );
372
369
}
373
- updateColor ();
370
+
371
+ // updateColor();
374
372
375
373
if (!sameIndexCount)
376
374
{
@@ -388,11 +386,25 @@ void ProgressTimer::updateRadial(void)
388
386
_vertexData[i+2 ].texCoords = textureCoordFromAlphaPoint (alphaPoint);
389
387
_vertexData[i+2 ].vertices = vertexFromAlphaPoint (alphaPoint);
390
388
}
389
+
390
+ for (int i = 0 ; i < index + 1 ; i++)
391
+ {
392
+ _indexData[i * 3 ] = 0 ;
393
+ _indexData[i * 3 + 1 ] = i + 2 ;
394
+ _indexData[i * 3 + 2 ] = i + 1 ;
395
+ }
396
+
397
+ _customCommand.createIndexBuffer (sizeof (_indexData[0 ]), _indexData.size ());
398
+ _customCommand.updateIndexBuffer (_indexData.data (), _indexData.size () * sizeof (_indexData[0 ]));
391
399
}
392
400
393
401
// hitpoint will go last
394
- _vertexData[_vertexDataCount - 1 ].texCoords = textureCoordFromAlphaPoint (hit);
395
- _vertexData[_vertexDataCount - 1 ].vertices = vertexFromAlphaPoint (hit);
402
+ _vertexData[_vertexData.size () - 1 ].texCoords = textureCoordFromAlphaPoint (hit);
403
+ _vertexData[_vertexData.size () - 1 ].vertices = vertexFromAlphaPoint (hit);
404
+
405
+ _customCommand.createVertexBuffer (sizeof (_vertexData[0 ]), _vertexData.size ());
406
+
407
+ updateColor ();
396
408
}
397
409
398
410
// /
@@ -436,11 +448,9 @@ void ProgressTimer::updateBar(void)
436
448
437
449
438
450
if (!_reverseDirection) {
439
- if (! _vertexData) {
440
- _vertexDataCount = 4 ;
441
- _vertexData = (V2F_C4B_T2F*)malloc (_vertexDataCount * sizeof (V2F_C4B_T2F));
442
- CCASSERT ( _vertexData, " CCProgressTimer. Not enough memory" );
443
- }
451
+
452
+ _vertexData.resize (4 );
453
+
444
454
// TOPLEFT
445
455
_vertexData[0 ].texCoords = textureCoordFromAlphaPoint (Vec2 (min.x ,max.y ));
446
456
_vertexData[0 ].vertices = vertexFromAlphaPoint (Vec2 (min.x ,max.y ));
@@ -457,10 +467,8 @@ void ProgressTimer::updateBar(void)
457
467
_vertexData[3 ].texCoords = textureCoordFromAlphaPoint (Vec2 (max.x ,min.y ));
458
468
_vertexData[3 ].vertices = vertexFromAlphaPoint (Vec2 (max.x ,min.y ));
459
469
} else {
460
- if (!_vertexData) {
461
- _vertexDataCount = 8 ;
462
- _vertexData = (V2F_C4B_T2F*)malloc (_vertexDataCount * sizeof (V2F_C4B_T2F));
463
- CCASSERT ( _vertexData, " CCProgressTimer. Not enough memory" );
470
+ if (_vertexData.empty ()) {
471
+ _vertexData.resize (8 );
464
472
// TOPLEFT 1
465
473
_vertexData[0 ].texCoords = textureCoordFromAlphaPoint (Vec2 (0 ,1 ));
466
474
_vertexData[0 ].vertices = vertexFromAlphaPoint (Vec2 (0 ,1 ));
@@ -495,7 +503,7 @@ void ProgressTimer::updateBar(void)
495
503
_vertexData[5 ].vertices = vertexFromAlphaPoint (Vec2 (max.x ,min.y ));
496
504
}
497
505
498
- _customCommand.createVertexBuffer (sizeof (_vertexData[0 ]), _vertexDataCount );
506
+ _customCommand.createVertexBuffer (sizeof (_vertexData[0 ]), _vertexData. size () );
499
507
updateColor ();
500
508
}
501
509
@@ -514,7 +522,7 @@ Vec2 ProgressTimer::boundaryTexCoord(char index)
514
522
515
523
void ProgressTimer::draw (Renderer *renderer, const Mat4 &transform, uint32_t flags)
516
524
{
517
- if ( ! _vertexData || ! _sprite)
525
+ if ( _vertexData. empty () || ! _sprite)
518
526
return ;
519
527
520
528
// TODO: support rial
@@ -535,21 +543,30 @@ void ProgressTimer::draw(Renderer *renderer, const Mat4 &transform, uint32_t fla
535
543
pipelineDescriptor.bindGroup .setUniform (" u_MVPMatrix" , finalMat.m , sizeof (finalMat.m ));
536
544
pipelineDescriptor.bindGroup .setTexture (" u_texture" , 0 , _sprite->getTexture ()->getBackendTexture ());
537
545
538
- if (!_reverseDirection)
546
+
547
+ if (_type == Type::BAR)
539
548
{
540
- _customCommand.init (_globalZOrder, transform, flags);
541
- _customCommand.setVertexDrawInfo (0 , _vertexDataCount);
542
- renderer->addCommand (&_customCommand);
549
+ if (!_reverseDirection)
550
+ {
551
+ _customCommand.init (_globalZOrder, transform, flags);
552
+ _customCommand.setVertexDrawInfo (0 , _vertexData.size ());
553
+ renderer->addCommand (&_customCommand);
554
+ }
555
+ else
556
+ {
557
+ _customCommand.init (_globalZOrder, transform, flags);
558
+ _customCommand.setVertexDrawInfo (0 , _vertexData.size () / 2 );
559
+ renderer->addCommand (&_customCommand);
560
+
561
+ _customCommand2 = _customCommand;
562
+ _customCommand2.setVertexDrawInfo (4 , _vertexData.size () / 2 );
563
+ renderer->addCommand (&_customCommand2);
564
+ }
543
565
}
544
566
else
545
567
{
546
- _customCommand.init (_globalZOrder, transform, flags);
547
- _customCommand.setVertexDrawInfo (0 , _vertexDataCount / 2 );
568
+ _customCommand.init (_globalZOrder);
548
569
renderer->addCommand (&_customCommand);
549
-
550
- _customCommand2 = _customCommand;
551
- _customCommand2.setVertexDrawInfo (4 , _vertexDataCount / 2 );
552
- renderer->addCommand (&_customCommand2);
553
570
}
554
571
}
555
572
0 commit comments