0

Im using openGL with GLKit to make a tiles-based game. In the game, around 1024 tiles are placed in the screen, the problem is that I'm rendering the tiles every time drawInRect is called, and I'm having a lost of performance very big.

This is how I render (tilesArray is static, ballsArray is moving around the screen):

- (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];
    }
}

As I said, tilesArray is static in the screen, but ballsArray is moving around the screen very fast (with every update). If I put only the balls, it goes very fast, if I add the tiles, it goes very slow (only tried in simulator)

This is how I render:

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

}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Antonio MG
  • 20,382
  • 3
  • 43
  • 62
  • 3
    Why not render the tiles to a texture and then just draw the 1 quad. Update the tiles in the texture at your leisure. – Robinson Apr 10 '12 at 20:21
  • 1
    Take a look at [CCTMXTiledMap from Cocos2D](https://github.com/cocos2d/cocos2d-iphone/blob/gles20/cocos2d/CCTMXLayer.m) or the experimental [Cocos2D extension: HKTMXTiledMap](https://github.com/ricardoquesada/cocos2d-iphone-extensions/tree/gles20-hktmx/Extensions/HKTMXTiledMap) for ideas! – MechEthan Apr 10 '12 at 20:28
  • Thanks @Robinson, good idea, can you be more specific on how to do it? If you put it in an answer I'll accept. – Antonio MG Apr 11 '12 at 07:38
  • 1
    Well it all depends on what's available to you Antonio. I don't know anything about GLKit, so I won't put it as an answer. The basic form would be to create an FBO (Frame Buffer Object), attach a texture to it, render the scene with the FBO, and then use the attached texture as a texture to draw a single textured quad. Whenever you need to draw the screen again, you can use this textured quad. Whenever you need to update the tiles, you bind the FBO and render them again. Google for FBO. They can be a little fiddly to get setup, but they do work well. – Robinson Apr 11 '12 at 08:56

1 Answers1

0

I fixed it reducing the size of the images used by the tiles, and putting a specific framerate in the view.

Antonio MG
  • 20,382
  • 3
  • 43
  • 62