Skip to content

Commit

Permalink
KOTOR: Add sound object class
Browse files Browse the repository at this point in the history
  • Loading branch information
Nostritius authored and DrMcCoy committed Jul 30, 2018
1 parent 0e358a7 commit d630ce9
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/engines/kotor/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ src_engines_kotor_libkotor_la_SOURCES += \
src/engines/kotor/placeable.h \
src/engines/kotor/door.h \
src/engines/kotor/creature.h \
src/engines/kotor/sound.h \
src/engines/kotor/area.h \
src/engines/kotor/room.h \
src/engines/kotor/objectcontainer.h \
Expand Down Expand Up @@ -63,6 +64,7 @@ src_engines_kotor_libkotor_la_SOURCES += \
src/engines/kotor/placeable.cpp \
src/engines/kotor/door.cpp \
src/engines/kotor/creature.cpp \
src/engines/kotor/sound.cpp \
src/engines/kotor/area.cpp \
src/engines/kotor/room.cpp \
src/engines/kotor/objectcontainer.cpp \
Expand Down
102 changes: 102 additions & 0 deletions src/engines/kotor/sound.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* An ingame sound in kotor.
*/

#include "src/aurora/resman.h"
#include "src/aurora/gff3file.h"

#include "src/sound/sound.h"

#include "src/engines/aurora/util.h"

#include "src/engines/kotor/sound.h"

namespace Engines {

namespace KotOR {

SoundObject::SoundObject(const Aurora::GFF3Struct &sound) {
Common::UString temp = sound.getString("TemplateResRef");

Common::ScopedPtr<Aurora::GFF3File> uts;
if (!temp.empty())
uts.reset(loadOptionalGFF3(temp, Aurora::kFileTypeUTS, MKTAG('U', 'T', 'S', ' ')));

if (!uts)
throw Common::Exception("Sound \"%s\" has no blueprint", _tag.c_str());

const Aurora::GFF3Struct &gff = uts->getTopLevel();

Aurora::GFF3List soundFileList = gff.getList("Sounds");
for (Aurora::GFF3List::const_iterator c = soundFileList.begin(); c != soundFileList.end(); ++c) {
_soundFiles.push_back((*c)->getString("Sound"));
}

_looping = gff.getBool("Looping");
_positional = gff.getBool("Positional");
_random = gff.getBool("Random");

_interval = gff.getUint("Interval");

// TODO: Investigate how kotor handles randomness of sounds.
Common::UString soundFile = _soundFiles[0];
if (_random)
soundFile = _soundFiles[std::rand() % _soundFiles.size()];

Common::SeekableReadStream *soundStream = ResMan.getResource(Aurora::kResourceSound, soundFile);

_sound = SoundMan.playSoundFile(soundStream, Sound::kSoundTypeSFX, _looping);

_name = gff.getString("Tag");

if (gff.getBool("Active")) {
play();
}

setPosition(
static_cast<float>(sound.getDouble("XPosition")),
static_cast<float>(sound.getDouble("YPosition")),
static_cast<float>(sound.getDouble("ZPosition"))
);

SoundMan.setChannelGain(_sound, static_cast<float>(gff.getUint("Volume"))/100.0f);
}

void SoundObject::setPosition(float x, float y, float z) {
Object::setPosition(x, y, z);

if (_positional)
SoundMan.setChannelPosition(_sound, x, y, z);
}

void SoundObject::play() {
SoundMan.startChannel(_sound);
}

void SoundObject::stop() {
SoundMan.stopChannel(_sound);
}

} // End of namespace KotOR

} // End of namespace Engines
61 changes: 61 additions & 0 deletions src/engines/kotor/sound.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* An ingame sound in kotor.
*/

#ifndef ENGINES_KOTOR_SOUND_H
#define ENGINES_KOTOR_SOUND_H

#include "src/events/timerman.h"

#include "src/engines/kotor/object.h"

namespace Engines {

namespace KotOR {

class SoundObject : public Object {
public:
SoundObject(const Aurora::GFF3Struct &sound);

void setPosition(float x, float y, float z);

void play();
void stop();

private:
bool _positional;
bool _looping;
bool _random;

unsigned int _interval;

std::vector<Common::UString> _soundFiles;

Sound::ChannelHandle _sound;
};

} // End of namespace KotOR

} // End of namespace Engines

#endif // ENGINES_KOTOR_SOUND_H

0 comments on commit d630ce9

Please sign in to comment.