I see many cool games being developed around here for the iPhone. Many of them are designed with a PC in mind which can lead to problems on the iPhone as it is still only a mobile device with very limited capabilities. The iPhone has very limited RAM and will quickly begin to send out low memory warnings to all running applications.
So I thought I wanted to write out a little guide for you to make your games:
- load faster
- run faster
- consume less battery power
- take less disk space
- use less memory on the device
Best optimization tip:
Use the 'Color reduction' option globally
Alpha-channels look awesome and gives a visually pleasing effect but they will slow down your game if you overuse them, just as they do in the PC runtime (just more).
An image with an alpha-channel takes 33% more memory and space than an image without + it takes more power and time to render them.
Does your graphic really need the alpha-channel? Does the background underneath it ever change? If not you could probably "bake" the alpha-channel effect into the image itself so that the shadow is just part of the picture.
Many are often tempted to import really large pictures into the game where it will only show very little of that picture at one time. A good advice is to break it up into smaller parts so that fewer of them are shown at one time.
Remember that any image you have in your game has to fit inside their own texture. In a sample game I saw there was a 960x480 sized background image on a frame. This picture alone had to fit into a 1024x512 texture taking up a huge amount of memory. What was even worse was that it had an alpha-channel even though no pixel in it was transparent. Second, the same frame had two different huge animations sized 957x158 (each of them contained 17 frames). This already takes up 2*17 * 1024*256 * 3 = 26738688 bytes = 25.5 MB of RAM which is way above what the iPhone likes. That was even without the rest of the graphics and the huge background image I talked about before.
Most graphics doesn't need 100% crispness and quality. It will only take 1/4th of the previous memory usage if you cut your image size in half and then at runtime scale it to 2.0. It will also run much faster as it utilizes the graphics card RAM cache much better.
Tip: (From Yves)
Crop your images, remove any useless empty space around them. Some developers tend to use a single frame size for an entire animation with sometimes huge transparent parts in some images. It's probably better at design time, but at runtime it results in a loss of time and space.
Try to make your graphics of a size close to something of a power-of-two. It will waste less precious memory.
A power of two size is one of the following: 8,16,32,64,128,256,512,1024
Images less than 8x8 pixels will be stored in a 8x8 texture as they cannot be smaller than that.
You can easily use different sizes for your width and height, they don't have to be equal.
Be sure to set the "Image compression" property to "Color reduction" either globally for all objects in the App iPhone properties or per object - as often as you can.
In many cases it will make it consume half the memory it did before.
Take for example the big background image before:
Before it wasted 2 MB of texture memory.
With color compression and no alpha channel it will only take 1 MB. And if it was cropped to only fill out the resolution of the iPhone device it would only take 30 KB.
The animations mentioned before that took 25 MB of RAM can with color compression and half the resolution only take: 2*17 * 512*128 * 2 = 4456448 bytes = 4.25 MB!
That is a huge difference.
Single color graphic
This is kinda an extension to the previous pitfall:
I have also often in the sample games I've debugged seen some big actives (typical white) that are meant to cover the entire screen and fade to transparent. People need to realize that even thought they are pure white (or whatever color) they are still stored in memory uncompressed at runtime and waste just as much memory as if it was a pretty picture. If you need those fade-effects you can again scale a small active to fit the screen at runtime and gradually alter it's semi-transparency.
Transitions on big graphics
You should in general avoid using transitions on objects larger than the iPhone screen resolution even though it works well enough. Using a transition on such an object will consume a lot of extra memory during the transition and can be slow. You have to try it at runtime to see if it gives you a noticeable performance drop. Just remember that even though it doesn't hurt the FPS that much, it can still drain the battery much more than you know.
Overuse of "text" counters/score and strings
Though it may not be a big of a performance issue, it can hurt your game's performance a little bit if you use too many objects that draws text. If the text doesn't change it should not be a performance problem, but if you update your string/score/counters often (as counters/scores often do) you will get a performance problem.
Only use text whenever you know it will not change often (like every frame).
Use bitmap counters/scores instead of text counters/scores. There you can also much better control the visual style of your objects and there is nearly no performance penalty.
Too many transitions at one time
Each object that needs to have a transition effect on it will consume more memory than usual and will use some special buffers on the device that aren't unlimited. Once you reach a certain limit you will begin getting errors.
Also if you have many transitions going on at one time it will slow down performance.
Note: Some transitions are slower than others as they aren't generally suited for hardware acceleration (effects that does many small pixel effects). Try out the performance of them and find one that suits your purposes and runs fast enough.
Instead of using transitions try using animations or other effects to give the same effect.
Instead of using the "Fade" transition, manually setting the semi transparency of your objects will be much much faster and will not consume any more memory.
I have seen other games almost ready for release where INK effects are unnecessarily used very often.
Instead of having a big background covering the screen and setting it to either transparent or ADD so that it will be added to some gradient behind it, it will only make your game slower and consume more power. Instead, make your graphics as they should look at runtime and then use them like that. INK-effects are only meant for places where you cannot modify the original image to look like it should at edit time.
For example if you want a grayscale background from a picture, you make it grayscale before you import it into MMF2. You simply don't put the monochrome INK-effect on it and forget about it