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

View File

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

View File

@@ -1,20 +1,18 @@
#include "ChargeAudio.hpp"
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector3.h>
#include <functional>
#include <stdexcept>
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.endCallback = Sound::onSoundFinish;
maConfig.pEndCallbackUserData = this;
}
Sound::~Sound() { ma_sound_uninit(&maSound); }
Sound::SoundState Sound::GetState() { return state; }
void Sound::init(std::string additionalErrorMessage) {
setupFunction(this);
ma_result maResponse =
ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound);
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
void Sound::Play() {
ma_sound_start(&maSound);