Changing initing to be more standardised(needs to be refactored)

This commit is contained in:
2025-09-07 22:39:17 +03:00
parent 71e4ff9317
commit 146bd49b36
3 changed files with 27 additions and 14 deletions

View File

@@ -28,11 +28,14 @@ public:
private: private:
Sound(class Engine *engine); Sound(class Engine *engine);
void init(std::string additionalErrorMessage = "");
static void onSoundFinish(void *customData, ma_sound *); static void onSoundFinish(void *customData, ma_sound *);
class Engine *baseEngine; class Engine *baseEngine;
ma_sound maSound; ma_sound maSound;
ma_sound_config maConfig;
SoundState state = SoundState::Idle; SoundState state = SoundState::Idle;
friend class Engine; friend class Engine;
}; };

View File

@@ -20,20 +20,13 @@ Engine::Engine() {
Engine::~Engine() { ma_engine_uninit(&maEngine); } Engine::~Engine() { ma_engine_uninit(&maEngine); }
Containers::Pointer<Sound> Engine::CreateSound(std::string filepath) { Containers::Pointer<Sound> Engine::CreateSound(const std::string filepath) {
auto sound = Containers::Pointer<Sound>(new Sound(this)); auto sound = Containers::Pointer<Sound>(new Sound(this));
maResponse = ma_sound_init_from_file(&maEngine, filepath.c_str(), 0, NULL, sound->maConfig.pFilePath = filepath.c_str();
NULL, &sound->maSound); sound->maConfig.flags = 0;
sound->maSound.endCallback = Sound::onSoundFinish; sound->maConfig.pInitialAttachment = NULL;
sound->maSound.pEndCallbackUserData = sound.get(); sound->maConfig.pDoneFence = NULL;
sound->init("Failed to create the sound from the file: " + filepath);
if (maResponse != MA_SUCCESS) {
Utility::Error{} << "Failed to create the sound from the file: "
<< filepath.c_str() << " (" << maResponse << ")";
throw new std::runtime_error(
"Failed to create the sound from file. Check STDERR for more info.");
}
return sound; return sound;
} }

View File

@@ -1,14 +1,31 @@
#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 <stdexcept>
using namespace ChargeAudio; using namespace ChargeAudio;
Sound::Sound(Engine *engine) : baseEngine(engine) {} Sound::Sound(Engine *engine) : baseEngine(engine) {
maConfig = ma_sound_config_init_2(&baseEngine->maEngine);
maConfig.endCallback = Sound::onSoundFinish;
maConfig.pEndCallbackUserData = this;
}
Sound::~Sound() { ma_sound_uninit(&maSound); } Sound::~Sound() { ma_sound_uninit(&maSound); }
Sound::SoundState Sound::GetState() { return state; } Sound::SoundState Sound::GetState() { return state; }
void Sound::init(std::string additionalErrorMessage) {
ma_result maResponse =
ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound);
if (maResponse != MA_SUCCESS) {
Utility::Error{} << "Failed to create a new sound" << " (" << maResponse
<< ")";
throw new std::runtime_error(
"Failed to create a new sound. Check STDERR for more info.\n" +
additionalErrorMessage);
}
}
// Controls // Controls
void Sound::Play() { void Sound::Play() {
ma_sound_start(&maSound); ma_sound_start(&maSound);