diff options
author | Clyne Sullivan <clyne@bitgloo.com> | 2019-10-01 20:50:28 -0400 |
---|---|---|
committer | Clyne Sullivan <clyne@bitgloo.com> | 2019-10-01 20:50:28 -0400 |
commit | af39f2e08b0503db723ae707a5c7278d8c85f812 (patch) | |
tree | ad6ab6e51e1a7e30d72a28e2bac51b210fd22f6c | |
parent | bebc6e955114a0907c43df9a9c00f22b1f743446 (diff) |
Audio component loading, getting ready to play
-rw-r--r-- | Assets/jump.wav | bin | 0 -> 303194 bytes | |||
-rw-r--r-- | Scripts/init.lua | 3 | ||||
-rw-r--r-- | src/audio.cpp | 25 | ||||
-rw-r--r-- | src/audio.hpp | 3 | ||||
-rw-r--r-- | src/components/Audio.hpp | 17 | ||||
-rw-r--r-- | src/engine.cpp | 2 | ||||
-rw-r--r-- | src/script.cpp | 10 |
7 files changed, 56 insertions, 4 deletions
diff --git a/Assets/jump.wav b/Assets/jump.wav Binary files differnew file mode 100644 index 0000000..1cd1681 --- /dev/null +++ b/Assets/jump.wav diff --git a/Scripts/init.lua b/Scripts/init.lua index ec9e350..ea5a833 100644 --- a/Scripts/init.lua +++ b/Scripts/init.lua @@ -37,6 +37,9 @@ player = { }, Physics = 0, Name = "bord", + Audio = { + file = "Assets/jump.wav" + }, hellotrue = true, hellofalse = false, Render = { diff --git a/src/audio.cpp b/src/audio.cpp index 975cfce..8bb1413 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -20,13 +20,19 @@ #include "audio.hpp" +#include <AL/alut.h> +#include <iostream> + AudioSystem::AudioSystem(void) : device(nullptr, [](ALCdevice *d) { alcCloseDevice(d); }), context(nullptr, [](ALCcontext *c) { alcDestroyContext(c); }) {} AudioSystem::~AudioSystem(void) { + alutExit(); + // Delete context before device + alcMakeContextCurrent(nullptr); context.reset(); device.reset(); } @@ -46,6 +52,9 @@ void AudioSystem::configure([[maybe_unused]] entityx::EntityManager& entities, context.reset(alcCreateContext(device.get(), nullptr)); if (!alcMakeContextCurrent(context.get())) return; // TODO Another uh oh + + if (alutInitWithoutContext(nullptr, nullptr) != AL_TRUE) + return; // TODO Third uh oh } void AudioSystem::update([[maybe_unused]] entityx::EntityManager& entities, @@ -56,10 +65,26 @@ void AudioSystem::update([[maybe_unused]] entityx::EntityManager& entities, void AudioSystem::receive(const entityx::ComponentAddedEvent<Audio>& cae) { alGenSources(1, const_cast<ALuint*>(&cae.component->source)); + //alGenBuffers(1, const_cast<ALuint*>(&cae.component->buffer)); + + if (auto buf = alutCreateBufferFromFile(cae.component->fileName.c_str()); + buf != AL_NONE) { + const_cast<Audio*>(cae.component.get())->buffer = buf; + alSourcei(cae.component->source, AL_BUFFER, buf); + + std::cout << "Loaded audio: " << cae.component->fileName << std::endl; + } } void AudioSystem::receive(const entityx::ComponentRemovedEvent<Audio>& cae) { + alDeleteBuffers(1, &cae.component->buffer); alDeleteSources(1, &cae.component->source); } +void AudioSystem::playSound(const Position& pos, const Audio& audio) +{ + alSource3f(audio.source, AL_POSITION, pos.x, pos.y, 0); + alSourcePlay(audio.source); +} + diff --git a/src/audio.hpp b/src/audio.hpp index 97fff99..7a2076a 100644 --- a/src/audio.hpp +++ b/src/audio.hpp @@ -24,6 +24,7 @@ #include <entityx/entityx.h> #include <components/Audio.hpp> +#include <components/Position.hpp> class AudioSystem : public entityx::System<AudioSystem>, public entityx::Receiver<AudioSystem> @@ -51,6 +52,8 @@ public: void receive(const entityx::ComponentAddedEvent<Audio>& cae); void receive(const entityx::ComponentRemovedEvent<Audio>& cae); + + void playSound(const Position& pos, const Audio& audio); }; #endif // SYSTEM_AUDIO_HPP_ diff --git a/src/components/Audio.hpp b/src/components/Audio.hpp index 3f7296d..a97b235 100644 --- a/src/components/Audio.hpp +++ b/src/components/Audio.hpp @@ -24,14 +24,23 @@ struct Audio : Component<Audio> { public: + std::string fileName; ALuint source; + ALuint buffer; - Audio(ALuint _source = 0) : - source(_source) {} + Audio(std::string _fileName = "") : + fileName(_fileName), source(0), buffer(0) {} - Audio FromLua([[maybe_unused]] sol::object ref) + Audio FromLua(sol::object ref) { - // TODO load from file name? + if (ref.get_type() == sol::type::table) { + sol::table tab = ref; + if (tab["file"] != nullptr) + this->fileName = tab["file"]; + } else { + throw std::string("Audio table not formatted properly"); + } + return *this; } diff --git a/src/engine.cpp b/src/engine.cpp index b1d9a56..3fc4fdd 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -19,6 +19,7 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +#include "audio.hpp" #include "config.hpp" #include "engine.hpp" #include "gamestate.hpp" @@ -54,6 +55,7 @@ int Engine::init(void) systems.add<ScriptSystem>(entities, *(systems.system<WorldSystem>().get())); systems.add<PhysicsSystem>(); systems.add<TextSystem>(); + systems.add<AudioSystem>(); systems.configure(); // Load game script and entity data diff --git a/src/script.cpp b/src/script.cpp index 6cda627..20011b4 100644 --- a/src/script.cpp +++ b/src/script.cpp @@ -75,6 +75,7 @@ void ScriptSystem::doFile(void) /******************** * SCRIPT PARSING * ********************/ +#include <components/Audio.hpp> #include <components/EventListener.hpp> #include <components/Position.hpp> #include <components/Player.hpp> @@ -127,6 +128,10 @@ void ScriptSystem::scriptExport(void) sol::constructors<Physics(void), Physics()>(), "standing", &Physics::standing); + lua.new_usertype<Audio>("Audio", + sol::constructors<Audio(std::string)>(), + "file", &Audio::fileName); + lua.new_usertype<World>("World", sol::constructors<World(sol::object), World(void)>(), "Generate", &World::generate, @@ -173,6 +178,11 @@ sol::table ScriptSystem::spawn(sol::object param) e.assign<Name>(Name().FromLua(tab["Name"])).get(); } + if (tab["Audio"] != nullptr) { + (*toRet)["Audio"] = + e.assign<Audio>(Audio().FromLua(tab["Audio"])).get(); + } + if (tab["Render"] != nullptr) { if (!e.has_component<Position>()) // Position must exist for render (*toRet)["Position"] = e.assign<Position>().get(); |