},
Physics = 0,
Name = "bord",
+ Audio = {
+ file = "Assets/jump.wav"
+ },
hellotrue = true,
hellofalse = false,
Render = {
#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();
}
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,
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);
+}
+
#include <entityx/entityx.h>
#include <components/Audio.hpp>
+#include <components/Position.hpp>
class AudioSystem : public entityx::System<AudioSystem>,
public entityx::Receiver<AudioSystem>
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_
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;
}
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+#include "audio.hpp"
#include "config.hpp"
#include "engine.hpp"
#include "gamestate.hpp"
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
/********************
* SCRIPT PARSING *
********************/
+#include <components/Audio.hpp>
#include <components/EventListener.hpp>
#include <components/Position.hpp>
#include <components/Player.hpp>
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,
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();