Making sound constructor more generalised(probably will make it public soon)

This commit is contained in:
2025-09-08 00:37:10 +03:00
parent 146bd49b36
commit 13a21387df
3 changed files with 23 additions and 19 deletions

View File

@@ -3,6 +3,7 @@
#include <Corrade/Containers/Containers.h> #include <Corrade/Containers/Containers.h>
#include <Magnum/Magnum.h> #include <Magnum/Magnum.h>
#include <Magnum/Math/Vector.h> #include <Magnum/Math/Vector.h>
#include <functional>
#include <string> #include <string>
namespace ChargeAudio { namespace ChargeAudio {
@@ -27,8 +28,8 @@ public:
float GetVolume(); float GetVolume();
private: private:
Sound(class Engine *engine); Sound(class Engine *engine, std::function<void(Sound *)> setupFunction,
void init(std::string additionalErrorMessage = ""); std::string additionalErrorMessage = "");
static void onSoundFinish(void *customData, ma_sound *); static void onSoundFinish(void *customData, ma_sound *);
class Engine *baseEngine; class Engine *baseEngine;
@@ -61,7 +62,8 @@ public:
~Engine(); ~Engine();
// Creating tools // Creating tools
Containers::Pointer<Sound> CreateSound(std::string filepath); Containers::Pointer<Sound> CreateSound(std::string filepath,
bool streamFile = false);
Containers::Pointer<Listener> CreateListener(); Containers::Pointer<Listener> CreateListener();
void SetVolume(float value); void SetVolume(float value);

View File

@@ -20,14 +20,15 @@ Engine::Engine() {
Engine::~Engine() { ma_engine_uninit(&maEngine); } Engine::~Engine() { ma_engine_uninit(&maEngine); }
Containers::Pointer<Sound> Engine::CreateSound(const std::string filepath) { Containers::Pointer<Sound> Engine::CreateSound(const std::string filepath,
auto sound = Containers::Pointer<Sound>(new Sound(this)); bool streamFile) {
return Containers::Pointer<Sound>(new Sound(
this,
[filepath, streamFile](Sound *sound) {
sound->maConfig.pFilePath = filepath.c_str(); sound->maConfig.pFilePath = filepath.c_str();
sound->maConfig.flags = 0; sound->maConfig.flags = (streamFile ? MA_SOUND_FLAG_STREAM : 0);
sound->maConfig.pInitialAttachment = NULL; },
sound->maConfig.pDoneFence = NULL; "Failed to create the sound from the file: " + filepath));
sound->init("Failed to create the sound from the file: " + filepath);
return sound;
} }
Containers::Pointer<Listener> Engine::CreateListener() { Containers::Pointer<Listener> Engine::CreateListener() {

View File

@@ -1,20 +1,18 @@
#include "ChargeAudio.hpp" #include "ChargeAudio.hpp"
#include <Magnum/Magnum.h> #include <Magnum/Magnum.h>
#include <Magnum/Math/Vector3.h> #include <Magnum/Math/Vector3.h>
#include <functional>
#include <stdexcept> #include <stdexcept>
using namespace ChargeAudio; using namespace ChargeAudio;
Sound::Sound(Engine *engine) : baseEngine(engine) { Sound::Sound(Engine *engine, std::function<void(Sound *)> setupFunction,
std::string additionalErrorMessage)
: baseEngine(engine) {
maConfig = ma_sound_config_init_2(&baseEngine->maEngine); maConfig = ma_sound_config_init_2(&baseEngine->maEngine);
maConfig.endCallback = Sound::onSoundFinish; maConfig.endCallback = Sound::onSoundFinish;
maConfig.pEndCallbackUserData = this; maConfig.pEndCallbackUserData = this;
} setupFunction(this);
Sound::~Sound() { ma_sound_uninit(&maSound); }
Sound::SoundState Sound::GetState() { return state; }
void Sound::init(std::string additionalErrorMessage) {
ma_result maResponse = ma_result maResponse =
ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound); ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound);
if (maResponse != MA_SUCCESS) { if (maResponse != MA_SUCCESS) {
@@ -26,6 +24,9 @@ void Sound::init(std::string additionalErrorMessage) {
} }
} }
Sound::~Sound() { ma_sound_uninit(&maSound); }
Sound::SoundState Sound::GetState() { return state; }
// Controls // Controls
void Sound::Play() { void Sound::Play() {
ma_sound_start(&maSound); ma_sound_start(&maSound);