At any given time, there can be up to 50 bullets on screen. The texture for the bullet is 256 B. In addition, there can be up to around 20 asteroids. The asteroid texture averages 80 KB. So, worst case, we would be using roughly 1600 KB (1.5 MB) of memory, just to hold the textures!
Here is the original code(called every time a game object was created):
So, every time we created an object, we were reading from disk, and adding more memory, to hold the texture.
protected TileSet GetTileSet(
Device dev,
int rows,
int cols)
{}
string path = Path.Combine(AppSettings["ImageDir"],
TextureFileName);
if (File.Exists(path))
{
texture = TextureLoader.FromFile(dev, path);
return new TileSet(texture, 0, 0, rows, cols, 16, 16);
}
else
throw new FileNotFoundException("Texture not found.");
Here is the new code, split into two parts:
(Called once per game session)
public static void LoadTexturesFromDisk(
string texturePath,
string searchPattern,
Device dev)
{}
string[] texList = Directory.GetFiles(
Path.GetFullPath(texturePath), searchPattern);
if (textures != null)
{
textures.Clear();
}
textures = new Hashtable(texList.Length);
foreach (string texture in texList)
{
TextureManager.textures.Add(
Path.GetFileName(texture),
TextureLoader.FromFile(dev, texture));
}
(Called every time an object is created)
So, now, we have each texture in memory exactly one time, in a static variable. Each object then references the correct texture, basically by using a pointer, thereby saving both disk IO time, and significant memory space. The maximum texture memory footprint for asteroids and bullets is now ~367.5 KB. The savings aren't as great as you would expect, since there are 9 textures for asteroids (three different sizes, with three different types). Still, the memory requirements are now one quarter of the original.
protected TileSet GetTileSet(
Device dev,
int rows,
int cols)
{}
return new TileSet(
TextureManager.Textures[TextureFileName] as Texture,
0, 0, rows, cols,
TexturePixelDimension,
TexturePixelDimension);
Pretty good savings, for one new method, and a couple of lines of code.
No comments:
Post a Comment