aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2019-09-30 15:29:49 -0400
committerClyne Sullivan <clyne@bitgloo.com>2019-09-30 15:29:49 -0400
commitbebc6e955114a0907c43df9a9c00f22b1f743446 (patch)
tree5a31e0f31727d69a978fcce9abd12551cdce4cea
parent7919f7f69b0abd54a6df92f34e1392b84dae3669 (diff)
created Audio component
-rw-r--r--src/audio.cpp18
-rw-r--r--src/audio.hpp9
-rw-r--r--src/components/Audio.hpp47
3 files changed, 70 insertions, 4 deletions
diff --git a/src/audio.cpp b/src/audio.cpp
index 7dec4c3..975cfce 100644
--- a/src/audio.cpp
+++ b/src/audio.cpp
@@ -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);
+}
+
diff --git a/src/audio.hpp b/src/audio.hpp
index 4d617c7..97fff99 100644
--- a/src/audio.hpp
+++ b/src/audio.hpp
@@ -21,10 +21,12 @@
#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
index 0000000..3f7296d
--- /dev/null
+++ b/src/components/Audio.hpp
@@ -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_
+