Working on music cross fade and settings

This commit is contained in:
Vladislav Khorev committed 2026-06-24 20:58:31 +03:00
1 parent 84f9fa37a8
commit b8d79c7b7b
13 files changed
+779 -332

No files matched your search

+107 -9
View File
@@ -1,7 +1,9 @@
#include "MenuManager.h"
#include "Game.h"
#include "render/TextRenderer.h"
#include "utils/Utils.h"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
@@ -252,7 +254,7 @@ namespace ZL {
void MenuManager::showMainMenu() {
if (onResetGame) onResetGame();
audioPlayer_.playMusicAsync("audio/main menu final.ogg");
audioPlayer_.crossFadeMusicAsync("audio/main menu final.ogg");
uiState_ = GameUiState::MainMenu;
uiManager.clearMenuStack();
@@ -294,9 +296,72 @@ namespace ZL {
void MenuManager::showSettingsScreen() {
uiManager.pushMenuFromSavedRoot(settingsScreenRoot);
uiManager.setTextButtonCallback("settingsBackButton", [this](const std::string&) {
saveSettings();
uiManager.popMenu();
uiManager.updateAllLayouts();
});
// Set initial slider positions before registering callbacks so
// setSliderValue does not fire an unregistered callback.
const float musicFrac = audioPlayer_.getMusicVolume() / 128.0f;
const float soundFrac = audioPlayer_.getSoundVolume() / 128.0f;
uiManager.setSliderValue("musicVolumeSlider", musicFrac);
uiManager.setSliderValue("soundVolumeSlider", soundFrac);
uiManager.setText("musicVolumeText",
"Music volume: " + std::to_string(audioPlayer_.getMusicVolume()));
uiManager.setText("soundVolumeText",
"Sound volume: " + std::to_string(audioPlayer_.getSoundVolume()));
uiManager.setSliderCallback("musicVolumeSlider",
[this](const std::string&, float value) {
const int vol = static_cast<int>(value * 128.0f + 0.5f);
audioPlayer_.setMusicVolume(vol);
uiManager.setText("musicVolumeText",
"Music volume: " + std::to_string(vol));
});
uiManager.setSliderCallback("soundVolumeSlider",
[this](const std::string&, float value) {
const int vol = static_cast<int>(value * 128.0f + 0.5f);
audioPlayer_.setSoundVolume(vol);
uiManager.setText("soundVolumeText",
"Sound volume: " + std::to_string(vol));
});
// Music on/off toggle — helper to sync button text and color from current state
auto refreshMusicToggle = [this]() {
const bool on = audioPlayer_.isMusicEnabled();
uiManager.setTextButtonText("musicToggleButton", on ? "Music: ON" : "Music: OFF");
uiManager.setTextButtonColor("musicToggleButton", on
? std::array<float, 4>{ 0.2f, 0.9f, 0.2f, 1.0f }
: std::array<float, 4>{ 0.9f, 0.2f, 0.2f, 1.0f });
};
auto refreshSoundToggle = [this]() {
const bool on = audioPlayer_.isSoundEnabled();
uiManager.setTextButtonText("soundToggleButton", on ? "Sound: ON" : "Sound: OFF");
uiManager.setTextButtonColor("soundToggleButton", on
? std::array<float, 4>{ 0.2f, 0.9f, 0.2f, 1.0f }
: std::array<float, 4>{ 0.9f, 0.2f, 0.2f, 1.0f });
};
refreshMusicToggle();
refreshSoundToggle();
uiManager.setTextButtonCallback("musicToggleButton", [this](const std::string&) {
audioPlayer_.setMusicEnabled(!audioPlayer_.isMusicEnabled());
const bool on = audioPlayer_.isMusicEnabled();
uiManager.setTextButtonText("musicToggleButton", on ? "Music: ON" : "Music: OFF");
uiManager.setTextButtonColor("musicToggleButton", on
? std::array<float, 4>{ 0.2f, 0.9f, 0.2f, 1.0f }
: std::array<float, 4>{ 0.9f, 0.2f, 0.2f, 1.0f });
});
uiManager.setTextButtonCallback("soundToggleButton", [this](const std::string&) {
audioPlayer_.setSoundEnabled(!audioPlayer_.isSoundEnabled());
const bool on = audioPlayer_.isSoundEnabled();
uiManager.setTextButtonText("soundToggleButton", on ? "Sound: ON" : "Sound: OFF");
uiManager.setTextButtonColor("soundToggleButton", on
? std::array<float, 4>{ 0.2f, 0.9f, 0.2f, 1.0f }
: std::array<float, 4>{ 0.9f, 0.2f, 0.2f, 1.0f });
});
}
void MenuManager::showLoadGameScreen()
@@ -906,29 +971,29 @@ namespace ZL {
setupGameplayHudCallbacks();
if (gameState_.isDarklands)
{
audioPlayer_.playMusicAsync("audio/bishkek fight calm.ogg");
audioPlayer_.crossFadeMusicAsync("audio/bishkek fight calm.ogg");
}
else if (gameState_.isNight)
{
audioPlayer_.playMusicAsync("audio/bishkek fight calm.ogg");
audioPlayer_.crossFadeMusicAsync("audio/bishkek night.ogg");
}
else
{
audioPlayer_.playMusicAsync("audio/bishkek univer day.ogg");
audioPlayer_.crossFadeMusicAsync("audio/bishkek univer day.ogg");
}
} else if (locationName == "uni_interior") {
applyUniIntHud();
if (gameState_.isDarklands)
{
audioPlayer_.playMusicAsync("audio/bishkek fight calm.ogg");
audioPlayer_.crossFadeMusicAsync("audio/bishkek fight calm.ogg");
}
else if (gameState_.isNight)
{
audioPlayer_.playMusicAsync("audio/bishkek fight calm.ogg");
audioPlayer_.crossFadeMusicAsync("audio/bishkek night.ogg");
}
else
{
audioPlayer_.playMusicAsync("audio/bishkek univer day.ogg");
audioPlayer_.crossFadeMusicAsync("audio/bishkek univer day.ogg");
}
} else {
// Returning to dorm: reuse step5ab, suppress already-completed hints
@@ -938,11 +1003,11 @@ namespace ZL {
if (gameState_.isNight)
{
audioPlayer_.playMusicAsync("audio/bishkek fight calm.ogg");
audioPlayer_.crossFadeMusicAsync("audio/bishkek night.ogg");
}
else
{
audioPlayer_.playMusicAsync("audio/obshaga.ogg");
audioPlayer_.crossFadeMusicAsync("audio/obshaga.ogg");
}
}
}
@@ -1452,4 +1517,37 @@ namespace ZL {
updateToasts(deltaMs);
}
void MenuManager::saveSettings() {
nlohmann::json root;
root["musicVolume"] = audioPlayer_.getMusicVolume();
root["soundVolume"] = audioPlayer_.getSoundVolume();
root["musicEnabled"] = audioPlayer_.isMusicEnabled();
root["soundEnabled"] = audioPlayer_.isSoundEnabled();
std::ofstream file("settings.json");
if (file.is_open()) {
file << root.dump(2);
} else {
std::cerr << "[settings] Could not open settings.json for writing" << std::endl;
}
}
void MenuManager::loadSettings() {
const std::string content = ZL::readTextFile("settings.json");
if (content.empty()) return;
try {
const nlohmann::json root = nlohmann::json::parse(content);
if (root.contains("musicVolume"))
audioPlayer_.setMusicVolume(root["musicVolume"].get<int>());
if (root.contains("soundVolume"))
audioPlayer_.setSoundVolume(root["soundVolume"].get<int>());
if (root.contains("musicEnabled"))
audioPlayer_.setMusicEnabled(root["musicEnabled"].get<bool>());
if (root.contains("soundEnabled"))
audioPlayer_.setSoundEnabled(root["soundEnabled"].get<bool>());
} catch (const std::exception& e) {
std::cerr << "[settings] Failed to parse settings.json: " << e.what() << std::endl;
}
}
} // namespace ZL