]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Audio component loading, getting ready to play
authorClyne Sullivan <clyne@bitgloo.com>
Wed, 2 Oct 2019 00:50:28 +0000 (20:50 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Wed, 2 Oct 2019 00:50:28 +0000 (20:50 -0400)
Assets/jump.wav [new file with mode: 0644]
Scripts/init.lua
src/audio.cpp
src/audio.hpp
src/components/Audio.hpp
src/engine.cpp
src/script.cpp

diff --git a/Assets/jump.wav b/Assets/jump.wav
new file mode 100644 (file)
index 0000000..1cd1681
Binary files /dev/null and b/Assets/jump.wav differ
index ec9e35071460ba81a74c5432e03c9289711387f8..ea5a833b20cdab49b0b31854bd8a799af249d613 100644 (file)
@@ -37,6 +37,9 @@ player = {
     },
     Physics = 0,
     Name = "bord",
+    Audio = {
+        file = "Assets/jump.wav"
+    },
     hellotrue = true,
     hellofalse = false,
     Render = {
index 975cfce104ee321ea8be76bc98f86d1eaae4486f..8bb14131d6767625f82f221938f30dab66048920 100644 (file)
 
 #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);
+}
+
index 97fff99a9ac473b235ff8e74f174e6bd5a8a7cd6..7a2076ab7eba06628265c38b5c3ffcb1f4dff198 100644 (file)
@@ -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_
index 3f7296d7b887d258bcf8349c994f8fa26f307b36..a97b2352ba371dd46e27d6515ae5fc63a2fddfe8 100644 (file)
 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;
     }
 
index b1d9a562f20552676ca8aa5f0085e966c1e45acf..3fc4fdd74a343b33e86693b0a49f341cce395fd6 100644 (file)
@@ -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
index 6cda627d2d8f41418ef1ea2c17c275bc8f068040..20011b48d49c93c1be7f4c84f7df2e73e080d7bb 100644 (file)
@@ -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();