FULLPIPE: Improve memory safety and fix leaks in sound code

This commit is contained in:
Colin Snover 2017-11-12 10:16:34 -06:00 committed by Eugene Sandulenko
parent 8f1d76d261
commit 72a4292727
7 changed files with 40 additions and 61 deletions

View File

@ -345,7 +345,7 @@ int global_messageHandler2(ExCommand *cmd) {
SoundList *s = g_fp->_currSoundList1[snd];
int ms = s->getCount();
for (int i = 0; i < ms; i++) {
s->getSoundByIndex(i)->setPanAndVolumeByStaticAni();
s->getSoundByIndex(i).setPanAndVolumeByStaticAni();
}
}
}

View File

@ -1504,11 +1504,11 @@ void ModalMainMenu::updateVolume() {
}
}
void ModalMainMenu::updateSoundVolume(Sound *snd) {
if (!snd->_objectId)
void ModalMainMenu::updateSoundVolume(Sound &snd) {
if (!snd._objectId)
return;
StaticANIObject *ani = g_fp->_currentScene->getStaticANIObject1ById(snd->_objectId, -1);
StaticANIObject *ani = g_fp->_currentScene->getStaticANIObject1ById(snd._objectId, -1);
if (!ani)
return;
@ -1522,7 +1522,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) {
if (ani->_oy <= _screct.bottom) {
if (ani->_oy >= _screct.top) {
snd->setPanAndVolume(g_fp->_sfxVolume, 0);
snd.setPanAndVolume(g_fp->_sfxVolume, 0);
return;
}
@ -1534,7 +1534,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) {
par = 0;
if (dx > 800) {
snd->setPanAndVolume(-3500, 0);
snd.setPanAndVolume(-3500, 0);
return;
}
@ -1545,7 +1545,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) {
int dx = ani->_ox - _screct.right;
if (dx > 800) {
snd->setPanAndVolume(-3500, 0);
snd.setPanAndVolume(-3500, 0);
return;
}
@ -1557,7 +1557,7 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) {
int32 pp = b * a;
snd->setPanAndVolume(pan + pp / 800, par);
snd.setPanAndVolume(pan + pp / 800, par);
return;
}
@ -1570,9 +1570,9 @@ void ModalMainMenu::updateSoundVolume(Sound *snd) {
if (p > g_fp->_sfxVolume)
p = g_fp->_sfxVolume;
snd->setPanAndVolume(p, dx * (-3500) / 800);
snd.setPanAndVolume(p, dx * (-3500) / 800);
} else {
snd->setPanAndVolume(-3500, 0);
snd.setPanAndVolume(-3500, 0);
}
}

View File

@ -228,7 +228,7 @@ private:
void enableDebugMenu(char c);
int checkHover(Common::Point &point);
void updateVolume();
void updateSoundVolume(Sound *snd);
void updateSoundVolume(Sound &snd);
void updateSliderPos();
bool isOverArea(PictureObject *obj, Common::Point *point);
};

View File

