I can now load a json config file name .tarpit.json to configure everything. It now works to configure the sounds used.
This commit is contained in:
parent
268d8abf52
commit
90f4f727ba
8 changed files with 91 additions and 17 deletions
36
gui.cpp
36
gui.cpp
|
@ -14,25 +14,47 @@
|
|||
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
|
||||
#include <vector>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <fstream>
|
||||
|
||||
using namespace ftxui;
|
||||
using namespace std;
|
||||
using namespace fmt;
|
||||
using namespace nlohmann;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
void load_sound(sf::Sound &sound, sf::SoundBuffer &buffer, const char *in_file) {
|
||||
if(!buffer.loadFromFile(in_file)) {
|
||||
fmt::println("Failed to load {}", in_file);
|
||||
SoundQuip::SoundQuip() {
|
||||
|
||||
};
|
||||
|
||||
void SoundQuip::load(json &data, const char *file_key) {
|
||||
auto audio = data["audio"];
|
||||
json::string_t file_name = audio[file_key].template get<std::string>();
|
||||
|
||||
if(!buffer.loadFromFile(file_name)) {
|
||||
cout << "Failed to load sound: " << file_key << " with file " << file_name << "\n";
|
||||
}
|
||||
|
||||
sound.setBuffer(buffer);
|
||||
}
|
||||
|
||||
void SoundQuip::play() {
|
||||
sound.play();
|
||||
}
|
||||
|
||||
void SoundQuip::stop() {
|
||||
sound.stop();
|
||||
}
|
||||
|
||||
GUI::GUI() {
|
||||
load_sound(you_died_sound, you_died_buffer, "./assets/you_died.wav");
|
||||
load_sound(build_works_sound, build_works_buffer, "./assets/build_works.wav");
|
||||
load_sound(build_failed_sound, build_failed_buffer, "./assets/build_failed.wav");
|
||||
load_sound(building_sound, building_buffer, "./assets/building.wav");
|
||||
ifstream infile(".tarpit.json");
|
||||
json data = json::parse(infile);
|
||||
|
||||
// json load the config file
|
||||
you_died_sound.load(data, "you_died");
|
||||
build_works_sound.load(data, "build_works");
|
||||
build_failed_sound.load(data, "build_failed");
|
||||
building_sound.load(data, "building");
|
||||
}
|
||||
|
||||
void GUI::output(const string &msg) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue