Basics of Resource Management
Java unlike C/C++ does not allow you to deallocate your own allocated memory. Nor does it allow you to know the size of physical memory used by an object. In games we need to allocate more and more resources as the game gets progressively longer. We wouldn't want to load all 50 1024x768 level backgrounds if we are making a puzzle game with unique backgrounds. Or say 500 64x64 animated sprites for a platformer. The VM will simply exceed its heap size limit and you'll see this nasty little message "java.lang.OutOfMemoryError" you can increase the heap size using VM command line arguments.java -Xms
The defaults are:
java -Xms32 -Xmx128
Even so you don't want to be eating up a users resources especially if you are an applet game or a mobile game. So you need some way of deallocating memory and releasing unused resources.
The Simple Method
Alternative Resource Handling
import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageResource { //The one and only reference to the Image private BufferedImage realObject = null; private File resourcePath = null; public ImageResource(String path) throws IOException { resourcePath = new File(path); reload(); } public boolean isLoaded() { return realObject == null; } public void reload() throws IOException { realObject = ImageIO.read(resourcePath); } public void release() { realObject = null; } public void render(Graphics2D g) { g.drawImage(realObject, 0, 0, null); } public int getWidth() { return realObject.getWidth(); } public int getHeight() { return realObject.getHeight(); } }Another option to providing Proxy methods is to use a Visitor pattern for performing operations on specific Resource classes. But that is a more detailed idea for another day perhaps.
No comments:
Post a Comment