Skip to content

Commit a8589a8

Browse files
PatriceJiangminggo
authored and
minggo
committed
Impl progress radial (cocos2d#162)
* progresstimer add radial impl * default drawType to element
1 parent e00d05b commit a8589a8

File tree

2 files changed

+85
-66
lines changed

2 files changed

+85
-66
lines changed

cocos/2d/CCProgressTimer.cpp

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,14 @@ bool ProgressTimer::initWithSprite(Sprite* sp)
8080
vertexLayout.setAtrribute("a_color", 2, backend::VertexFormat::UBYTE_R8G8B8A8, colorOffset, true);
8181
vertexLayout.setLayout(totalSize, backend::VertexStepMode::VERTEX);
8282

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);
8585

8686
return true;
8787
}
8888

8989
ProgressTimer::~ProgressTimer(void)
9090
{
91-
CC_SAFE_FREE(_vertexData);
9291
CC_SAFE_RELEASE(_sprite);
9392
}
9493

@@ -121,27 +120,32 @@ void ProgressTimer::setSprite(Sprite *sprite)
121120
setContentSize(_sprite->getContentSize());
122121

123122
// Every time we set a new sprite, we free the current vertex data
124-
if (_vertexData)
123+
if (!_vertexData.empty())
125124
{
126-
CC_SAFE_FREE(_vertexData);
127-
_vertexDataCount = 0;
125+
_vertexData.clear();
128126
updateProgress();
129127
}
130128
}
131129
}
132130

133131
void ProgressTimer::setType(Type type)
134132
{
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+
135145
if (type != _type)
136146
{
137147
// release all previous information
138-
if (_vertexData)
139-
{
140-
CC_SAFE_FREE(_vertexData);
141-
_vertexData = nullptr;
142-
_vertexDataCount = 0;
143-
}
144-
148+
_vertexData.clear();
145149
_type = type;
146150
}
147151
}
@@ -150,10 +154,8 @@ void ProgressTimer::setReverseDirection(bool reverse)
150154
{
151155
if( _reverseDirection != reverse ) {
152156
_reverseDirection = reverse;
153-
154157
// release all previous information
155-
CC_SAFE_FREE(_vertexData);
156-
_vertexDataCount = 0;
158+
_vertexData.clear();
157159
}
158160
}
159161

@@ -197,32 +199,31 @@ void ProgressTimer::updateColor()
197199
if (!_sprite)
198200
return;
199201

200-
if (_vertexData)
202+
if (!_vertexData.empty())
201203
{
202204
const Color4B& sc = _sprite->getQuad().tl.colors;
203-
for (int i = 0; i < _vertexDataCount; ++i)
205+
for (int i = 0; i < _vertexData.size(); ++i)
204206
{
205207
_vertexData[i].colors = sc;
206208
}
207-
_customCommand.updateVertexBuffer(_vertexData, sizeof(_vertexData[0]) * _vertexDataCount);
209+
_customCommand.updateVertexBuffer(_vertexData.data(), sizeof(_vertexData[0]) * _vertexData.size());
208210
}
209211
}
210212

211213
void ProgressTimer::updateProgress()
212214
{
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();
226227
}
227228

228229
void ProgressTimer::setAnchorPoint(const Vec2& anchorPoint)
@@ -356,21 +357,18 @@ void ProgressTimer::updateRadial(void)
356357
// the 3 is for the _midpoint, 12 o'clock point and hitpoint position.
357358

358359
bool sameIndexCount = true;
359-
if (_vertexDataCount != index + 3)
360+
if (_vertexData.size() != index + 3)
360361
{
361362
sameIndexCount = false;
362-
CC_SAFE_FREE(_vertexData);
363-
_vertexDataCount = 0;
363+
_vertexData.resize(index + 3);
364364
}
365365

366-
367-
if (!_vertexData)
366+
if (_indexData.size() != 3 + 3 * index)
368367
{
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);
372369
}
373-
updateColor();
370+
371+
//updateColor();
374372

