views:

535

answers:

1

Hi,

I'm using UIImage to render a game map from 32x32 blocks. The code is as follows

for( int x = 0; x < rwidth; ++x)
{
 for(int y = 0; y < rheight; ++y)
 {
  int bindex = [current_map GetTileIndex:x :y];
  CGPoint p;
  p.x = x * tile_size_x;
  p.y = y * tile_size_x;
  [img_list[bindex] drawAtPoint:p];
 }
}

This ends up rendering about 150 tiles. My problem is when I run this code my render time goes down to 2-3 frames a second.

I thought the iPhone was fill bound, however if I force bindex to = 1 (ie render the same block 150 times) then I get full framerate.

I can't believe it is that expensive to render from different UIImages.

Could someone please tell me what I'm doing wrong ...

Oh I forgot to mention my image list created from a bigger texture page using CGImageCreateWithImageInRect.

Thanks Rich

+1  A: 

Crack open Instruments and profile your code. It looks like you are drawing into a UIView, and they are not really optimized for games.. Since UIViews are layer-backed, drawAtPoint: needs to draw the image several times (to offscreen buffers) before it appears on screen.

That being said, UIImage isn't really designed for games. There's a lot of overhead with regards to being part of UIKit. You're not really meant to have hundreds of them.

In approx order of speed (slowest to fastest):

  • UIImage (usually used if you have a few large images)
  • CALayers (if you need fine animation control or you have roughly 20 to 100 tiles/images)
  • OpelGL textures (what most games should be using)
Kailoa Kadano
Thanks, I was going to go straight to opengl, however the docs kept persuading me that CGDrawImage was highly optmizied. As I only needed a tiled background I figured it was good enough ... guess not.
Rich