@ -342,7 +342,7 @@ void Scene::setPictureObjectsFlag4() {
void Scene::stopAllSounds() {
for (int i = 0; i < _soundList->getCount(); i++)
_soundList->getSoundByIndex(i)->stop();
_soundList->getSoundByIndex(i).stop();
}
PictureObject *Scene::getPictureObjectById(int objId, int flags) {

View File

@ -577,7 +577,7 @@ bool FullpipeEngine::sceneSwitcher(EntranceInfo *entrance) {
_currSoundList1[1] = scene->_soundList;
for (int i = 0; i < scene->_soundList->getCount(); i++) {
scene->_soundList->getSoundByIndex(i)->updateVolume();
scene->_soundList->getSoundByIndex(i).updateVolume();
}
} else {
_currSoundListCount = 1;

View File

@ -38,39 +38,23 @@
namespace Fullpipe {
SoundList::SoundList() {
_soundItems = 0;
_soundItemsCount = 0;
_libHandle = 0;
}
SoundList::~SoundList() {
for (int i = 0; i < _soundItemsCount; i++)
delete _soundItems[i];
free(_soundItems);
}
bool SoundList::load(MfcArchive &file, const Common::String &fname) {
debugC(5, kDebugLoading, "SoundList::load()");
_soundItemsCount = file.readUint32LE();
_soundItems = (Sound **)calloc(_soundItemsCount, sizeof(Sound *));
uint32 count = file.readUint32LE();
_soundItems.resize(count);
if (!fname.empty()) {
_libHandle = makeNGIArchive(fname);
_libHandle.reset(makeNGIArchive(fname));
} else {
_libHandle = 0;
_libHandle.reset();
}
for (int i = 0; i < _soundItemsCount; i++) {
Sound *snd = new Sound();
_soundItems[i] = snd;
snd->load(file, _libHandle);
for (uint i = 0; i < count; i++) {
_soundItems[i].load(file, _libHandle.get());
}
return true;
}
bool SoundList::loadFile(const Common::String &fname, const Common::String &libname) {
@ -85,26 +69,21 @@ bool SoundList::loadFile(const Common::String &fname, const Common::String &libn
}
Sound *SoundList::getSoundItemById(int id) {
if (_soundItemsCount == 0) {
return _soundItems[0]->getId() != id ? 0 : _soundItems[0];
for (uint i = 0; i < _soundItems.size(); ++i) {
if (_soundItems[i].getId() == id)
return &_soundItems[i];
}
for (int i = 0; i < _soundItemsCount; i++) {
if (_soundItems[i]->getId() == id)
return _soundItems[i];
}
return NULL;
return nullptr;
}
Sound::Sound() {
_id = 0;
_directSoundBuffer = 0;
_soundData = 0;
_objectId = 0;
memset(_directSoundBuffers, 0, sizeof(_directSoundBuffers));
_volume = 100;
_handle = new Audio::SoundHandle();
}
Sound::Sound() :
_id(0),
_directSoundBuffer(0),
_directSoundBuffers(),
_soundData(nullptr),
_handle(new Audio::SoundHandle()),
_volume(100),
_objectId(0) {}
Sound::~Sound() {
freeSound();
@ -389,7 +368,7 @@ void FullpipeEngine::playOggSound(const Common::String &trackName, Audio::SoundH
void FullpipeEngine::stopAllSounds() {
for (int i = 0; i < _currSoundListCount; i++)
for (int j = 0; j < _currSoundList1[i]->getCount(); j++) {
_currSoundList1[i]->getSoundByIndex(j)->stop();
_currSoundList1[i]->getSoundByIndex(j).stop();
}
}
@ -542,7 +521,7 @@ void FullpipeEngine::updateSoundVolume() {
for (int i = 0; i < _currSoundListCount; i++)
for (int j = 0; j < _currSoundList1[i]->getCount(); j++) {
_currSoundList1[i]->getSoundByIndex(j)->setPanAndVolume(_sfxVolume, 0);
_currSoundList1[i]->getSoundByIndex(j).setPanAndVolume(_sfxVolume, 0);
}
}

View File

@ -23,6 +23,9 @@
#ifndef FULLPIPE_SOUND_H
#define FULLPIPE_SOUND_H
#include "common/array.h"
#include "common/ptr.h"
namespace Audio {
class SoundHandle;
}
@ -60,19 +63,16 @@ public:
};
class SoundList : public CObject {
Sound **_soundItems;
int _soundItemsCount;
NGIArchive *_libHandle;
Common::Array<Sound> _soundItems;
Common::ScopedPtr<NGIArchive> _libHandle;
public:
SoundList();
~SoundList();
virtual bool load(MfcArchive &file, const Common::String &fname);
virtual bool load(MfcArchive &file) { assert(0); return false; } // Disable base class
bool loadFile(const Common::String &fname, const Common::String &libname);
int getCount() { return _soundItemsCount; }
Sound *getSoundByIndex(int idx) { return _soundItems[idx]; }
int getCount() { return _soundItems.size(); }
Sound &getSoundByIndex(int idx) { return _soundItems[idx]; }
Sound *getSoundItemById(int id);
};