Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 6, 2009, 5:02:11 PM (15 years ago)
Author:
rgrieder
Message:

Added sound effects pooling. This should avoid long respawns (due to sound loading) if there are no more enemies.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2/src/orxonox/sound/SoundManager.cc

    r6244 r6254  
    5252
    5353    SoundManager::SoundManager()
     54        : effectsPoolSize_(0)
    5455    {
    5556        RegisterRootObject(SoundManager);
     
    529530    shared_ptr<SoundBuffer> SoundManager::getSoundBuffer(shared_ptr<ResourceInfo> fileInfo)
    530531    {
    531         std::map<std::string, weak_ptr<SoundBuffer> >::const_iterator it
    532             = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
     532        shared_ptr<SoundBuffer> buffer;
     533        // Check active or pooled buffers
     534        SoundBufferMap::const_iterator it = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
    533535        if (it != this->soundBuffers_.end())
    534             return it->second.lock();
     536        {
     537            buffer = it->second;
     538
     539            // Remove from effects pool if not active used before
     540            if (buffer->poolIterator_ != this->effectsPool_.end())
     541            {
     542                this->effectsPoolSize_ -= buffer->getSize();
     543                this->effectsPool_.erase(buffer->poolIterator_);
     544                buffer->poolIterator_ = this->effectsPool_.end();
     545            }
     546        }
    535547        else
    536548        {
    537             shared_ptr<SoundBuffer> buffer;
    538549            try
    539550            {
    540551                buffer.reset(new SoundBuffer(fileInfo));
     552                buffer->poolIterator_ = this->effectsPool_.end();
    541553            }
    542554            catch (...)
    543555            {
    544556                COUT(1) << Exception::handleMessage() << std::endl;
    545                 return shared_ptr<SoundBuffer>();
     557                return buffer;
    546558            }
    547559            this->soundBuffers_[fileInfo->group + '/' + fileInfo->filename] = buffer;
    548             return buffer;
    549         }
    550     }
    551 
    552     void SoundManager::removeBuffer(shared_ptr<ResourceInfo> fileInfo)
    553     {
    554         std::map<std::string, weak_ptr<SoundBuffer> >::iterator it
    555             = this->soundBuffers_.find(fileInfo->group + '/' + fileInfo->filename);
     560        }
     561        return buffer;
     562    }
     563
     564    void SoundManager::releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer)
     565    {
     566        // Check if others are still using the buffer
     567        if (buffer.use_count() != 2)
     568            return;
     569        SoundBufferMap::iterator it = this->soundBuffers_.find(buffer->fileInfo_->group + '/' + buffer->fileInfo_->filename);
    556570        if (it != this->soundBuffers_.end())
    557             this->soundBuffers_.erase(it);
     571        {
     572            if (bPoolBuffer)
     573            {
     574                // Pool already too large?
     575                while (this->effectsPoolSize_ + it->second->getSize() > this->maxEffectsPoolSize_s && !this->effectsPool_.empty())
     576                {
     577                    shared_ptr<SoundBuffer> bufferDel = this->effectsPool_.back();
     578                    this->effectsPoolSize_ -= bufferDel->getSize();
     579                    bufferDel->poolIterator_ = this->effectsPool_.end();
     580                    this->effectsPool_.pop_back();
     581                    // Remove from buffer map too
     582                    SoundBufferMap::iterator itDel = this->soundBuffers_.find(bufferDel->fileInfo_->group + '/' + bufferDel->fileInfo_->filename);
     583                    if (itDel != this->soundBuffers_.end())
     584                        this->soundBuffers_.erase(itDel);
     585                }
     586                // Put buffer into the pool
     587                this->effectsPoolSize_ += it->second->getSize();
     588                this->effectsPool_.push_front(it->second);
     589                it->second->poolIterator_ = this->effectsPool_.begin();
     590                COUT(0) << "pool size: " << this->effectsPoolSize_ << std::endl;
     591            }
     592            else
     593                this->soundBuffers_.erase(it);
     594        }
    558595    }
    559596}
Note: See TracChangeset for help on using the changeset viewer.