[英]Low performance with tiles-based game with OpenGL and GLKit
我将 openGL 与 GLKit 一起使用来制作基于图块的游戏。 在游戏中,屏幕上放置了大约 1024 个图块,问题是每次调用 drawInRect 时我都在渲染图块,我的性能损失非常大。
这就是我渲染的方式(tilesArray 是 static,ballsArray 在屏幕上移动):
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
for (int i = 0; i < [self.tilesArray count]; i++){
RPSprite *tempTile = [self.tilesArray objectAtIndex:i];
[tempTile render];
}
for (RPSprite *ball in self.ballsArray){
[ball render];
}
}
正如我所说,tilesArray 在屏幕上是 static,但 ballsArray 在屏幕上移动得非常快(每次更新)。 如果我只放球,它会很快,如果我添加瓷砖,它会很慢(只在模拟器中试过)
这就是我渲染的方式:
- (GLKMatrix4) modelMatrix {
GLKMatrix4 modelMatrix = GLKMatrix4Identity;
modelMatrix = GLKMatrix4Translate(modelMatrix, self.position.x,320 - self.position.y - self.textureInfo.height, 0);
return modelMatrix;
}
- (void)render {
self.effect.texture2d0.name = self.textureInfo.name;
self.effect.texture2d0.enabled = YES;
self.effect.transform.modelviewMatrix = self.modelMatrix;
[self.effect prepareToDraw];
long offset = (long)&_quad;
glEnableVertexAttribArray(GLKVertexAttribPosition);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, geometryVertex)));
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), (void *) (offset + offsetof(TexturedVertex, textureVertex)));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
我修复了它,减少了图块使用的图像的大小,并在视图中放置了特定的帧率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.