这种闪电确实影响了我的游戏性能,因为我不断添加和移除闪电,而且每次闪电都由 3 条抗锯齿线组成,使用:
void ccDrawSmoothLine(CGPoint pos1, CGPoint pos2, float width)
{
GLfloat lineVertices[12], curc[4];
GLint ir, ig, ib, ia;
CGPoint dir, tan;
// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
// Needed states: GL_VERTEX_ARRAY,
// Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
//glEnable(GL_LINE_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
pos1.x *= CC_CONTENT_SCALE_FACTOR();
pos1.y *= CC_CONTENT_SCALE_FACTOR();
pos2.x *= CC_CONTENT_SCALE_FACTOR();
pos2.y *= CC_CONTENT_SCALE_FACTOR();
width *= CC_CONTENT_SCALE_FACTOR();
width = width*2;
dir.x = pos2.x - pos1.x;
dir.y = pos2.y - pos1.y;
float len = sqrtf(dir.x*dir.x+dir.y*dir.y);
if(len<0.00001)
return;
dir.x = dir.x/len;
dir.y = dir.y/len;
tan.x = -width*dir.y;
tan.y = width*dir.x;
lineVertices[0] = pos1.x + tan.x;
lineVertices[1] = pos1.y + tan.y;
lineVertices[2] = pos2.x + tan.x;
lineVertices[3] = pos2.y + tan.y;
lineVertices[4] = pos1.x;
lineVertices[5] = pos1.y;
lineVertices[6] = pos2.x;
lineVertices[7] = pos2.y;
lineVertices[8] = pos1.x - tan.x;
lineVertices[9] = pos1.y - tan.y;
lineVertices[10] = pos2.x - tan.x;
lineVertices[11] = pos2.y - tan.y;
glGetFloatv(GL_CURRENT_COLOR,curc);
ir = 255.0*curc[0];
ig = 255.0*curc[1];
ib = 255.0*curc[2];
ia = 255.0*curc[3];
const GLubyte lineColors[] = {
ir, ig, ib, 0,
ir, ig, ib, 0,
ir, ig, ib, ia,
ir, ig, ib, ia,
ir, ig, ib, 0,
ir, ig, ib, 0,
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, lineVertices);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, lineColors);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
glDisableClientState(GL_COLOR_ARRAY);
// restore default state
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
}
我的 FPS 将下降到 40 左右,然后又回到 60。我读过纹理映射线可以提高我的游戏性能。
我已经尝试解决这个问题好几个星期了,但没有运气。有人可以帮我吗?
这是我目前的 ccDrawLines 和 draw 方法
-(void) draw
{
numPoints_ = 0;
glColor4ub(_color.r, _color.g, _color.b, _opacity);
if (_opacity != 255)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
drawLightning(_strikePoint2, _strikePoint, _displacement, _minDisplacement, _seed, lightningPoints_, &numPoints_);
ccDrawLines(lightningPoints_, numPoints_, texture);
if (_opacity != 255)
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
glColor4f(1.0, 1.0, 1.0, 1.0);
}
void ccDrawLines( CGPoint* points, uint numberOfPoints, CCTexture2D* texture )
{
//layout of points [0] = origin, [1] = destination and so on
ccVertex2F vertices[numberOfPoints];
if (CC_CONTENT_SCALE_FACTOR() != 1 )
{
for (int i = 0; i < numberOfPoints; i++)
{
vertices[i].x = points[i].x * CC_CONTENT_SCALE_FACTOR();
vertices[i].y= points[i].y * CC_CONTENT_SCALE_FACTOR();
}
glVertexPointer(2, GL_FLOAT, 0, vertices);
}
else glVertexPointer(2, GL_FLOAT, 0, points);
ccTex2F texCoords[numberOfPoints];
float width = texture.pixelsWide;
float height = texture.pixelsHigh;
if (CC_CONTENT_SCALE_FACTOR() != 1 )
{
for (int i = 0; i < numberOfPoints; i++)
{
texCoords[i].u = (vertices[i].x * CC_CONTENT_SCALE_FACTOR()) / width;
texCoords[i].v = (vertices[i].y * CC_CONTENT_SCALE_FACTOR()) / height;
}
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
}
else glTexCoordPointer(2, GL_FLOAT, 0, points);
// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
// Needed states: GL_VERTEX_ARRAY,
// Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, [texture name]);
glDisableClientState(GL_COLOR_ARRAY);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, numberOfPoints);
// restore default state
glEnableClientState(GL_COLOR_ARRAY);
glPopMatrix();
}
纹理只是一个带有小蓝点的 32x32 .png 文件。
如果您查看 ccDrawLines,我已将代码添加到纹理映射线。它的问题是,线条中有间隙,绘制了多条线,看起来很可怕。
编辑: 我决定不对线条进行纹理贴图并使用 ccDrawSmoothLine。
我所做的只是在我的游戏层初始化中分配闪电
lightningStrike_ = [Lightning lightningWithStrikePoint:ccp(-100, -100) strikePoint2:ccp(-100, -100)];
[self addChild:lightningStrike_ z:1];
然后,我创建了一个实例方法来设置_strikePoint 和_strikePoint2 属性并调用strikeRandom 方法。
-(Lightning *)lightningStrike:(CGPoint)p end:(CGPoint)p2
{
lightningStrike_.strikePoint = ccp(p.x, p.y);
lightningStrike_.strikePoint2 = ccp(p2.x, p2.y);
[lightningStrike_ strikeRandom];
return lightningStrike_;
}
用法:
[self lightningStrike:ccp(100, 100) end:ccp(100, 100)];
这修复了 FPS 下降。24 小时后我会回答并接受我自己的回答。