1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 06:52:34 +02:00

Store ptr to GameWorld in SoundManager

It's allowed to remove duplicated calls of
LoaderSDT's `load`, and need of calling `loadSound`
before `createSfxInstance`.
This commit is contained in:
Filip Gawin 2018-08-12 01:08:14 +02:00
parent 1945d67818
commit 5b110126d2
5 changed files with 22 additions and 12 deletions

View File

@ -8,6 +8,8 @@ extern "C" {
}
#include "audio/alCheck.hpp"
#include "engine/GameData.hpp"
#include "engine/GameWorld.hpp"
#include <rw/types.hpp>
@ -25,7 +27,9 @@ Sound& SoundManager::getSoundRef(const std::string& name) {
return sounds[name]; // @todo reloading, how to check is it wav/mp3?
}
SoundManager::SoundManager() {
SoundManager::SoundManager(GameWorld* engine) : _engine(engine) {
sdt.load(_engine->data->getDataPath() / "audio/sfx");
initializeOpenAL();
initializeAVCodec();
}
@ -97,7 +101,7 @@ bool SoundManager::loadSound(const std::string& name,
return sound->isLoaded;
}
void SoundManager::loadSfxSound(const rwfs::path& path, size_t index) {
void SoundManager::loadSound(size_t index) {
Sound* sound = nullptr;
auto emplaced =
@ -106,13 +110,19 @@ void SoundManager::loadSfxSound(const rwfs::path& path, size_t index) {
sound = &emplaced.first->second;
sound->source = std::make_shared<SoundSource>();
sound->source->loadSfx(path, sdt, index);
sound->source->loadSfx(sdt, index);
}
size_t SoundManager::createSfxInstance(size_t index) {
Sound* sound = nullptr;
auto soundRef = sfx.find(index);
if(soundRef == sfx.end()) {
// Sound source is not loaded yet
loadSound(index);
soundRef = sfx.find(index);
}
// Try to reuse first available buffer
// (aka with stopped state)
for (auto& sound : buffers) {

View File

@ -18,6 +18,8 @@
#include <rw/filesystem.hpp>
#include <loaders/LoaderSDT.hpp>
class GameWorld;
/// Game's sound manager.
/// It handles all stuff connected with sounds.
/// Worth noted: there are three types of sounds,
@ -26,14 +28,14 @@
/// instances simultaneously without duplicating raw source).
class SoundManager {
public:
SoundManager();
SoundManager(GameWorld* engine);
~SoundManager();
/// Load sound from file and store it with selected name
bool loadSound(const std::string& name, const std::string& fileName);
/// Load all sfx sounds
void loadSfxSound(const rwfs::path& path, size_t index);
/// Load selected sfx sound
void loadSound(size_t index);
Sound& getSoundRef(size_t name);
Sound& getSoundRef(const std::string& name);
@ -105,6 +107,7 @@ private:
/// Nr of already created buffers
size_t bufferNr = 0;
GameWorld* _engine;
LoaderSDT sdt{};
};

View File

@ -254,7 +254,7 @@ static int read_packet(void* opaque, uint8_t* buf, int buf_size) {
return buf_size;
}
void SoundSource::loadSfx(const rwfs::path& path, LoaderSDT& sdt, size_t index, bool asWave) {
void SoundSource::loadSfx(LoaderSDT& sdt, size_t index, bool asWave) {
// Allocate audio frame
AVFrame* frame = av_frame_alloc();
if (!frame) {
@ -262,9 +262,6 @@ void SoundSource::loadSfx(const rwfs::path& path, LoaderSDT& sdt, size_t index,
return;
}
sdt.load(path / "audio/sfx");
/// Now we need to prepare "custom" format context
/// We need sdt loader for that purpose
std::unique_ptr<char[]> raw_sound = sdt.loadToMemory(index, asWave);

View File

@ -16,7 +16,7 @@ public:
void loadFromFile(const rwfs::path& filePath);
/// Load sound from sdt file
void loadSfx(const rwfs::path& path, LoaderSDT& sdt, size_t index, bool asWave = true);
void loadSfx(LoaderSDT& sdt, size_t index, bool asWave = true);
private:
/// Raw data

View File

@ -54,7 +54,7 @@ public:
};
GameWorld::GameWorld(Logger* log, GameData* dat)
: logger(log), data(dat) {
: logger(log), data(dat), sound(this) {
data->engine = this;
collisionConfig = std::make_unique<btDefaultCollisionConfiguration>();