Adapting music to web, fixing scripts, fixing bug in initial state

This commit is contained in:
Vladislav Khorev committed 2026-07-24 23:58:29 +03:00
1 parent c3f449b999
commit 5ecf5fb9d7
43 files changed
+875 -388

No files matched your search

+46 -15
View File
@@ -6,6 +6,13 @@
#include <android/log.h>
#endif
namespace FRG
{
extern const char* CONST_MUSIC_ZIP_FILE;
extern const char* CONST_ZIP_FILE;
}
#ifdef __EMSCRIPTEN__
AudioPlayerAsync::AudioPlayerAsync() {}
@@ -92,9 +99,10 @@ void AudioPlayerAsync::playSoundAsync(const std::string& filePath, int loops, in
}
if (!sound) {
#ifdef __ANDROID__
//#ifdef __ANDROID__
std::vector<char> buffer = FRG::readFile(filePath);
//Sounds are loaded from regular resource file
std::vector<char> buffer = FRG::readFileFromZIP(filePath, FRG::CONST_ZIP_FILE);
if (buffer.empty()) {
FRG::logger() << "Failed to read sound file into buffer: " << filePath << std::endl;
@@ -108,9 +116,9 @@ void AudioPlayerAsync::playSoundAsync(const std::string& filePath, int loops, in
sound = Mix_LoadWAV_RW(rw, 1); // 1 means it will free rw automatically
//FRG::logger() << "sound = Mix_LoadWAV_RW(rw, 1); just fired " << std::endl;
#else
sound = Mix_LoadWAV(filePath.c_str());
#endif
//#else
// sound = Mix_LoadWAV(filePath.c_str());
//#endif
if (!sound) {
FRG::logger() << "Failed to load sound " << filePath << ": " << Mix_GetError() << std::endl;
return;
@@ -144,6 +152,10 @@ void AudioPlayerAsync::playMusicAsync(const std::string& filePath, int loops) {
currentTrack = filePath;
if (!musicEnabled_) return;
if ((strlen(FRG::CONST_MUSIC_ZIP_FILE)>0) && !FRG::zipFileExists(FRG::CONST_MUSIC_ZIP_FILE)) {
return;
}
auto task = [this, filePath, loops]() {
Mix_HaltMusic();
if (currentMusic_) {
@@ -153,17 +165,17 @@ void AudioPlayerAsync::playMusicAsync(const std::string& filePath, int loops) {
currentMusicData_.clear();
Mix_Music* music = nullptr;
#ifdef __ANDROID__
currentMusicData_ = FRG::readFile(filePath);
//#ifdef __ANDROID__
currentMusicData_ = FRG::readFileFromZIP(filePath, FRG::CONST_MUSIC_ZIP_FILE);
if (!currentMusicData_.empty()) {
SDL_RWops* rw = SDL_RWFromMem(currentMusicData_.data(), currentMusicData_.size());
if (rw) {
music = Mix_LoadMUS_RW(rw, 1);
}
}
#else
music = Mix_LoadMUS(filePath.c_str());
#endif
//#else
// music = Mix_LoadMUS(filePath.c_str());
//#endif
if (!music) {
FRG::logger() << "Failed to load music " << filePath << ": " << Mix_GetError() << std::endl;
@@ -268,11 +280,30 @@ void AudioPlayerAsync::setSoundVolume(int volume) {
}
void AudioPlayerAsync::crossFadeMusicAsync(const std::string& filePath, int fadeDurationMs, int loops) {
FRG::logger() << "crossFadeMusicAsync called step 1" << std::endl;
if (!initialized) return;
FRG::logger() << "crossFadeMusicAsync called step 1.1" << std::endl;
if ((strlen(FRG::CONST_MUSIC_ZIP_FILE) > 0) && !FRG::zipFileExists(FRG::CONST_MUSIC_ZIP_FILE)) {
FRG::logger() << "crossFadeMusicAsync called step 1.2" << std::endl;
return;
}
FRG::logger() << "crossFadeMusicAsync called step 1.3" << std::endl;
if (filePath == currentTrack) return;
FRG::logger() << "crossFadeMusicAsync called step 1.4" << std::endl;
currentTrack = filePath;
if (!musicEnabled_) return;
FRG::logger() << "crossFadeMusicAsync called step 2" << std::endl;
FRG::logger() << "crossFadeMusicAsync called step 3" << std::endl;
auto task = [this, filePath, fadeDurationMs, loops]() {
#ifdef __EMSCRIPTEN__
Mix_HaltMusic();
@@ -287,17 +318,17 @@ void AudioPlayerAsync::crossFadeMusicAsync(const std::string& filePath, int fade
currentMusicData_.clear();
#endif
Mix_Music* music = nullptr;
#ifdef __ANDROID__
currentMusicData_ = FRG::readFile(filePath);
//#ifdef __ANDROID__
currentMusicData_ = FRG::readFileFromZIP(filePath, FRG::CONST_MUSIC_ZIP_FILE);
if (!currentMusicData_.empty()) {
SDL_RWops* rw = SDL_RWFromMem(currentMusicData_.data(), currentMusicData_.size());
if (rw) {
music = Mix_LoadMUS_RW(rw, 1);
}
}
#else
music = Mix_LoadMUS(filePath.c_str());
#endif
//#else
// music = Mix_LoadMUS(filePath.c_str());
//#endif
if (!music) {
FRG::logger() << "Failed to load music " << filePath << ": " << Mix_GetError() << std::endl;
return;