Game Development Reference
In-Depth Information
class OggResourceLoader : public IResourceLoader
{
public:
virtual bool VUseRawFile() { return false; }
virtual unsigned int VGetLoadedResourceSize(char *rawBuffer,
unsigned int rawSize);
virtual bool VLoadResource(char *rawBuffer, unsigned int rawSize,
shared_ptr<ResHandle> handle);
virtual std::string VGetPattern() { return
*.ogg
;}
protected:
bool ParseOgg(char *oggStream, size_t length, shared_ptr<ResHandle> handle);
};
The SoundResourceExtraData class stores data that will be used by DirectSound. It
is initialized when the resource cache loads the sound. Take a look at the protected
members first. The m_SoundType members store an enumeration that defines the dif-
ferent sound types you support: WAV, OGG, and so on. The next Boolean stores
whether the sound has been initialized, which is to say that the sound is ready to play.
The next data member, m_wavFormatEx , stores information about the sound so that
DirectSound can play it. This includes how many channels are included in the sound,
its sample rate, its bits per sample, and other data. The last member is a convenience
member used to grab the length of the sound in milliseconds, which is nice to have if
you are timing something, like an animation, to coincide with the end of the sound.
A real game would keep compressed sounds in memory and send bits and pieces of
them into the audio hardware as they were needed, saving precious memory space. For
longer pieces such as music, the system might even stream bits of the compressed music
from digital media and then uncompress those bits as they were consumed by the audio
card. As you can see, that system could use its own book to describe it thoroughly.
The resource cache will use implementations of the IResourceLoader interface to
determine what kind of resource the sound is and the size of the loaded resource and
to actually load the resource into the memory the resource cache allocates.
Stream Your Music
A better solution for music files, which tend to be huge in an uncompressed form,
is to stream them into memory as the sound data is played. This is a complicated
subject, so for now we
ll simply play uncompressed sound data that is loaded
completely into memory. Notice that even though a multimegabyte OGG file is
loaded into a decompressed buffer,
'
taking up perhaps 10 times as much
memory,
it
loads many
times
faster. As
you might expect,
the Vorbis
decompression algorithm is much faster than your hard drive.
 
Search WWH ::




Custom Search