]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
created Audio component
authorClyne Sullivan <clyne@bitgloo.com>
Mon, 30 Sep 2019 19:29:49 +0000 (15:29 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Mon, 30 Sep 2019 19:29:49 +0000 (15:29 -0400)
src/audio.cpp
src/audio.hpp
src/components/Audio.hpp [new file with mode: 0644]

index 7dec4c3a509a0fa9a0ffdca91afb62f4a87764e2..975cfce104ee321ea8be76bc98f86d1eaae4486f 100644 (file)
@@ -27,12 +27,16 @@ AudioSystem::AudioSystem(void) :
 AudioSystem::~AudioSystem(void)
 {
     // Delete context before device
-    context.get_deleter()(context.get());
+    context.reset();
+    device.reset();
 }
 
 void AudioSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
-                            [[maybe_unused]] entityx::EventManager& events)
+                            entityx::EventManager& events)
 {
+    events.subscribe<entityx::ComponentAddedEvent<Audio>>(*this);
+    events.subscribe<entityx::ComponentRemovedEvent<Audio>>(*this);
+
     // Access device
     device.reset(alcOpenDevice(nullptr));
     if (!device)
@@ -49,3 +53,13 @@ void AudioSystem::update([[maybe_unused]] entityx::EntityManager& entities,
                          [[maybe_unused]] entityx::TimeDelta dt)
 {}
 
+void AudioSystem::receive(const entityx::ComponentAddedEvent<Audio>& cae)
+{
+    alGenSources(1, const_cast<ALuint*>(&cae.component->source));
+}
+
+void AudioSystem::receive(const entityx::ComponentRemovedEvent<Audio>& cae)
+{
+    alDeleteSources(1, &cae.component->source);
+}
+
index 4d617c7d20eb07dbf822292337328bf6156d3c6a..97fff99a9ac473b235ff8e74f174e6bd5a8a7cd6 100644 (file)
 #define SYSTEM_AUDIO_HPP_
 
 #include <AL/alut.h>
-
 #include <entityx/entityx.h>
 
-class AudioSystem : public entityx::System<AudioSystem>
+#include <components/Audio.hpp>
+
+class AudioSystem : public entityx::System<AudioSystem>,
+                    public entityx::Receiver<AudioSystem>
 {
 private:
     std::unique_ptr<ALCdevice, void (*)(ALCdevice *)> device;
@@ -46,6 +48,9 @@ public:
     void update(entityx::EntityManager& entities,
                 entityx::EventManager& events,
                 entityx::TimeDelta dt) final;
+
+    void receive(const entityx::ComponentAddedEvent<Audio>& cae);
+    void receive(const entityx::ComponentRemovedEvent<Audio>& cae);
 };
 
 #endif // SYSTEM_AUDIO_HPP_
diff --git a/src/components/Audio.hpp b/src/components/Audio.hpp
new file mode 100644 (file)
index 0000000..3f7296d
--- /dev/null
@@ -0,0 +1,47 @@
+/**
+ * Copyright (C) 2019 Clyne Sullivan
+ *
+ * This program 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.
+ *
+ * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef COMPONENT_AUDIO_HPP_
+#define COMPONENT_AUDIO_HPP_
+
+#include <AL/al.h>
+
+#include "Component.hpp"
+
+struct Audio : Component<Audio>
+{
+public:
+    ALuint source;
+
+    Audio(ALuint _source = 0) :
+        source(_source) {}
+
+    Audio FromLua([[maybe_unused]] sol::object ref)
+    {
+        // TODO load from file name?
+        return *this;
+    }
+
+    void serialize([[maybe_unused]] cereal::JSONOutputArchive& ar) final {}
+    void serialize([[maybe_unused]] cereal::JSONInputArchive& ar) final {}
+
+    std::string serializeName(void) const final {
+        return "Audio";
+    }
+};
+
+#endif // COMPONENT_AUDIO_HPP_
+