From 5291b0833001fa80909dd67237c2e206e2f33ed1 Mon Sep 17 00:00:00 2001 From: cat Date: Sun, 21 Sep 2025 18:55:41 +0300 Subject: [PATCH] Added magic_enum as a dependency. Tweaked ErrorHandling --- .gitmodules | 3 +++ CMakeLists.txt | 2 ++ lib/magic_enum | 1 + src/ErrorHandling.cpp | 7 +++++-- src/Sound.cpp | 10 +++++++--- 5 files changed, 18 insertions(+), 5 deletions(-) create mode 160000 lib/magic_enum diff --git a/.gitmodules b/.gitmodules index 30f525f..89c1f0f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "lib/miniaudio"] path = lib/miniaudio url = https://github.com/mackron/miniaudio +[submodule "lib/magic_enum"] + path = lib/magic_enum + url = https://github.com/Neargye/magic_enum diff --git a/CMakeLists.txt b/CMakeLists.txt index cfe2a88..ff8d2a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,9 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-subobject-linkage") if(CMAKE_BUILD_TYPE STREQUAL "Debug") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address") endif() + add_subdirectory(lib/miniaudio) +add_subdirectory(lib/magic_enum) find_package(Corrade REQUIRED Main) find_package(Magnum REQUIRED) diff --git a/lib/magic_enum b/lib/magic_enum new file mode 160000 index 0000000..ec5734e --- /dev/null +++ b/lib/magic_enum @@ -0,0 +1 @@ +Subproject commit ec5734e491ab7cb443c6175996d1791854341a55 diff --git a/src/ErrorHandling.cpp b/src/ErrorHandling.cpp index 3d12d37..88baf42 100644 --- a/src/ErrorHandling.cpp +++ b/src/ErrorHandling.cpp @@ -1,4 +1,6 @@ #include "ChargeAudio.hpp" +#include "magic_enum/include/magic_enum/magic_enum.hpp" +#include #include void ChargeAudio::ThrowOnRuntimeError(std::string message, @@ -7,6 +9,7 @@ void ChargeAudio::ThrowOnRuntimeError(std::string message, return; } - Utility::Error{} << message << " (" << errorType << ")"; - throw new std::runtime_error(message + ". Check STDERR for more info.\n"); + Utility::Error{} << message.c_str() + << magic_enum::enum_name(errorType).data(); + throw new std::runtime_error(message + " Check STDERR for more info.\n"); } diff --git a/src/Sound.cpp b/src/Sound.cpp index 95c7b24..cc640a7 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -9,7 +9,9 @@ Sound::Sound(Engine *engine, std::function setupFunction, maConfig = ma_sound_config_init_2(&baseEngine->maEngine); maConfig.endCallback = Sound::onSoundFinish; maConfig.pEndCallbackUserData = this; + setupFunction(this); + ma_result maResponse = ma_sound_init_ex(&baseEngine->maEngine, &maConfig, &maSound); ThrowOnRuntimeError("Failed to create a new sound. " + additionalErrorMessage, @@ -63,18 +65,20 @@ bool Sound::SetPlaybackTime(float time) { // Controls void Sound::Play() { - ma_sound_start(&maSound); + ThrowOnRuntimeError("Failed to start the sound.", ma_sound_start(&maSound)); state = Sound::SoundState::Playing; } void Sound::Pause() { - ma_sound_stop(&maSound); + ThrowOnRuntimeError("Failed to pause the sound.", ma_sound_stop(&maSound)); state = Sound::SoundState::Paused; } void Sound::Reset() { if (type == SoundType::StreamedRawPCM) { - ThrowOnRuntimeError("You cannot reset on Streamed RawPCM sounds!"); + ThrowOnRuntimeError( + "You cannot reset on Streamed RawPCM sounds! Since the buffer is a " + "ring buffer there isn't a \"start\" to return to."); } ma_sound_seek_to_pcm_frame(&maSound, 0);