Working on audio in web part 1
This commit is contained in:
parent
3778a603f2
commit
b6e0422d71
3
.gitattributes
vendored
3
.gitattributes
vendored
@ -2,3 +2,6 @@
|
|||||||
*.png filter=lfs diff=lfs merge=lfs -text
|
*.png filter=lfs diff=lfs merge=lfs -text
|
||||||
*.jpg filter=lfs diff=lfs merge=lfs -text
|
*.jpg filter=lfs diff=lfs merge=lfs -text
|
||||||
*.anim 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
|
||||||
|
|||||||
@ -149,6 +149,8 @@ set(EMSCRIPTEN_FLAGS
|
|||||||
"-sUSE_LIBPNG=1"
|
"-sUSE_LIBPNG=1"
|
||||||
"-sUSE_ZLIB=1"
|
"-sUSE_ZLIB=1"
|
||||||
"-sUSE_SDL_TTF=2"
|
"-sUSE_SDL_TTF=2"
|
||||||
|
"-sUSE_SDL_MIXER=2"
|
||||||
|
"-sSDL2_MIXER_FORMATS=[ogg,mp3]"
|
||||||
#"-pthread"
|
#"-pthread"
|
||||||
#"-sUSE_PTHREADS=1"
|
#"-sUSE_PTHREADS=1"
|
||||||
"-fexceptions"
|
"-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/loading.png@resources/loading.png"
|
||||||
"--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/../resources/shaders@resources/shaders"
|
"--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}/../resources/start.lua@resources/start.lua"
|
||||||
|
"--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/../audio@audio"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Применяем настройки линковки
|
# Применяем настройки линковки
|
||||||
|
|||||||
@ -173,6 +173,7 @@ endif()
|
|||||||
# Какие папки с ресурсами нужно копировать
|
# Какие папки с ресурсами нужно копировать
|
||||||
set(RUNTIME_RESOURCE_DIRS
|
set(RUNTIME_RESOURCE_DIRS
|
||||||
"resources"
|
"resources"
|
||||||
|
"audio"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Копируем ресурсы и шейдеры в папку exe и в корень build/
|
# Копируем ресурсы и шейдеры в папку exe и в корень build/
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@ -1,9 +1,14 @@
|
|||||||
#include "AudioPlayerAsync.h"
|
#include "AudioPlayerAsync.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
AudioPlayerAsync::AudioPlayerAsync() {}
|
||||||
|
#else
|
||||||
AudioPlayerAsync::AudioPlayerAsync() : worker(&AudioPlayerAsync::workerThread, this) {}
|
AudioPlayerAsync::AudioPlayerAsync() : worker(&AudioPlayerAsync::workerThread, this) {}
|
||||||
|
#endif
|
||||||
|
|
||||||
AudioPlayerAsync::~AudioPlayerAsync() {
|
AudioPlayerAsync::~AudioPlayerAsync() {
|
||||||
|
#ifndef __EMSCRIPTEN__
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
stop = true;
|
stop = true;
|
||||||
@ -11,6 +16,7 @@ AudioPlayerAsync::~AudioPlayerAsync() {
|
|||||||
}
|
}
|
||||||
if (worker.joinable())
|
if (worker.joinable())
|
||||||
worker.join();
|
worker.join();
|
||||||
|
#endif
|
||||||
shutdown();
|
shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,8 +63,7 @@ void AudioPlayerAsync::playSoundAsync(const std::string& filePath, int loops, in
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
auto task = [this, filePath, loops, channel]() {
|
||||||
taskQueue.push([this, filePath, loops, channel]() {
|
|
||||||
Mix_Chunk* sound = nullptr;
|
Mix_Chunk* sound = nullptr;
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> cacheLock(soundCacheMutex);
|
std::lock_guard<std::mutex> cacheLock(soundCacheMutex);
|
||||||
@ -82,15 +87,21 @@ void AudioPlayerAsync::playSoundAsync(const std::string& filePath, int loops, in
|
|||||||
if (result == -1) {
|
if (result == -1) {
|
||||||
std::cerr << "Mix_PlayChannel failed: " << Mix_GetError() << std::endl;
|
std::cerr << "Mix_PlayChannel failed: " << Mix_GetError() << std::endl;
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
task();
|
||||||
|
#else
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
taskQueue.push(std::move(task));
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerAsync::playMusicAsync(const std::string& filePath, int loops) {
|
void AudioPlayerAsync::playMusicAsync(const std::string& filePath, int loops) {
|
||||||
if (!initialized) return;
|
if (!initialized) return;
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
auto task = [filePath, loops]() {
|
||||||
taskQueue.push([this, filePath, loops]() {
|
|
||||||
Mix_Music* music = Mix_LoadMUS(filePath.c_str());
|
Mix_Music* music = Mix_LoadMUS(filePath.c_str());
|
||||||
if (!music) {
|
if (!music) {
|
||||||
std::cerr << "Failed to load music " << filePath << ": " << Mix_GetError() << std::endl;
|
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;
|
std::cerr << "Mix_PlayMusic failed: " << Mix_GetError() << std::endl;
|
||||||
Mix_FreeMusic(music);
|
Mix_FreeMusic(music);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
task();
|
||||||
|
#else
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
taskQueue.push(std::move(task));
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerAsync::stopMusicAsync() {
|
void AudioPlayerAsync::stopMusicAsync() {
|
||||||
if (!initialized) return;
|
if (!initialized) return;
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
Mix_HaltMusic();
|
||||||
|
#else
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
taskQueue.push([]() {
|
taskQueue.push([]() {
|
||||||
Mix_HaltMusic();
|
Mix_HaltMusic();
|
||||||
});
|
});
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerAsync::pauseMusicAsync() {
|
void AudioPlayerAsync::pauseMusicAsync() {
|
||||||
if (!initialized) return;
|
if (!initialized) return;
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
Mix_PauseMusic();
|
||||||
|
#else
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
taskQueue.push([]() {
|
taskQueue.push([]() {
|
||||||
Mix_PauseMusic();
|
Mix_PauseMusic();
|
||||||
});
|
});
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerAsync::resumeMusicAsync() {
|
void AudioPlayerAsync::resumeMusicAsync() {
|
||||||
if (!initialized) return;
|
if (!initialized) return;
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
Mix_ResumeMusic();
|
||||||
|
#else
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
taskQueue.push([]() {
|
taskQueue.push([]() {
|
||||||
Mix_ResumeMusic();
|
Mix_ResumeMusic();
|
||||||
});
|
});
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerAsync::setMusicVolume(int volume) {
|
void AudioPlayerAsync::setMusicVolume(int volume) {
|
||||||
if (!initialized) return;
|
if (!initialized) return;
|
||||||
volume = std::max(0, std::min(128, volume));
|
volume = std::max(0, std::min(128, volume));
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
Mix_VolumeMusic(volume);
|
||||||
|
#else
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
taskQueue.push([volume]() {
|
taskQueue.push([volume]() {
|
||||||
Mix_VolumeMusic(volume);
|
Mix_VolumeMusic(volume);
|
||||||
});
|
});
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void AudioPlayerAsync::setSoundVolume(int volume) {
|
void AudioPlayerAsync::setSoundVolume(int volume) {
|
||||||
if (!initialized) return;
|
if (!initialized) return;
|
||||||
volume = std::max(0, std::min(128, volume));
|
volume = std::max(0, std::min(128, volume));
|
||||||
|
#ifdef __EMSCRIPTEN__
|
||||||
|
Mix_Volume(-1, volume);
|
||||||
|
#else
|
||||||
std::unique_lock<std::mutex> lock(mtx);
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
taskQueue.push([volume]() {
|
taskQueue.push([volume]() {
|
||||||
Mix_Volume(-1, volume); // все каналы
|
Mix_Volume(-1, volume); // все каналы
|
||||||
});
|
});
|
||||||
cv.notify_one();
|
cv.notify_one();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef __EMSCRIPTEN__
|
||||||
void AudioPlayerAsync::workerThread() {
|
void AudioPlayerAsync::workerThread() {
|
||||||
while (true) {
|
while (true) {
|
||||||
std::function<void()> task;
|
std::function<void()> task;
|
||||||
@ -165,3 +204,4 @@ void AudioPlayerAsync::workerThread() {
|
|||||||
task();
|
task();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|||||||
@ -5,11 +5,14 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
#ifndef __EMSCRIPTEN__
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <SDL2/SDL.h>
|
#endif
|
||||||
|
|
||||||
class AudioPlayerAsync {
|
class AudioPlayerAsync {
|
||||||
public:
|
public:
|
||||||
@ -30,12 +33,14 @@ public:
|
|||||||
void exit() { stop = true; }
|
void exit() { stop = true; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
#ifndef __EMSCRIPTEN__
|
||||||
void workerThread();
|
void workerThread();
|
||||||
|
|
||||||
std::thread worker;
|
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;
|
||||||
|
#endif
|
||||||
bool stop = false;
|
bool stop = false;
|
||||||
|
|
||||||
std::unordered_map<std::string, Mix_Chunk*> soundCache;
|
std::unordered_map<std::string, Mix_Chunk*> soundCache;
|
||||||
|
|||||||
@ -487,10 +487,10 @@ namespace ZL
|
|||||||
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:
|
case SDLK_1:
|
||||||
if (audioPlayer) audioPlayer->playSoundAsync("resources/sounds/background.wav");
|
if (audioPlayer) audioPlayer->playSoundAsync("audio/background.wav");
|
||||||
break;
|
break;
|
||||||
case SDLK_2:
|
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;
|
break;
|
||||||
case SDLK_3:
|
case SDLK_3:
|
||||||
if (audioPlayer) audioPlayer->stopMusicAsync();
|
if (audioPlayer) audioPlayer->stopMusicAsync();
|
||||||
|
|||||||
@ -12,7 +12,7 @@ namespace ZL {
|
|||||||
|
|
||||||
void InteractiveObject::draw(Renderer& renderer) const {
|
void InteractiveObject::draw(Renderer& renderer) const {
|
||||||
if (!isActive || !texture) return;
|
if (!isActive || !texture) return;
|
||||||
|
/*
|
||||||
std::cout << "[DRAW] InteractiveObject::draw() called" << std::endl;
|
std::cout << "[DRAW] InteractiveObject::draw() called" << std::endl;
|
||||||
std::cout << "[DRAW] Object: " << name << std::endl;
|
std::cout << "[DRAW] Object: " << name << std::endl;
|
||||||
std::cout << "[DRAW] Position: (" << position.x() << ", " << position.y() << ", " << position.z() << ")" << 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() << ", "
|
std::cout << "[DRAW] First vertex: (" << mesh.data.PositionData[0].x() << ", "
|
||||||
<< mesh.data.PositionData[0].y() << ", " << mesh.data.PositionData[0].z() << ")" << std::endl;
|
<< mesh.data.PositionData[0].y() << ", " << mesh.data.PositionData[0].z() << ")" << std::endl;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
// Apply position transformation
|
// Apply position transformation
|
||||||
renderer.PushMatrix();
|
renderer.PushMatrix();
|
||||||
renderer.TranslateMatrix(position);
|
renderer.TranslateMatrix(position);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user