Game Development Reference
In-Depth Information
'
Here
s an example of how this class is used. You construct the cache with a size in
mind, in our case 50MB, and an object that implements the IResourceFile inter-
face. You then call Init() to allocate the cache and open the file.
ResourceZipFile zipFile(
Assets.zip
);
ResCache resCache (50, zipFile);
if (m_ResCache.Init())
{
Resource resource(
);
shared_ptr<ResHandle> texture = g_pApp->m_ResCache->GetHandle(&resource);
int size = texture->GetSize();
char *brickBitmap = (char *) texture->Buffer();
// do something cool with brickBitmap !
art\\brick.bmp
}
If you want to use this in a real game, you
s
hardly a line of defensive or debugging code in ResCache . Resource caches are a
significant source of bugs and other mayhem. Data corruption from buggy cache
code or something else trashing the cache internals will cause your game to simply
freak out.
A functional cache will need to be aware of more than one resource file. It
'
ve got more work to do. First, there
'
s not rea-
sonable to assume that a game can stuff every resource into a single file, especially
since it makes it inconvenient for teams. If every resource were stuffed into a single
file, then even the change of a minor texture in the options screen would cause every
person on the team to grab a new copy of the entire resource file for the game, which
could be multiple gigabytes. Break your game up into some reasonable number of
resource files, and you
'
'
ll be happier for it.
Write a Custom Memory Manager
Consider implementing your own memory allocator. Many resource caches
allocate one contiguous block of memory when they initialize and manage
the block internally. Some even have garbage collection, where the
resources are moved around as the internal block becomes fragmented. A
garbage collection scheme is an interesting problem, but it is extremely
difficult to implement a good one that doesn
'
t make the game stutter.
Ultima VIII used a scheme like this.
That brings us to the idea of making the cache multithreading compliant. Why not
have the cache defrag itself if there
s some extra time in the main loop, or perhaps
allow a reader in a different thread to fill the cache with resources that might be used
in the near future? With high-definition consoles like the PS3 and Xbox360, this area
of game programming is getting a lot of attention. The new multiprocessor systems
'
 
Search WWH ::




Custom Search