-
-class sound_resource_loader
-{
-public:
-
- sound_resource_loader()
- {
- resource::register_type<sound_resource>("ogg", "sounds");
- }
-
- ~sound_resource_loader()
- {
- resource::unregister_type("ogg");
- }
-};
-
-static sound_resource_loader loader;
-
-
-
-// SOUND BUFFER
-
-class buffer
-{
-public:
-
- typedef hash<ALuint,int,hash_function> retcount_lookup;
-
-
- buffer() :
- buffer_((ALuint)-1) {}
-
- buffer(const void* data,
- ALsizei size,
- ALenum format,
- ALsizei freq)
- {
- alGenBuffers(1, &buffer_);
- alBufferData(buffer_, format, data, size, freq);
-
- retain_counts_[buffer_] = 1;
- }
-
- buffer(const buffer& buf)
- {
- buffer_ = buf.buffer_;
- retain();
- }
-
- buffer& operator = (const buffer& buf)
- {
- buffer_ = buf.buffer_;
- retain();
- return *this;
- }
-
- ~buffer()
- {
- release();
- }
-
-
- void queue(ALuint source) const
- {
- if (*this)
- {
- alSourceQueueBuffers(source, 1, &buffer_);
- retain();
- }
- }
-
- static buffer unqueue(ALuint source)
- {
- ALuint buf = (ALuint)-1;
- alSourceUnqueueBuffers(source, 1, &buf);
- return buffer(buf);
- }
-
- void set(ALuint source) const
- {
- if (*this) alSourcei(source, AL_BUFFER, buffer_);
- }
-
- operator bool () const
- {
- return buffer_ != (ALuint)-1;
- }
-
-
-private:
-
- explicit buffer(ALuint buf) :
- buffer_(buf) {}
-
-
- void retain() const
- {
- retcount_lookup::iterator it = retain_counts_.find(buffer_);
- if (it.valid()) ++it->second;
- }
-
- void release() const
- {
- retcount_lookup::iterator it = retain_counts_.find(buffer_);
- if (it.valid() && --it->second <= 0)
- {
- alDeleteBuffers(1, &buffer_);
- retain_counts_.erase(it);
- }
- }
-
-
- ALuint buffer_;
- sound_backend backend_;
-
- static retcount_lookup retain_counts_;
-};
-
-buffer::retcount_lookup buffer::retain_counts_;
-