diff --git a/.gitattributes b/.gitattributes index 256ada1..3d5b677 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,3 +2,6 @@ *.png filter=lfs diff=lfs merge=lfs -text *.jpg filter=lfs diff=lfs merge=lfs -text *.anim filter=lfs diff=lfs merge=lfs -text +*.wav filter=lfs diff=lfs merge=lfs -text +*.ogg filter=lfs diff=lfs merge=lfs -text +*.mp3 filter=lfs diff=lfs merge=lfs -text diff --git a/proj-web/CMakeLists.txt b/proj-web/CMakeLists.txt index 2d5b599..f97fb5f 100644 --- a/proj-web/CMakeLists.txt +++ b/proj-web/CMakeLists.txt @@ -149,6 +149,8 @@ set(EMSCRIPTEN_FLAGS "-sUSE_LIBPNG=1" "-sUSE_ZLIB=1" "-sUSE_SDL_TTF=2" + "-sUSE_SDL_MIXER=2" + "-sSDL2_MIXER_FORMATS=[ogg,mp3]" #"-pthread" #"-sUSE_PTHREADS=1" "-fexceptions" @@ -168,6 +170,7 @@ set(EMSCRIPTEN_LINK_FLAGS "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/../resources/loading.png@resources/loading.png" "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/../resources/shaders@resources/shaders" "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/../resources/start.lua@resources/start.lua" + "--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/../audio@audio" ) # Применяем настройки линковки diff --git a/proj-windows/CMakeLists.txt b/proj-windows/CMakeLists.txt index cdfb367..49313ec 100644 --- a/proj-windows/CMakeLists.txt +++ b/proj-windows/CMakeLists.txt @@ -173,6 +173,7 @@ endif() # Какие папки с ресурсами нужно копировать set(RUNTIME_RESOURCE_DIRS "resources" + "audio" ) # Копируем ресурсы и шейдеры в папку exe и в корень build/ diff --git a/resources/sounds/background.wav b/resources/sounds/background.wav deleted file mode 100644 index dfe4693..0000000 Binary files a/resources/sounds/background.wav and /dev/null differ diff --git a/resources/sounds/lullaby-music-vol20-186394--online-audio-convert.com.ogg b/resources/sounds/lullaby-music-vol20-186394--online-audio-convert.com.ogg deleted file mode 100644 index f22a04e..0000000 Binary files a/resources/sounds/lullaby-music-vol20-186394--online-audio-convert.com.ogg and /dev/null differ diff --git a/src/AudioPlayerAsync.cpp b/src/AudioPlayerAsync.cpp index d738ba3..d928f28 100644 --- a/src/AudioPlayerAsync.cpp +++ b/src/AudioPlayerAsync.cpp @@ -1,9 +1,14 @@ -#include "AudioPlayerAsync.h" +#include "AudioPlayerAsync.h" #include +#ifdef __EMSCRIPTEN__ +AudioPlayerAsync::AudioPlayerAsync() {} +#else AudioPlayerAsync::AudioPlayerAsync() : worker(&AudioPlayerAsync::workerThread, this) {} +#endif AudioPlayerAsync::~AudioPlayerAsync() { +#ifndef __EMSCRIPTEN__ { std::unique_lock lock(mtx); stop = true; @@ -11,6 +16,7 @@ AudioPlayerAsync::~AudioPlayerAsync() { } if (worker.joinable()) worker.join(); +#endif shutdown(); } @@ -57,8 +63,7 @@ void AudioPlayerAsync::playSoundAsync(const std::string& filePath, int loops, in return; } - std::unique_lock lock(mtx); - taskQueue.push([this, filePath, loops, channel]() { + auto task = [this, filePath, loops, channel]() { Mix_Chunk* sound = nullptr; { std::lock_guard cacheLock(soundCacheMutex); @@ -82,15 +87,21 @@ void AudioPlayerAsync::playSoundAsync(const std::string& filePath, int loops, in if (result == -1) { std::cerr << "Mix_PlayChannel failed: " << Mix_GetError() << std::endl; } - }); + }; + +#ifdef __EMSCRIPTEN__ + task(); +#else + std::unique_lock lock(mtx); + taskQueue.push(std::move(task)); cv.notify_one(); +#endif } void AudioPlayerAsync::playMusicAsync(const std::string& filePath, int loops) { if (!initialized) return; - std::unique_lock lock(mtx); - taskQueue.push([this, filePath, loops]() { + auto task = [filePath, loops]() { Mix_Music* music = Mix_LoadMUS(filePath.c_str()); if (!music) { std::cerr << "Failed to load music " << filePath << ": " << Mix_GetError() << std::endl; @@ -100,57 +111,85 @@ void AudioPlayerAsync::playMusicAsync(const std::string& filePath, int loops) { std::cerr << "Mix_PlayMusic failed: " << Mix_GetError() << std::endl; Mix_FreeMusic(music); } - }); + }; + +#ifdef __EMSCRIPTEN__ + task(); +#else + std::unique_lock lock(mtx); + taskQueue.push(std::move(task)); cv.notify_one(); +#endif } void AudioPlayerAsync::stopMusicAsync() { if (!initialized) return; +#ifdef __EMSCRIPTEN__ + Mix_HaltMusic(); +#else std::unique_lock lock(mtx); taskQueue.push([]() { Mix_HaltMusic(); }); cv.notify_one(); +#endif } void AudioPlayerAsync::pauseMusicAsync() { if (!initialized) return; +#ifdef __EMSCRIPTEN__ + Mix_PauseMusic(); +#else std::unique_lock lock(mtx); taskQueue.push([]() { Mix_PauseMusic(); }); cv.notify_one(); +#endif } void AudioPlayerAsync::resumeMusicAsync() { if (!initialized) return; +#ifdef __EMSCRIPTEN__ + Mix_ResumeMusic(); +#else std::unique_lock lock(mtx); taskQueue.push([]() { Mix_ResumeMusic(); }); cv.notify_one(); +#endif } void AudioPlayerAsync::setMusicVolume(int volume) { if (!initialized) return; volume = std::max(0, std::min(128, volume)); +#ifdef __EMSCRIPTEN__ + Mix_VolumeMusic(volume); +#else std::unique_lock lock(mtx); taskQueue.push([volume]() { Mix_VolumeMusic(volume); }); cv.notify_one(); +#endif } void AudioPlayerAsync::setSoundVolume(int volume) { if (!initialized) return; volume = std::max(0, std::min(128, volume)); +#ifdef __EMSCRIPTEN__ + Mix_Volume(-1, volume); +#else std::unique_lock lock(mtx); taskQueue.push([volume]() { Mix_Volume(-1, volume); // все каналы }); cv.notify_one(); +#endif } +#ifndef __EMSCRIPTEN__ void AudioPlayerAsync::workerThread() { while (true) { std::function task; @@ -164,4 +203,5 @@ void AudioPlayerAsync::workerThread() { } task(); } -} \ No newline at end of file +} +#endif diff --git a/src/AudioPlayerAsync.h b/src/AudioPlayerAsync.h index 4f97c8f..ab699b5 100644 --- a/src/AudioPlayerAsync.h +++ b/src/AudioPlayerAsync.h @@ -5,11 +5,14 @@ #include #include #include +#include + +#ifndef __EMSCRIPTEN__ #include #include #include #include -#include +#endif class AudioPlayerAsync { public: @@ -30,12 +33,14 @@ public: void exit() { stop = true; } private: +#ifndef __EMSCRIPTEN__ void workerThread(); std::thread worker; std::mutex mtx; std::condition_variable cv; std::queue> taskQueue; +#endif bool stop = false; std::unordered_map soundCache; diff --git a/src/Game.cpp b/src/Game.cpp index 2d73298..d6d19f9 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -487,10 +487,10 @@ namespace ZL if (event.type == SDL_KEYDOWN && event.key.repeat == 0) { switch (event.key.keysym.sym) { case SDLK_1: - if (audioPlayer) audioPlayer->playSoundAsync("resources/sounds/background.wav"); + if (audioPlayer) audioPlayer->playSoundAsync("audio/background.wav"); break; case SDLK_2: - if (audioPlayer) audioPlayer->playMusicAsync("resources/sounds/lullaby-music-vol20-186394--online-audio-convert.com.ogg"); + if (audioPlayer) audioPlayer->playMusicAsync("audio/lullaby-music-vol20-186394--online-audio-convert.com.ogg"); break; case SDLK_3: if (audioPlayer) audioPlayer->stopMusicAsync(); diff --git a/src/items/InteractiveObject.cpp b/src/items/InteractiveObject.cpp index 42a6621..77be231 100644 --- a/src/items/InteractiveObject.cpp +++ b/src/items/InteractiveObject.cpp @@ -12,7 +12,7 @@ namespace ZL { void InteractiveObject::draw(Renderer& renderer) const { if (!isActive || !texture) return; - + /* std::cout << "[DRAW] InteractiveObject::draw() called" << std::endl; std::cout << "[DRAW] Object: " << name << std::endl; std::cout << "[DRAW] Position: (" << position.x() << ", " << position.y() << ", " << position.z() << ")" << std::endl; @@ -22,7 +22,7 @@ namespace ZL { std::cout << "[DRAW] First vertex: (" << mesh.data.PositionData[0].x() << ", " << mesh.data.PositionData[0].y() << ", " << mesh.data.PositionData[0].z() << ")" << std::endl; } - + */ // Apply position transformation renderer.PushMatrix(); renderer.TranslateMatrix(position);