Game Development Community

How to shrink your app

by Ronny Bangsund · in Torque 2D for the iPhone · 11/03/2009 (7:44 pm) · 8 replies

"Does my code look fat in this?"


You've just downloaded the latest beta of Torque 2D for iPhone, previously known as iTGB, hereafter T2Di. After wrestling with this strange new Mac you just bought to make iPhone games, you manage to actually compile the whole thing.

Shock, horror - the built app is gigantic!
in.structive.net/_media/torque:huge.pngThis certainly seems a bit large. You've got all the assets on top of that, making the magic 10MB limit seem hard to reach when your game has a bit of substance.





A few easy steps

There are fortunately a few minor toggles and a preprocessor flag available to shrink this down. I'll also mention another trick or two. When making mobile applications, every little kilobyte counts (kilobyte here meaning the discounted 1000-byte kilobytes of Snow Leopard).





We'll use the iTGB_Script_Optimize target:
in.structive.net/_media/torque:targetinfo.png




Scroll down to the Targets in the tree on the left-hand side of the project, and choose Get Info for this target:
in.structive.net/_media/torque:targetinfo.png




Ensure you have the Release configuration selected:
in.structive.net/_media/torque:configuration.png




Linker flags

  • Deployment Postprocessing: YES
  • Strip Debug Symbols During Copy: YES
  • Strip Linked Product: YES
  • Strip Style: All Symbols
  • Use Separate Strip: YES
in.structive.net/_media/torque:compilerlinker.png




Preprocessor flags

There is one more thing which causes a little bloat. Most of the TorqueScript functions also have some explanatory text, which should disappear with the right flag defined. This is not defined in this project. Add TORQUE_SHIPPING to trim about 300kB off the build:
in.structive.net/_media/torque:shipping.png




Results

Build this and you should have a somewhat slimmer binary:
in.structive.net/_media/torque:result.png




Other optimisations

PNGs

If you have any PNGs at all, it can be worth your time to run a tool called pngout on them for optimisation. This tool removes the metadata you don't need to just display the images, and has its own compressor which does a better job than the gzip algorithm PNG is based on. The result is 100% compatible with the PNG decompressor, while gaining a bit more.
Installation instructions and a useful script

A flag has already been defined in the project to skip recompression of the PNGs, so they should stay intact. It should be noted that GG's method in this case isn't the correct one ;)

Disable the flag named Compress PNG Files:
in.structive.net/_media/torque:pngflag.png
From Orion Elenzil:
You can also use palette-based PNGs. Photoshop, GIMP and ImageMagick all support this conversion process (easiest in the first two).





JPEGs

Thanks to Orion Elenzil for this tip!
If you can make an image a JPEG and still have it look good, it's an alternative to the colour-reduction of PVRTC.
See this post by Vern Jensen for some info on using JPEGs with alpha channels.

Fonts

There might be leftover UFTs, sometimes taking up several megabytes, if you've been experimenting a bit with GUIs. Delete them from common/data/fonts and run TGBGame on your desktop to create new cached fonts.





Other files

That should help a little if you're really close to the 10MB mark for your app bundle. There are other things you can remove on a per-project basis. GUIs and scripts in common/ are probably not going to be needed, and if you don't ever show a mouse cursor (pretty common) just delete it. Metrics, console and server scripts can all be removed from the release build.

Thanks to Sven Bergström for having a look at this before I posted :)

I'll add any other small (somewhat generic) tips which might fit - just let me know. Things related to the actual code and what can be removed will change for the next release of T2Di, but there is a fairly large chunk of unnecessary code (700k compiled) which can be removed if you're patient and careful.

#1
11/03/2009 (7:51 pm)
great post, ronny.

another method for shrinking the size of PNGs is to make sure they're palette-based instead of full-color. this is something you can do in photoshop or gimp, or it's not hard to write an imagemagick script to strip meta-data, convert to palette, sanity-check dimensions, etc. also, if you find you have a large PNG image that would compress better as a JPEG, but you need transparency, then splitting the jpeg into a color jpeg and an alpha-channel jpeg may help.
#2
11/03/2009 (8:04 pm)
Thanks, added your tips :)
Maybe I'll elaborate on the palette-based formats later.
#3
11/03/2009 (8:46 pm)
Great post and I can attest that these tips work and work well. I was able to trim 3 seconds off my load time (from 10 down to 7, including the 5 second GG splash) and keep the app size flat despite adding a significant new portion of content thanks to Ronny's tips.
#4
11/03/2009 (8:52 pm)
This is great.
especially TORQUE_SHIPPING that's my favorite :)
#5
11/04/2009 (11:22 am)
Stickied for reference. Huge props for posting this neatly - Thanks Ronny!
#6
11/04/2009 (6:21 pm)
Duplicate your TGB Project and delete all your .cs and .tgb files from common and game(just keep the dsos).
Compile/deploy this stripped version.
This will save you about 500kB.
#7
11/05/2009 (10:12 pm)
Actually one thing not mentioned is just the process of removing features from the engine. I removed most GUI elements, many object types, support for non-PVR or Non-PNG images, etc... and was really able to trim things down.
#8
11/05/2009 (10:36 pm)
@Zilla & Chris:

Sven suggested I hold off on any trickery specific to certain games. I guess there are some big changes in file organisation and included features coming soon. This little guide is just stuff you can memorise and quickly do for any project.

About fonts: I actually think this needs some investigating, as I'm not 100% they're always recreated here. Should they just create UFTs from anything found in the various profiles? I'd like a tool I could use in Xcode while building to create the fonts, too, since it's near xmas :)