CWaveFile *CSound::loadSound(const CString &name) { checkSounds(); // Check whether an entry for the given name is already active for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ++i) { CSoundItem *soundItem = *i; if (soundItem->_name == name) { // Found it, so move it to the front of the list and return _sounds.remove(soundItem); _sounds.push_front(soundItem); return soundItem->_waveFile; } } // Create new sound item CSoundItem *soundItem = new CSoundItem(name); soundItem->_waveFile = _soundManager.loadSound(name); if (!soundItem->_waveFile) { // Couldn't load sound, so destroy new item and return delete soundItem; return 0; } // Add the item to the list of sounds _sounds.push_front(soundItem); // If there are more than 10 sounds loaded, remove the last one, // which is the least recently used of all of them if (_sounds.size() > 10) removeOldest(); return soundItem->_waveFile; }
CWaveFile *CSound::loadSpeech(CDialogueFile *dialogueFile, int speechId) { checkSounds(); // Check whether an entry for the given name is already active for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ++i) { CSoundItem *soundItem = *i; if (soundItem->_dialogueFileHandle == dialogueFile->getFile() && soundItem->_speechId == speechId) { // Found it, so move it to the front of the list and return _sounds.remove(soundItem); _sounds.push_front(soundItem); return soundItem->_waveFile; } } // Create new sound item CSoundItem *soundItem = new CSoundItem(dialogueFile->getFile(), speechId); soundItem->_waveFile = _soundManager.loadSpeech(dialogueFile, speechId); if (!soundItem->_waveFile) { // Couldn't load speech, so destroy new item and return delete soundItem; return 0; } // Add the item to the list of sounds _sounds.push_front(soundItem); // If there are more than 10 sounds loaded, remove the last one, // which is the least recently used of all of them if (_sounds.size() > 10) removeOldest(); return soundItem->_waveFile; }
void set(int key, int value) { tick(); auto result = cache.find(key); if (result != cache.end()) { // old value are removed here // "pool" only cares about the timestamp, // the other two values are unnecessary pool.erase( Entity(-1,-1,result->second.timestamp) ); // also remove it from "cache" cache.erase( result ); // runs in: // O(log(n)) + O(log(n)) = O(log(n)) time } Entity e(key,value,timestamp); // O(log(n)), insert both in "cache" and in "pool" cache.insert( std::pair<int,Entity>(key,e) ); pool.insert(e); // after an insertion, the element size might exceed the capacity, // if this is the case, remove the oldest one if (cache.size() > capacity) removeOldest(); }
int SpriteCache::loadSprite(int index) { int hh = 0; while (cachesize > maxCacheSize) { removeOldest(); hh++; if (hh > 1000) { write_log("!!!! RUNTIME CACHE ERROR: STUCK IN FREE_UP_MEM; RESETTING CACHE"); removeAll(); } } if ((index < 0) || (index >= elements)) quit("sprite cache array index out of bounds"); // If we didn't just load the previous sprite, seek to it seekToSprite(index); int coldep = getshort(ff); if (coldep == 0) { lastLoad = index; return 0; } int wdd = getshort(ff); int htt = getshort(ff); // update the stored width/height spritewidth[index] = wdd; spriteheight[index] = htt; images[index] = create_bitmap_ex(coldep * 8, wdd, htt); if (images[index] == NULL) { offsets[index] = 0; return 0; } if (this->spritesAreCompressed) { getw(ff); // skip data size if (coldep == 1) { for (hh = 0; hh < htt; hh++) cunpackbitl(&images[index]->line[hh][0], wdd, ff); } else if (coldep == 2) { for (hh = 0; hh < htt; hh++) cunpackbitl16((unsigned short*)&images[index]->line[hh][0], wdd, ff); } else { for (hh = 0; hh < htt; hh++) cunpackbitl32((unsigned long*)&images[index]->line[hh][0], wdd, ff); } } else { for (hh = 0; hh < htt; hh++) // MACPORT FIX: size and nmemb split fread(&images[index]->line[hh][0], coldep, wdd, ff); } lastLoad = index; // Stop it adding the sprite to the used list just because it's loaded long offs = offsets[index]; offsets[index] = SPRITE_LOCKED; initialize_sprite(index); if (index != 0) // leave sprite 0 locked offsets[index] = offs; // we need to store this because the main program might // alter spritewidth/height if it resizes stuff sizes[index] = spritewidth[index] * spriteheight[index] * coldep; cachesize += sizes[index]; #ifdef DEBUG_SPRITECACHE char msgg[100]; sprintf(msgg, "Loaded %d, size now %d KB", index, cachesize / 1024); write_log(msgg); #endif return sizes[index]; }