From 52df67da095ef6d0cbb9a12119332674902eeeda Mon Sep 17 00:00:00 2001 From: DcruBro Date: Fri, 13 Mar 2026 14:28:08 +0100 Subject: [PATCH] Audio Abstrakcija - WIP --- CMakeLists.txt | 10 ++- include/audio/audio.hpp | 21 +++++ include/game/agame/player.hpp | 4 + include/object/sound.hpp | 37 +++++++++ include/renderer/texture.hpp | 3 + include/utils.hpp | 6 ++ include/window/window.hpp | 1 + resources/example.wav | Bin 0 -> 1048558 bytes src/audio/audio.cpp | 37 +++++++++ src/game/agame/player.cpp | 20 ++--- src/object/sound.cpp | 150 ++++++++++++++++++++++++++++++++++ src/renderer/texture.cpp | 76 ++++++++++------- src/window/window.cpp | 2 + 13 files changed, 324 insertions(+), 43 deletions(-) create mode 100644 include/audio/audio.hpp create mode 100644 include/object/sound.hpp create mode 100644 resources/example.wav create mode 100644 src/audio/audio.cpp create mode 100644 src/object/sound.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index af136d6..2181d88 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,13 @@ FetchContent_Declare( GIT_TAG release-3.2.2 ) +# Download SDL3_mixer from source +FetchContent_Declare( + SDL3_mixer + GIT_REPOSITORY https://github.com/libsdl-org/SDL_mixer.git + GIT_TAG release-3.2.0 +) + # Work around PipeWire API mismatch on some Linux distributions. # Disable PipeWire backend in SDL; PulseAudio/ALSA backends remain available. set(SDL_PIPEWIRE OFF CACHE BOOL "Disable SDL PipeWire backend" FORCE) @@ -37,6 +44,7 @@ set(SDL_PIPEWIRE_SHARED OFF CACHE BOOL "Disable dynamic PipeWire loading in SDL" FetchContent_MakeAvailable(SDL3) FetchContent_MakeAvailable(SDL3_image) FetchContent_MakeAvailable(SDL3_ttf) +FetchContent_MakeAvailable(SDL3_mixer) # Collect all source files from src/ and nested directories file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS "src/*.cpp") @@ -51,4 +59,4 @@ add_executable(${PROJECT_NAME} ${SOURCES}) target_include_directories(${PROJECT_NAME} PRIVATE include) # Link SDL3 and SDL3_image to the executable -target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3 SDL3_image::SDL3_image SDL3_ttf::SDL3_ttf) +target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3 SDL3_image::SDL3_image SDL3_ttf::SDL3_ttf SDL3_mixer::SDL3_mixer) diff --git a/include/audio/audio.hpp b/include/audio/audio.hpp new file mode 100644 index 0000000..e45cd37 --- /dev/null +++ b/include/audio/audio.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +namespace Game::Audio { + class Audio { + public: + Audio() = default; + DISABLE_COPY_AND_MOVE(Audio) + ~Audio(); + + static Audio& getInstance(); + bool init(); + SDL_AudioDeviceID getAudioDevice() const { return mDevice; } + + private: + SDL_AudioSpec mAudioSpec; + SDL_AudioDeviceID mDevice; + }; +}; \ No newline at end of file diff --git a/include/game/agame/player.hpp b/include/game/agame/player.hpp index fba2832..0ee854f 100644 --- a/include/game/agame/player.hpp +++ b/include/game/agame/player.hpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace Game::AGame { class Player : public Object::Entity { @@ -12,5 +13,8 @@ namespace Game::AGame { ~Player() override = default; void start() override; void update(float deltaTime) override; + + private: + Object::Sound mSound; }; } \ No newline at end of file diff --git a/include/object/sound.hpp b/include/object/sound.hpp new file mode 100644 index 0000000..b1c292f --- /dev/null +++ b/include/object/sound.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include +#include +#include +#include