Skip to content

Commit

Permalink
Merge pull request #74 from YGNI-RType/dev
Browse files Browse the repository at this point in the history
Release v0.4.1
  • Loading branch information
Nfire2103 authored Nov 5, 2024
2 parents fc49e0d + 24eb8de commit 83fcc6a
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 131 deletions.
21 changes: 21 additions & 0 deletions include/GEngine/GEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class GEngine {
* It also registers the necessary game engine elements by calling the registerElements method.
*/
static void init(int size, const char **argv) {
getArgsSize(size);
getArgs(argv);
std::call_once(initFlag, [size, argv]() {
instance.reset(new GEngine());
instance->getLocal().setParams(argv, size);
Expand Down Expand Up @@ -172,6 +174,22 @@ class GEngine {
return instance->m_remote;
}

static size_t getArgsSize(size_t argsSize) {
static size_t _argsSize = 0;

if (argsSize)
_argsSize = argsSize;
return _argsSize;
}

static const char **getArgs(const char **args) {
static const char **_args = NULL;

if (args)
_args = args;
return _args;
}

private:
/**
* @brief Private constructor to prevent direct instantiation.
Expand Down Expand Up @@ -213,3 +231,6 @@ class GEngine {
// Initialize static members
// std::unique_ptr<GEngine> GEngine::instance = nullptr;
// std::once_flag GEngine::initFlag;

#define GENGINE_ARGS_SIZE GEngine::getArgsSize(0)
#define GENGINE_ARGS GEngine::getArgs(NULL)
4 changes: 2 additions & 2 deletions include/GEngine/libdev/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ using MotionAcceleration3D = gengine::system::MotionAcceleration3D;
#include "GEngine/libdev/systems/driver/input/KeyboardCatcher.hpp"
#include "GEngine/libdev/systems/driver/input/MouseCatcher.hpp"
#include "GEngine/libdev/systems/driver/output/Animate.hpp"
#include "GEngine/libdev/systems/driver/output/AudioManager.hpp"
#include "GEngine/libdev/systems/driver/output/Draw.hpp"
#include "GEngine/libdev/systems/driver/output/FontManager.hpp"
#include "GEngine/libdev/systems/driver/output/RenderWindow.hpp"
#include "GEngine/libdev/systems/driver/output/SoundManager.hpp"
#include "GEngine/libdev/systems/driver/output/TextureManager.hpp"

namespace geg::system::io {
Expand All @@ -60,7 +60,7 @@ using FontManager = gengine::system::driver::output::FontManager;

using RenderWindow = gengine::system::driver::output::RenderWindow;

using SoundManager = gengine::system::driver::output::SoundManager;
using AudioManager = gengine::system::driver::output::AudioManager;

using TextureManager = gengine::system::driver::output::TextureManager;
}; // namespace geg::system::io
12 changes: 11 additions & 1 deletion include/GEngine/libdev/components/driver/output/Sound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@
namespace gengine::component::driver::output {
struct Sound : public gengine::Component<Sound> {
std::uint64_t soundId;
std::uint64_t _ack = 0;

Sound(std::uint64_t soundId)
: soundId(soundId) {
}
};

struct Music : public gengine::Component<Music> {
bool pause = false;
std::uint64_t musicId;

Music(std::uint64_t musicId)
: musicId(musicId) {
}

bool operator==(const Music &) const = default;
};
} // namespace gengine::component::driver::output
3 changes: 3 additions & 0 deletions include/GEngine/libdev/systems/MainLoop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <chrono>

#include "GEngine/libdev/System.hpp"
#include "GEngine/libdev/systems/events/CLI.hpp"
#include "GEngine/libdev/systems/events/MainLoop.hpp"
#include "GEngine/libdev/systems/events/Native.hpp"

Expand All @@ -35,6 +36,8 @@ class AutoMainLoop : public gengine::System<AutoMainLoop> {

void onStopMainLoop(gengine::system::event::StopMainLoop &e);

void onExit(gengine::system::event::CLINewInput &e);

std::chrono::microseconds getElapsedTime(void);

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
** ════════════════════════════════════════════════════════════════════════════
** GEngine (libdev) System
** ════════════════════════════════════════════════════════════════════════════
** File : SoundManager.hpp
** File : AudioManager.hpp
** Create at : 2024-10-15 05:02
** Author : AUTHOR
** Description : This system dedicated to ther DriverEngine, it must with
Expand Down Expand Up @@ -32,10 +32,12 @@

namespace gengine::system::driver::output {

class SoundManager : public gengine::System<SoundManager, gengine::component::driver::output::Sound,
gengine::interface::component::RemoteLocal> {
class AudioManager
: public gengine::System<AudioManager, gengine::component::driver::output::Sound,
gengine::component::driver::output::Music, gengine::interface::component::RemoteLocal,
geg::component::network::NetSend> {
public:
SoundManager(const std::string &folder);
AudioManager(const std::string &soundFolder, const std::string &musicFolder);
virtual void init(void) override {
}

Expand All @@ -51,27 +53,58 @@ class SoundManager : public gengine::System<SoundManager, gengine::component::dr

void onMainLoop(geg::event::MainLoop &e);
void onSoundPlayed(gengine::interface::event::SharedEvent<gengine::system::event::driver::output::SoundPlayed> &e);
void onMusic(gengine::system::event::driver::output::Music &e) {
auto &musics = getComponents<gengine::component::driver::output::Music>();
auto &netSends = getComponents<geg::component::network::NetSend>();

if (!musics.size())
spawnEntity(gengine::component::driver::output::Music(getMusicIdByPath(e.path)),
geg::component::network::NetSend());
else {
getMusicComponent().musicId = getMusicIdByPath(e.path);
for (auto [e, _unused, netSend] : gengine::Zip(musics, netSends)) {
netSend.update();
break;
}
}
std::cout << "music: " << getMusicComponent().musicId << std::endl;
}

protected:
const std::string m_soundFolder;
const std::string m_musicFolder;
std::string m_folder;
std::unordered_map<std::string, std::pair<std::uint64_t, Sound>> m_soundTable;
std::uint64_t m_baseId = 0;
std::unordered_map<std::string, std::pair<std::uint64_t, Music>> m_musicTable;
std::uint64_t m_soundBaseId = 0;
std::uint64_t m_musicBaseId = 1;
std::uint64_t m_currentMusicId = 0;

std::uint64_t getIdByPath(const std::string &path) const;
Music getMusicById(std::uint64_t id);
std::uint64_t getMusicIdByPath(const std::string &path) {
auto it = m_musicTable.find(path);

if (it == m_musicTable.end())
THROW_WARNING("Music not found");

return it->second.first;
}
void playSoundById(std::uint64_t id);

gengine::component::driver::output::Music &getMusicComponent(void);
};

class SoundManagerLocal : public SoundManager, public gengine::LocalSystem {
class AudioManagerLocal : public AudioManager, public gengine::LocalSystem {
public:
SoundManagerLocal(const std::string &folder);
AudioManagerLocal(const std::string &soundFolder, const std::string &musicFolder);

void init(void) override;
};

class SoundManagerRemote : public SoundManager, public gengine::RemoteSystem {
class AudioManagerRemote : public AudioManager, public gengine::RemoteSystem {
public:
SoundManagerRemote(const std::string &folder);
AudioManagerRemote(const std::string &soundFolder, const std::string &musicFolder);

void init(void) override;
};
Expand Down
10 changes: 9 additions & 1 deletion include/GEngine/libdev/systems/events/driver/output/Sound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
** File : Sound.hpp
** Create at : 2024-10-15 05:14
** Author : AUTHOR
** Description : This event is listened to natively by the SoundManager system
** Description : This event is listened to natively by the AudioManager system
which will launch the sound linked to the path passed as an
argument when it is triggered by this event.
** ════════════════════════════════════════════════════════════════════════════
Expand Down Expand Up @@ -38,4 +38,12 @@ struct SoundPlayed : public Event {
: soundId(soundId) {
}
};

struct Music : public Event {
Music(const std::string &path)
: path(path) {
}

const std::string path;
};
} // namespace gengine::system::event::driver::output
6 changes: 6 additions & 0 deletions source/GEngine/libdev/systems/MainLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ void AutoMainLoop::init(void) {
subscribeToEvent<gengine::system::event::StartEngine>(&AutoMainLoop::onStartEngine);
subscribeToEvent<gengine::system::event::MainLoop>(&AutoMainLoop::onMainLoop);
subscribeToEvent<gengine::system::event::StopMainLoop>(&AutoMainLoop::onStopMainLoop);
subscribeToEvent<gengine::system::event::CLINewInput>(&AutoMainLoop::onExit);
}

void AutoMainLoop::onExit(gengine::system::event::CLINewInput &e) {
if (e.prompt.size() && !e.prompt[0].compare("exit"))
m_isRunning = false;
}

void AutoMainLoop::onStartEngine(gengine::system::event::StartEngine &e) {
Expand Down
159 changes: 159 additions & 0 deletions source/GEngine/libdev/systems/driver/output/AudioManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
** EPITECH PROJECT, 2024
** GameEngine
** File description:
** AudioManager.cpp
*/

#include "GEngine/libdev/systems/driver/output/AudioManager.hpp"

namespace gengine::system::driver::output {

AudioManager::AudioManager(const std::string &soundFolder, const std::string &musicFolder)
: m_soundFolder(soundFolder)
, m_musicFolder(musicFolder) {
}

void AudioManager::onStartEngine(gengine::system::event::StartEngine &e) {
SetTraceLogLevel(LOG_WARNING);
InitAudioDevice();
for (const auto &entry : std::filesystem::recursive_directory_iterator(m_soundFolder)) {
if (entry.is_regular_file()) {
std::string filePath = entry.path().string();

Sound sound = LoadSound(filePath.c_str());
std::string path = std::filesystem::relative(entry.path(), m_soundFolder).string();
m_soundTable.insert({path, {m_soundBaseId++, sound}});
}
}
for (const auto &entry : std::filesystem::recursive_directory_iterator(m_musicFolder)) {
if (entry.is_regular_file()) {
std::string filePath = entry.path().string();

Music music = LoadMusicStream(filePath.c_str());
std::string path = std::filesystem::relative(entry.path(), m_musicFolder).string();
m_musicTable.insert({path, {m_musicBaseId++, music}});
}
}
}

Music AudioManager::getMusicById(std::uint64_t id) {
for (auto &[_, pair] : m_musicTable)
if (pair.first == id)
return pair.second;
THROW_WARNING("Music component not initilized");
}

void AudioManager::onStopEngine(gengine::system::event::StopEngine &e) {
for (auto &[path, sound] : m_soundTable)
UnloadSound(sound.second);
}

const Sound &AudioManager::get(const std::string &path) {
const auto &sound = m_soundTable.find(path);
if (sound == m_soundTable.cend())
THROW_ERROR("Out of range: This sound does not exist. PATH: " + path);

return sound->second.second;
}

void AudioManager::onMainLoop(geg::event::MainLoop &e) {
auto &sounds = getComponents<gengine::component::driver::output::Sound>();
auto &musics = getComponents<gengine::component::driver::output::Music>();

if (musics.size()) {
auto &music = getMusicComponent();
if (music.musicId != m_currentMusicId) {
if (m_currentMusicId)
StopMusicStream(getMusicById(m_currentMusicId));
m_currentMusicId = music.musicId;
PlayMusicStream(getMusicById(m_currentMusicId));
}
}

if (m_currentMusicId)
UpdateMusicStream(getMusicById(m_currentMusicId));

static std::set<gengine::Entity> m_soundsPlayed;

for (auto &[e, sound] : sounds) {
if (m_soundsPlayed.find(e) == m_soundsPlayed.end()) {
m_soundsPlayed.insert(e);
playSoundById(sound.soundId);
}
publishEvent(gengine::system::event::driver::output::SoundPlayed(sound.soundId));
}
for (auto it = m_soundsPlayed.begin(); it != m_soundsPlayed.end();)
if (!sounds.contains(*it))
it = m_soundsPlayed.erase(it);
else
++it;
}

gengine::component::driver::output::Music &AudioManager::getMusicComponent(void) {
auto &musics = getComponents<gengine::component::driver::output::Music>();

if (!musics.size())
THROW_WARNING("Music component not initilazed");
for (auto &[_, m] : musics)
return m;
}

void AudioManager::onSoundPlayed(
gengine::interface::event::SharedEvent<gengine::system::event::driver::output::SoundPlayed> &e) {
static std::unordered_map<std::uint64_t, std::set<uuids::uuid>> m_soundsAck;
auto &remoteLocals = getComponents<gengine::interface::component::RemoteLocal>();
auto &sounds = getComponents<gengine::component::driver::output::Sound>();

if (m_soundsAck.find(e->soundId) == m_soundsAck.end())
m_soundsAck.insert({e->soundId, std::set<uuids::uuid>()});
m_soundsAck[e->soundId].insert(e.remoteUUID);

for (auto &[entity, s] : sounds) {
if (s.soundId == e->soundId && m_soundsAck[e->soundId].size() == remoteLocals.size())
killEntity(entity);
m_soundsAck.erase(e->soundId);
}
}

std::uint64_t AudioManager::getIdByPath(const std::string &path) const {
auto it = m_soundTable.find(path);

if (it == m_soundTable.end())
THROW_WARNING("Sound not found");

return it->second.first;
}

void AudioManager::playSoundById(std::uint64_t id) {
for (auto &[_, pair] : m_soundTable) {
if (pair.first == id) {
PlaySound(pair.second);
return;
}
}
}

AudioManagerLocal::AudioManagerLocal(const std::string &soundFolder, const std::string &musicFolder)
: AudioManager(soundFolder, musicFolder) {
}

void AudioManagerLocal::init(void) {
subscribeToEvent<gengine::system::event::StartEngine>(&AudioManager::onStartEngine);
subscribeToEvent<gengine::system::event::StopEngine>(&AudioManager::onStopEngine);
subscribeToEvent<geg::event::MainLoop>(&AudioManager::onMainLoop);
}

AudioManagerRemote::AudioManagerRemote(const std::string &soundFolder, const std::string &musicFolder)
: AudioManager(soundFolder, musicFolder) {
}

void AudioManagerRemote::init(void) {
subscribeToEvent<gengine::system::event::StartEngine>(&AudioManager::onStartEngine);
subscribeToEvent<gengine::system::event::StopEngine>(&AudioManager::onStopEngine);
subscribeToEvent<gengine::system::event::driver::output::Sound>(&AudioManager::onSound);
subscribeToEvent<gengine::interface::event::SharedEvent<gengine::system::event::driver::output::SoundPlayed>>(
&AudioManager::onSoundPlayed);
subscribeToEvent<gengine::system::event::driver::output::Music>(&AudioManager::onMusic);
}
} // namespace gengine::system::driver::output
Loading

0 comments on commit 83fcc6a

Please sign in to comment.