The memory problem and the BitmapStore
In all the projects since we introduced the Bitmap
class back in Chapter 13 the class representing the object in the game also held a copy of the Bitmap
. It is slightly different in the previous project because each GameObject
instance had a graphics-related component class of some type which held the Bitmap
. It still amounts to the same thing because each game object still has/needed a copy of the appropriate Bitmap
.
When we have just a few of each alien type as we did in the previous project this is not a problem but, in this project, we will have more than a hundred of some of the Bitmaps representing the tiles that make the platforms. At best this is inefficient and will waste memory, device power and make the game run more slowly and at worst (especially on older devices) the game will crash because it has run out of memory.
We need a way to share Bitmap
instances between objects. This suggests a central store. Hey; what about that HashMap
and Singleton...