Game Development Reference
In-Depth Information
ï?®
Method calls have a larger associated cost in Dalvik than in other VMs.
Use static methods if you can, as those perform best. Static methods are
generally regarded as evil, much like static variables, as they promote bad
design, so try to keep your design as clean as possible. Perhaps you should
avoid getters and setters as well. Direct field access is about three times
faster than method invocations without the JIT compiler, and about seven
times faster with the JIT compiler. Nevertheless, think of your design before
removing all your getters and setters.
ï?®
Floating-point operations are implemented in software on older devices and
Dalvik versions without a JIT compiler (anything before Android version 2.2).
Old-school game developers would immediately fall back to fixed-point
math. Don't do that either, since integer divisions are slow as well. Most of
the time, you can get away with floats, and newer devices sport Floating
Point Units (FPUs), which speed things up quite a bit once the JIT compiler
kicks in.
ï?®
Try to cram frequently accessed values into local variables inside a method.
Accessing local variables is faster than accessing members or calling
getters.
Of course, you need to be careful about many other things. We'll sprinkle the rest of the
book with some performance hints when the context calls for it. If you follow the preceding
recommendations, you should be on the safe side. Just don't let the garbage collector win!
Summary
This chapter covered everything you need to know in order to write a decent little 2D game
for Android. We looked at how easy it is to set up a new game project with some defaults. We
discussed the mysterious activity life cycle and how to live with it. We battled with touch (and
more importantly, multitouch) events, processed key events, and checked the orientation of
our device via the accelerometer. We explored how to read and write files. Outputting audio on
Android turns out to be child's play, and apart from the threading issues with the SurfaceView ,
drawing stuff to the screen isn't that hard either. Mr. Nom can now become a reality—a terrible,
hungry reality!
 
Search WWH ::




Custom Search