views:

737

answers:

5

Hello,

I'm writing a game that displays 56 hexagon pieces filling the screen in the shape of a board. I'm currently drawing each piece using a singleton rendering class that when called to draw a piece, creates a path from 6 points based of the coordinate passed in. This path is filled with a solid color and then a 59x59 png with an alpha to white gradient is overlayed over the drawing to give the piece a shiny look. Note i'm currently doing this in core graphics.

My first thought is that creating a path everytime i draw is costly and seems like i can somehow do this once and then reuse it, but i'm not sure of the best approach for this. When i look at the bottlenecks with shark, it looks like the drawing of the png is the most taxing part of the process. I've tried just rendering the png overlay or just rendering the path w/o the overlay and both give me some frame gains, altho removing the png overlay yields the most frames.

My current thought is that at startup, i should render 6 paths(1 for each color piece i have) and overlay them with the png and then store an image of these pieces and then just redraw the pieces each time i need them. Is there an effecient machanism for storing something you've drawn once and redrawing it? It kinda just sounds like i'd be running into the whole drawing pngs too often thing again, but maybe there's a less taxing method that does a similar thing...

Any suggestions are much appreciated. Thanks!

+1  A: 

General thoughts:

  • Game programming on iPhone usually necessitates OpenGL. Core Graphics is a bit easier to work with, but OpenGL is optimized for speed.
  • Prerender this "shiny look" into the textures as much as is possible (as in: do it in Photoshop before you even insert them into your project). Alpha blending is hell on performance.
  • Maybe try PVRTC (also this tutorial) as it's a format used by iPhone's GPU's manufacturer. Then again, this could make things worse depending on where your bottleneck is.
Kevin L.
+1  A: 

If you really need speed you have to go the OpenGL route. Be careful if you want to mix OpenGL and Core Animation, they can conflict.

OpenGL is a pain if you haven't done much with it. It sounds like you could use Core Animation and make each tile a layer. CA doesn't call the redraw again unless you change something, so you should be able to just move that layer around without taking a big hit. Also note that CA stores the layer in the texture memory so it should be much faster.

Zaph0d42
+1  A: 

Some others have mentioned that you should use OpenGL. Here's a nice introduction specifically for the iPhone: OpenGL ES from the Ground Up: Table of Contents

Thomas Müller
+1  A: 

You might try CGLayer or CALayer.

Peter Hosey
Thanks! CGLayer sounds like the best choice per the documentation: "Using CGLayer objects can improve performance, particularly when you need to capture a piece of drawing that you stamp repeatedly (using the same scale factor and orientation)."
Skyler
+1  A: 

You might also want to look at cocos2d. It seems to be significantly faster than using CoreAnimation in my tests, and provides lots of useful stuff for games.

pokeb
Yeah i plan on giving that a try on my next project. This is actually near the end of the current project and i started to realize that parts lagged unacceptably so i'd rather make some non-architecture changing fixes ;)
Skyler