375373
if (!sameIndexCount)
376374
{
@@ -388,11 +386,25 @@ void ProgressTimer::updateRadial(void)
388386
_vertexData[i+2].texCoords = textureCoordFromAlphaPoint(alphaPoint);
389387
_vertexData[i+2].vertices = vertexFromAlphaPoint(alphaPoint);
390388
}
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]));
391399
}
392400

393401
// 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();
396408
}
397409

398410
///
@@ -436,11 +448,9 @@ void ProgressTimer::updateBar(void)
436448

437449

438450
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+
444454
// TOPLEFT
445455
_vertexData[0].texCoords = textureCoordFromAlphaPoint(Vec2(min.x,max.y));
446456
_vertexData[0].vertices = vertexFromAlphaPoint(Vec2(min.x,max.y));
@@ -457,10 +467,8 @@ void ProgressTimer::updateBar(void)
457467
_vertexData[3].texCoords = textureCoordFromAlphaPoint(Vec2(max.x,min.y));
458468
_vertexData[3].vertices = vertexFromAlphaPoint(Vec2(max.x,min.y));
459469
} 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);
464472
// TOPLEFT 1
465473
_vertexData[0].texCoords = textureCoordFromAlphaPoint(Vec2(0,1));
466474
_vertexData[0].vertices = vertexFromAlphaPoint(Vec2(0,1));
@@ -495,7 +503,7 @@ void ProgressTimer::updateBar(void)
495503
_vertexData[5].vertices = vertexFromAlphaPoint(Vec2(max.x,min.y));
496504
}
497505

498-
_customCommand.createVertexBuffer(sizeof(_vertexData[0]), _vertexDataCount);
506+
_customCommand.createVertexBuffer(sizeof(_vertexData[0]), _vertexData.size());
499507
updateColor();
500508
}
501509

@@ -514,7 +522,7 @@ Vec2 ProgressTimer::boundaryTexCoord(char index)
514522

515523
void ProgressTimer::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
516524
{
517-
if( ! _vertexData || ! _sprite)
525+
if( _vertexData.empty() || ! _sprite)
518526
return;
519527

520528
//TODO: support rial
@@ -535,21 +543,30 @@ void ProgressTimer::draw(Renderer *renderer, const Mat4 &transform, uint32_t fla
535543
pipelineDescriptor.bindGroup.setUniform("u_MVPMatrix", finalMat.m, sizeof(finalMat.m));
536544
pipelineDescriptor.bindGroup.setTexture("u_texture", 0, _sprite->getTexture()->getBackendTexture());
537545

538-
if (!_reverseDirection)
546+
547+
if(_type == Type::BAR)
539548
{
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+
}
543565
}
544566
else
545567
{
546-
_customCommand.init(_globalZOrder, transform, flags);
547-
_customCommand.setVertexDrawInfo(0, _vertexDataCount / 2);
568+
_customCommand.init(_globalZOrder);
548569
renderer->addCommand(&_customCommand);
549-
550-
_customCommand2 = _customCommand;
551-
_customCommand2.setVertexDrawInfo(4, _vertexDataCount / 2);
552-
renderer->addCommand(&_customCommand2);
553570
}
554571
}
555572

cocos/2d/CCProgressTimer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ THE SOFTWARE.
3030
#include "2d/CCNode.h"
3131
#include "renderer/CCPipelineDescriptor.h"
3232

33+
#include <vector>
34+
3335
NS_CC_BEGIN
3436

3537
class Sprite;
@@ -182,8 +184,8 @@ class CC_DLL ProgressTimer : public Node
182184
Vec2 _barChangeRate;
183185
float _percentage = 0.0f;
184186
Sprite *_sprite = nullptr;
185-
int _vertexDataCount = 0;
186-
V2F_C4B_T2F *_vertexData = nullptr;
187+
std::vector<V2F_C4B_T2F> _vertexData;
188+
std::vector<uint16_t> _indexData;
187189
bool _reverseDirection = false;
188190

189191
CustomCommand _customCommand;

0 commit comments

Comments
 (0)