space-game001/src/AudioPlayerAsync.h
2026-04-17 17:13:55 +03:00

50 lines
1.1 KiB
C++

#pragma once
#include <SDL2/SDL_mixer.h>
#include <string>
#include <unordered_map>
#include <memory>
#include <mutex>
#include <SDL2/SDL.h>
#ifndef __EMSCRIPTEN__
#include <queue>
#include <thread>
#include <condition_variable>
#include <functional>
#endif
class AudioPlayerAsync {
public:
AudioPlayerAsync();
~AudioPlayerAsync();
bool init();
void shutdown();
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 exit() { stop = true; }
private:
#ifndef __EMSCRIPTEN__
void workerThread();
std::thread worker;
std::mutex mtx;
std::condition_variable cv;
std::queue<std::function<void()>> taskQueue;
#endif
bool stop = false;
std::unordered_map<std::string, Mix_Chunk*> soundCache;
std::mutex soundCacheMutex;
bool initialized = false;
};