图:
核心逻辑:
vector<b2Vec2>* allSegments = new vector<b2Vec2>();
float multiplyFactor = PTM_RATIO * CC_CONTENT_SCALE_FACTOR();
for(BYPolygon* polygon in comp.polyList) {
if(!polygon.isPirate) {
for(int k = 0; k < polygon.verticesCount; ++ k) {
polygon.vertices[k] += cen;
polygon.vertices[k] *= multiplyFactor;
}
glVertexPointer(2, GL_FLOAT, 0, polygon.vertices);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_FAN, 0, polygon.verticesCount);
for(int k = 0; k < polygon.verticesCount; ++ k) {
b2Vec2 v1 = polygon.vertices[k];
b2Vec2 v2 = (k + 1) < polygon.verticesCount ? polygon.vertices[k + 1] : polygon.vertices[0];
allSegments->push_back(b2Vec2(v1.x, v1.y));
allSegments->push_back(b2Vec2(v2.x, v2.y));
}
} else {
[pirateArray addObject:polygon];
}
}
vector<b2Vec2>* uselessSegments = new vector<b2Vec2>();
for(uint i = 0; i < allSegments->size()/2; ++ i) {
b2Vec2 m = allSegments->at(i * 2);
b2Vec2 n = allSegments->at(i * 2 + 1);
for(uint j = i + 1; j < allSegments->size()/2; ++ j) {
b2Vec2 p = allSegments->at(j * 2);
b2Vec2 q = allSegments->at(j * 2 + 1);
if(m.x==q.x && m.x==q.x && m.y==q.y && m.y==q.y) {
if(n.x==p.x && n.x== p.x && n.y==p.y && n.y==p.y) {
uselessSegments->push_back(b2Vec2(m.x, m.y));
uselessSegments->push_back(b2Vec2(n.x, n.y));
break;
}
}
}
}
if(uselessSegments->size() != 0) {
for(uint i = 0; i < uselessSegments->size()/2; ++ i) {
b2Vec2 m = uselessSegments->at(i * 2);
b2Vec2 n = uselessSegments->at(i * 2 + 1);
uint token = 0;
for(uint j = 0; j < allSegments->size()/2; ++ j) {
b2Vec2 p = allSegments->at(j * 2);
b2Vec2 q = allSegments->at(j * 2 + 1);
if(m.x==p.x && m.y==p.y && n.x==q.x && n.y==q.y) {
token = j;
vector<b2Vec2>::iterator begin = allSegments->begin();
allSegments->erase(begin + j * 2);
allSegments->erase(begin + j * 2);
break;
}
}
for(uint j = token; j < allSegments->size()/2; ++ j) {
b2Vec2 p = allSegments->at(j * 2);
b2Vec2 q = allSegments->at(j * 2 + 1);
if(m.x==q.x && m.y==q.y && n.x==p.x && n.y==p.y) {
vector<b2Vec2>::iterator begin = allSegments->begin();
allSegments->erase(begin + j * 2);
allSegments->erase(begin + j * 2);
break;
}
}
}
uselessSegments->clear();
}
delete uselessSegments;
glVertexPointer(2, GL_FLOAT, 0, &allSegments->at(0));
glColor4f(0, 0, 0, 1);
glDrawArrays(GL_LINES, 0, allSegments->size());
allSegments->clear();
delete allSegments;