added music
This commit is contained in:
parent
a1dda3fd50
commit
b5bcd3a812
@ -53,3 +53,6 @@ check_and_download("https://github.com/lua/lua/archive/refs/tags/v5.4.8.zip" "lu
|
|||||||
|
|
||||||
# 10) sol2 (header-only C++ bindings for Lua)
|
# 10) sol2 (header-only C++ bindings for Lua)
|
||||||
check_and_download("https://github.com/ThePhD/sol2/archive/refs/tags/v3.3.0.zip" "sol2-v3.3.0.zip" "sol2-3.3.0" "include/sol/sol.hpp")
|
check_and_download("https://github.com/ThePhD/sol2/archive/refs/tags/v3.3.0.zip" "sol2-v3.3.0.zip" "sol2-3.3.0" "include/sol/sol.hpp")
|
||||||
|
|
||||||
|
# 11) SDL2_mixer
|
||||||
|
check_and_download("https://github.com/libsdl-org/SDL_mixer/archive/refs/tags/release-2.8.0.zip" "SDL_mixer-release-2.8.0.zip" "SDL_mixer-release-2.8.0" "CMakeLists.txt")
|
||||||
|
|||||||
@ -608,3 +608,121 @@ if(NOT TARGET sol2_external_lib)
|
|||||||
target_include_directories(sol2_external_lib INTERFACE "${SOL2_SRC_DIR}/include")
|
target_include_directories(sol2_external_lib INTERFACE "${SOL2_SRC_DIR}/include")
|
||||||
target_link_libraries(sol2_external_lib INTERFACE lua_static)
|
target_link_libraries(sol2_external_lib INTERFACE lua_static)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# 11) SDL2_mixer (2.8.0) – сборка из исходников
|
||||||
|
# ===========================================
|
||||||
|
set(SDL2MIXER_SRC_DIR "${THIRDPARTY_DIR}/SDL_mixer-release-2.8.0")
|
||||||
|
set(SDL2MIXER_BASE_DIR "${SDL2MIXER_SRC_DIR}/install")
|
||||||
|
set(SDL2MIXER_BASE_DIR "${SDL2MIXER_BASE_DIR}" CACHE PATH "SDL2_mixer install base directory" FORCE)
|
||||||
|
|
||||||
|
set(_have_sdl2mixer TRUE)
|
||||||
|
foreach(cfg IN LISTS BUILD_CONFIGS)
|
||||||
|
if(NOT EXISTS "${SDL2MIXER_BASE_DIR}-${cfg}/lib/SDL2_mixer.lib" AND
|
||||||
|
NOT EXISTS "${SDL2MIXER_BASE_DIR}-${cfg}/lib/SDL2_mixerd.lib")
|
||||||
|
set(_have_sdl2mixer FALSE)
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
if(NOT _have_sdl2mixer)
|
||||||
|
foreach(cfg IN LISTS BUILD_CONFIGS)
|
||||||
|
if(cfg STREQUAL "Debug")
|
||||||
|
set(_SDL2_LIB "${SDL2_INSTALL_DIR}/lib/SDL2d.lib")
|
||||||
|
else()
|
||||||
|
set(_SDL2_LIB "${SDL2_INSTALL_DIR}/lib/SDL2.lib")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
log("Configuring SDL2_mixer (${cfg}) ...")
|
||||||
|
execute_process(
|
||||||
|
COMMAND ${CMAKE_COMMAND}
|
||||||
|
-G "${CMAKE_GENERATOR}"
|
||||||
|
-S "${SDL2MIXER_SRC_DIR}"
|
||||||
|
-B "${SDL2MIXER_SRC_DIR}/build-${cfg}"
|
||||||
|
-DCMAKE_INSTALL_PREFIX=${SDL2MIXER_BASE_DIR}-${cfg}
|
||||||
|
-DCMAKE_PREFIX_PATH=${SDL2_INSTALL_DIR}
|
||||||
|
-DSDL2_LIBRARY=${_SDL2_LIB}
|
||||||
|
-DSDL2_INCLUDE_DIR=${SDL2_INSTALL_DIR}/include/SDL2
|
||||||
|
-DSDL2MIXER_DEPS_SHARED=OFF
|
||||||
|
-DSDL2MIXER_VENDORED=ON
|
||||||
|
-DSDL2MIXER_SAMPLES=OFF
|
||||||
|
-DSDL2MIXER_MUSIC_CMD=OFF
|
||||||
|
-DSDL2MIXER_MOD=OFF
|
||||||
|
-DSDL2MIXER_MIDI=OFF
|
||||||
|
-DSDL2MIXER_OPUS=OFF
|
||||||
|
-DSDL2MIXER_WAVPACK=OFF
|
||||||
|
-DSDL2MIXER_MP3_MPG123=OFF
|
||||||
|
-DSDL2MIXER_MP3_DRMP3=ON
|
||||||
|
-DSDL2MIXER_FLAC_DRFLAC=ON
|
||||||
|
-DSDL2MIXER_OGG_STB=ON
|
||||||
|
-DCMAKE_DISABLE_FIND_PACKAGE_OGG=TRUE
|
||||||
|
-DCMAKE_DISABLE_FIND_PACKAGE_Vorbis=TRUE
|
||||||
|
-DCMAKE_DISABLE_FIND_PACKAGE_FLAC=TRUE
|
||||||
|
-DCMAKE_DISABLE_FIND_PACKAGE_MPG123=TRUE
|
||||||
|
-DCMAKE_DISABLE_FIND_PACKAGE_LibModPlug=TRUE
|
||||||
|
-DCMAKE_DISABLE_FIND_PACKAGE_FluidLite=TRUE
|
||||||
|
RESULT_VARIABLE _mixer_cfg_res
|
||||||
|
OUTPUT_VARIABLE _mixer_cfg_out
|
||||||
|
ERROR_VARIABLE _mixer_cfg_err
|
||||||
|
)
|
||||||
|
if(NOT _mixer_cfg_res EQUAL 0)
|
||||||
|
message(STATUS "SDL2_mixer configure stdout: ${_mixer_cfg_out}")
|
||||||
|
message(STATUS "SDL2_mixer configure stderr: ${_mixer_cfg_err}")
|
||||||
|
message(FATAL_ERROR "SDL2_mixer configure failed for ${cfg}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
log("Building SDL2_mixer (${cfg}) ...")
|
||||||
|
execute_process(
|
||||||
|
COMMAND ${CMAKE_COMMAND}
|
||||||
|
--build "${SDL2MIXER_SRC_DIR}/build-${cfg}" --config ${cfg}
|
||||||
|
RESULT_VARIABLE _mixer_build_res
|
||||||
|
)
|
||||||
|
if(NOT _mixer_build_res EQUAL 0)
|
||||||
|
message(FATAL_ERROR "SDL2_mixer build failed for ${cfg}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
log("Installing SDL2_mixer (${cfg}) ...")
|
||||||
|
execute_process(
|
||||||
|
COMMAND ${CMAKE_COMMAND}
|
||||||
|
--install "${SDL2MIXER_SRC_DIR}/build-${cfg}" --config ${cfg}
|
||||||
|
RESULT_VARIABLE _mixer_inst_res
|
||||||
|
)
|
||||||
|
if(NOT _mixer_inst_res EQUAL 0)
|
||||||
|
message(FATAL_ERROR "SDL2_mixer install failed for ${cfg}")
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(_mixer_debug_lib "")
|
||||||
|
foreach(cand
|
||||||
|
"${SDL2MIXER_BASE_DIR}-Debug/lib/SDL2_mixerd.lib"
|
||||||
|
"${SDL2MIXER_BASE_DIR}-Debug/lib/SDL2_mixer.lib"
|
||||||
|
)
|
||||||
|
if(EXISTS "${cand}")
|
||||||
|
set(_mixer_debug_lib "${cand}")
|
||||||
|
break()
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
set(_mixer_release_lib "")
|
||||||
|
foreach(cand
|
||||||
|
"${SDL2MIXER_BASE_DIR}-Release/lib/SDL2_mixer.lib"
|
||||||
|
)
|
||||||
|
if(EXISTS "${cand}")
|
||||||
|
set(_mixer_release_lib "${cand}")
|
||||||
|
break()
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
if(_mixer_debug_lib STREQUAL "" OR _mixer_release_lib STREQUAL "")
|
||||||
|
message(FATAL_ERROR "SDL2_mixer libs not found in ${SDL2MIXER_BASE_DIR}-Debug/Release")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_library(SDL2_mixer_external_lib UNKNOWN IMPORTED GLOBAL)
|
||||||
|
set_target_properties(SDL2_mixer_external_lib PROPERTIES
|
||||||
|
IMPORTED_LOCATION_DEBUG "${_mixer_debug_lib}"
|
||||||
|
IMPORTED_LOCATION_RELEASE "${_mixer_release_lib}"
|
||||||
|
INTERFACE_INCLUDE_DIRECTORIES
|
||||||
|
"$<IF:$<CONFIG:Debug>,${SDL2MIXER_BASE_DIR}-Debug/include,${SDL2MIXER_BASE_DIR}-Release/include>"
|
||||||
|
INTERFACE_LINK_LIBRARIES
|
||||||
|
"SDL2_external_lib"
|
||||||
|
)
|
||||||
@ -114,6 +114,7 @@ target_link_libraries(space-game001 PRIVATE
|
|||||||
eigen_external_lib
|
eigen_external_lib
|
||||||
boost_external_lib
|
boost_external_lib
|
||||||
sol2_external_lib
|
sol2_external_lib
|
||||||
|
SDL2_mixer_external_lib
|
||||||
)
|
)
|
||||||
|
|
||||||
# Линкуем OpenGL (Windows)
|
# Линкуем OpenGL (Windows)
|
||||||
@ -138,6 +139,7 @@ if (WIN32)
|
|||||||
|
|
||||||
set(SDL2TTF_DLL_SRC "$<IF:$<CONFIG:Debug>,${SDL2TTF_BASE_DIR}-Debug/bin/SDL2_ttfd.dll,${SDL2TTF_BASE_DIR}-Release/bin/SDL2_ttf.dll>")
|
set(SDL2TTF_DLL_SRC "$<IF:$<CONFIG:Debug>,${SDL2TTF_BASE_DIR}-Debug/bin/SDL2_ttfd.dll,${SDL2TTF_BASE_DIR}-Release/bin/SDL2_ttf.dll>")
|
||||||
|
|
||||||
|
set(SDL2MIXER_DLL_SRC "$<IF:$<CONFIG:Debug>,${SDL2MIXER_BASE_DIR}-Debug/bin/SDL2_mixerd.dll,${SDL2MIXER_BASE_DIR}-Release/bin/SDL2_mixer.dll>")
|
||||||
|
|
||||||
add_custom_command(TARGET space-game001 POST_BUILD
|
add_custom_command(TARGET space-game001 POST_BUILD
|
||||||
COMMAND ${CMAKE_COMMAND} -E echo "Copying DLLs to output folder..."
|
COMMAND ${CMAKE_COMMAND} -E echo "Copying DLLs to output folder..."
|
||||||
@ -159,6 +161,10 @@ if (WIN32)
|
|||||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||||
"${SDL2TTF_DLL_SRC}"
|
"${SDL2TTF_DLL_SRC}"
|
||||||
"$<TARGET_FILE_DIR:space-game001>"
|
"$<TARGET_FILE_DIR:space-game001>"
|
||||||
|
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/../thirdparty/SDL_mixer-release-2.8.0/install-$<CONFIG>/bin/SDL2_mixer$<$<CONFIG:Debug>:d>.dll"
|
||||||
|
"$<TARGET_FILE_DIR:space-game001>/SDL2_mixer$<$<CONFIG:Debug>:d>.dll"
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|||||||
BIN
resources/sounds/background.wav
Normal file
BIN
resources/sounds/background.wav
Normal file
Binary file not shown.
Binary file not shown.
@ -1,7 +1,5 @@
|
|||||||
#ifdef AUDIO
|
#include "AudioPlayerAsync.h"
|
||||||
|
#include <iostream>
|
||||||
#include "AudioPlayerAsync.h"
|
|
||||||
|
|
||||||
|
|
||||||
AudioPlayerAsync::AudioPlayerAsync() : worker(&AudioPlayerAsync::workerThread, this) {}
|
AudioPlayerAsync::AudioPlayerAsync() : worker(&AudioPlayerAsync::workerThread, this) {}
|
||||||
|
|
||||||
@ -11,62 +9,144 @@ AudioPlayerAsync::~AudioPlayerAsync() {
|
|||||||
stop = true;
|
stop = true;
|
||||||
cv.notify_all();
|
cv.notify_all();
|
||||||
}
|
}
|
||||||
|
if (worker.joinable())
|
||||||
worker.join();
|
worker.join();
|
||||||
|
shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AudioPlayerAsync::init() {
|
||||||
|
if (initialized) return true;
|
||||||
|
|
||||||
|
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
|
||||||
|
std::cerr << "SDL_InitSubSystem(SDL_INIT_AUDIO) failed: " << SDL_GetError() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
|
||||||
|
std::cerr << "Mix_OpenAudio failed: " << Mix_GetError() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mix_AllocateChannels(16);
|
||||||
|
|
||||||
|
initialized = true;
|
||||||
|
std::cout << "AudioPlayerAsync initialized with SDL2_mixer" << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioPlayerAsync::shutdown() {
|
||||||
|
if (!initialized) return;
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(soundCacheMutex);
|
||||||
|
for (auto& pair : soundCache) {
|
||||||
|
Mix_FreeChunk(pair.second);
|
||||||
|
}
|
||||||
|
soundCache.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
Mix_CloseAudio();
|
||||||
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
||||||
|
initialized = false;
|
||||||
|
std::cout << "AudioPlayerAsync shutdown" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioPlayerAsync::playSoundAsync(const std::string& filePath, int loops, int channel) {
|
||||||
|
if (!initialized) {
|
||||||
|
std::cerr << "AudioPlayerAsync not initialized" << std::endl;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerAsync::stopAsync() {
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
taskQueue.push([this]() {
|
taskQueue.push([this, filePath, loops, channel]() {
|
||||||
//audioPlayerMutex.lock();
|
Mix_Chunk* sound = nullptr;
|
||||||
audioPlayer->stop();
|
{
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
std::lock_guard<std::mutex> cacheLock(soundCacheMutex);
|
||||||
//audioPlayerMutex.unlock();
|
auto it = soundCache.find(filePath);
|
||||||
|
if (it != soundCache.end()) {
|
||||||
|
sound = it->second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!sound) {
|
||||||
|
sound = Mix_LoadWAV(filePath.c_str());
|
||||||
|
if (!sound) {
|
||||||
|
std::cerr << "Failed to load sound " << filePath << ": " << Mix_GetError() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::lock_guard<std::mutex> cacheLock(soundCacheMutex);
|
||||||
|
soundCache[filePath] = sound;
|
||||||
|
}
|
||||||
|
|
||||||
|
int result = Mix_PlayChannel(channel, sound, loops);
|
||||||
|
if (result == -1) {
|
||||||
|
std::cerr << "Mix_PlayChannel failed: " << Mix_GetError() << std::endl;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerAsync::resetAsync() {
|
void AudioPlayerAsync::playMusicAsync(const std::string& filePath, int loops) {
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
if (!initialized) return;
|
||||||
taskQueue.push([this]() {
|
|
||||||
//audioPlayerMutex.lock();
|
|
||||||
audioPlayer.reset();
|
|
||||||
audioPlayer = std::make_unique<AudioPlayer>();
|
|
||||||
|
|
||||||
//audioPlayerMutex.unlock();
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
taskQueue.push([this, filePath, loops]() {
|
||||||
|
Mix_Music* music = Mix_LoadMUS(filePath.c_str());
|
||||||
|
if (!music) {
|
||||||
|
std::cerr << "Failed to load music " << filePath << ": " << Mix_GetError() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (Mix_PlayMusic(music, loops) == -1) {
|
||||||
|
std::cerr << "Mix_PlayMusic failed: " << Mix_GetError() << std::endl;
|
||||||
|
Mix_FreeMusic(music);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerAsync::playSoundAsync(std::string soundName) {
|
void AudioPlayerAsync::stopMusicAsync() {
|
||||||
|
if (!initialized) return;
|
||||||
soundNameMutex.lock();
|
|
||||||
latestSoundName = soundName;
|
|
||||||
soundNameMutex.unlock();
|
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
taskQueue.push([this]() {
|
taskQueue.push([]() {
|
||||||
//audioPlayerMutex.lock();
|
Mix_HaltMusic();
|
||||||
if (audioPlayer) {
|
|
||||||
audioPlayer->playSound(latestSoundName);
|
|
||||||
}
|
|
||||||
//audioPlayerMutex.unlock();
|
|
||||||
});
|
});
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerAsync::playMusicAsync(std::string musicName) {
|
void AudioPlayerAsync::pauseMusicAsync() {
|
||||||
|
if (!initialized) return;
|
||||||
musicNameMutex.lock();
|
|
||||||
latestMusicName = musicName;
|
|
||||||
musicNameMutex.unlock();
|
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
taskQueue.push([this]() {
|
taskQueue.push([]() {
|
||||||
//audioPlayerMutex.lock();
|
Mix_PauseMusic();
|
||||||
if (audioPlayer) {
|
});
|
||||||
audioPlayer->playMusic(latestMusicName);
|
cv.notify_one();
|
||||||
}
|
}
|
||||||
//audioPlayerMutex.unlock();
|
|
||||||
|
void AudioPlayerAsync::resumeMusicAsync() {
|
||||||
|
if (!initialized) return;
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
taskQueue.push([]() {
|
||||||
|
Mix_ResumeMusic();
|
||||||
|
});
|
||||||
|
cv.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioPlayerAsync::setMusicVolume(int volume) {
|
||||||
|
if (!initialized) return;
|
||||||
|
volume = std::max(0, std::min(128, volume));
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
taskQueue.push([volume]() {
|
||||||
|
Mix_VolumeMusic(volume);
|
||||||
|
});
|
||||||
|
cv.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioPlayerAsync::setSoundVolume(int volume) {
|
||||||
|
if (!initialized) return;
|
||||||
|
volume = std::max(0, std::min(128, volume));
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
taskQueue.push([volume]() {
|
||||||
|
Mix_Volume(-1, volume); // все каналы
|
||||||
});
|
});
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
}
|
}
|
||||||
@ -76,18 +156,12 @@ void AudioPlayerAsync::workerThread() {
|
|||||||
std::function<void()> task;
|
std::function<void()> task;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
cv.wait(lock, [this]() { return !taskQueue.empty() || stop; });
|
cv.wait(lock, [this] { return !taskQueue.empty() || stop; });
|
||||||
|
if (stop && taskQueue.empty())
|
||||||
if (stop && taskQueue.empty()) {
|
|
||||||
break;
|
break;
|
||||||
}
|
task = std::move(taskQueue.front());
|
||||||
|
|
||||||
task = taskQueue.front();
|
|
||||||
taskQueue.pop();
|
taskQueue.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
task();
|
task();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
@ -1,53 +1,45 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef AUDIO
|
#include <SDL2/SDL_mixer.h>
|
||||||
|
#include <string>
|
||||||
#include <iostream>
|
#include <unordered_map>
|
||||||
#include <thread>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <condition_variable>
|
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <thread>
|
||||||
|
#include <condition_variable>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include "cmakeaudioplayer/include/AudioPlayer.hpp"
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
|
||||||
class AudioPlayerAsync {
|
class AudioPlayerAsync {
|
||||||
public:
|
public:
|
||||||
AudioPlayerAsync();
|
AudioPlayerAsync();
|
||||||
~AudioPlayerAsync();
|
~AudioPlayerAsync();
|
||||||
|
|
||||||
void resetAsync();
|
bool init();
|
||||||
|
void shutdown();
|
||||||
|
|
||||||
void playSoundAsync(std::string soundName);
|
void playSoundAsync(const std::string& filePath, int loops = 0, int channel = -1);
|
||||||
|
void playMusicAsync(const std::string& filePath, int loops = -1);
|
||||||
|
void stopMusicAsync();
|
||||||
|
void pauseMusicAsync();
|
||||||
|
void resumeMusicAsync();
|
||||||
|
void setMusicVolume(int volume); // 0..128
|
||||||
|
void setSoundVolume(int volume); // 0..128
|
||||||
|
|
||||||
void playMusicAsync(std::string musicName);
|
void exit() { stop = true; }
|
||||||
|
|
||||||
void stopAsync();
|
|
||||||
|
|
||||||
void exit()
|
|
||||||
{
|
|
||||||
stop = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::thread worker;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<AudioPlayer> audioPlayer;
|
void workerThread();
|
||||||
//std::mutex audioPlayerMutex;
|
|
||||||
|
|
||||||
std::mutex soundNameMutex;
|
|
||||||
std::mutex musicNameMutex;
|
|
||||||
|
|
||||||
std::string latestSoundName;
|
|
||||||
std::string latestMusicName;
|
|
||||||
|
|
||||||
|
std::thread worker;
|
||||||
std::mutex mtx;
|
std::mutex mtx;
|
||||||
std::condition_variable cv;
|
std::condition_variable cv;
|
||||||
std::queue<std::function<void()>> taskQueue;
|
std::queue<std::function<void()>> taskQueue;
|
||||||
bool stop = false;
|
bool stop = false;
|
||||||
|
|
||||||
void workerThread();
|
std::unordered_map<std::string, Mix_Chunk*> soundCache;
|
||||||
|
std::mutex soundCacheMutex;
|
||||||
|
|
||||||
|
bool initialized = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
|
||||||
23
src/Game.cpp
23
src/Game.cpp
@ -55,6 +55,7 @@ namespace ZL
|
|||||||
: newTickCount(0)
|
: newTickCount(0)
|
||||||
, lastTickCount(0)
|
, lastTickCount(0)
|
||||||
, menuManager(renderer)
|
, menuManager(renderer)
|
||||||
|
, audioPlayer(std::make_unique<AudioPlayerAsync>())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,6 +212,15 @@ namespace ZL
|
|||||||
|
|
||||||
loadingCompleted = true;
|
loadingCompleted = true;
|
||||||
|
|
||||||
|
if (audioPlayer->init()) {
|
||||||
|
audioPlayer->setMusicVolume(100);
|
||||||
|
audioPlayer->setSoundVolume(80);
|
||||||
|
std::cout << "Audio initialized successfully" << std::endl;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "Audio initialization failed" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::drawUI()
|
void Game::drawUI()
|
||||||
@ -472,12 +482,21 @@ namespace ZL
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type == SDL_KEYDOWN/* && dialogueSystem.handleKeyDown(event.key.keysym.sym)*/) {
|
/*if (event.type == SDL_KEYDOWN/* && dialogueSystem.handleKeyDown(event.key.keysym.sym)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
if (event.type == SDL_KEYDOWN && event.key.repeat == 0) {
|
if (event.type == SDL_KEYDOWN && event.key.repeat == 0) {
|
||||||
switch (event.key.keysym.sym) {
|
switch (event.key.keysym.sym) {
|
||||||
|
case SDLK_1:
|
||||||
|
if (audioPlayer) audioPlayer->playSoundAsync("resources/sounds/background.wav");
|
||||||
|
break;
|
||||||
|
case SDLK_2:
|
||||||
|
if (audioPlayer) audioPlayer->playMusicAsync("resources/sounds/lullaby-music-vol20-186394--online-audio-convert.com.ogg");
|
||||||
|
break;
|
||||||
|
case SDLK_3:
|
||||||
|
if (audioPlayer) audioPlayer->stopMusicAsync();
|
||||||
|
break;
|
||||||
case SDLK_f:
|
case SDLK_f:
|
||||||
currentLocation->dialogueSystem.startDialogue("test_cutscene_pan_dialogue_silent");
|
currentLocation->dialogueSystem.startDialogue("test_cutscene_pan_dialogue_silent");
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -20,6 +20,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include "Location.h"
|
#include "Location.h"
|
||||||
|
#include "AudioPlayerAsync.h"
|
||||||
|
|
||||||
namespace ZL {
|
namespace ZL {
|
||||||
|
|
||||||
@ -56,6 +57,7 @@ namespace ZL {
|
|||||||
bool rightMouseDown = false;
|
bool rightMouseDown = false;
|
||||||
int lastMouseX = 0;
|
int lastMouseX = 0;
|
||||||
int lastMouseY = 0;
|
int lastMouseY = 0;
|
||||||
|
std::unique_ptr<AudioPlayerAsync> audioPlayer;
|
||||||
|
|
||||||
int64_t getSyncTimeMs();
|
int64_t getSyncTimeMs();
|
||||||
void processTickCount();
|
void processTickCount();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user