From 90f4f727baeb1de70034507c241f524716272b61 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 26 Aug 2024 19:04:50 -0400 Subject: [PATCH] I can now load a json config file name .tarpit.json to configure everything. It now works to configure the sounds used. --- .tarpit.json | 9 +++++++++ escape_turings_tarpit.cpp | 2 +- gui.cpp | 36 +++++++++++++++++++++++++++++------- gui.hpp | 26 ++++++++++++++++++-------- jsontest.cpp | 27 +++++++++++++++++++++++++++ meson.build | 6 +++++- scripts/reset_build.ps1 | 1 + scripts/reset_build.sh | 1 + 8 files changed, 91 insertions(+), 17 deletions(-) create mode 100644 .tarpit.json create mode 100644 jsontest.cpp diff --git a/.tarpit.json b/.tarpit.json new file mode 100644 index 0000000..afbdd5e --- /dev/null +++ b/.tarpit.json @@ -0,0 +1,9 @@ +{ + "audio": { + "you_died": "./assets/you_died.wav", + "build_works": "./assets/build_works.wav", + "build_failed": "./assets/build_failed.wav", + "building": "./assets/building.wav" + }, + "build_cmd": "meson compile -C builddir" +} diff --git a/escape_turings_tarpit.cpp b/escape_turings_tarpit.cpp index f79b79b..2c5b56a 100644 --- a/escape_turings_tarpit.cpp +++ b/escape_turings_tarpit.cpp @@ -4,7 +4,7 @@ int main(int argc, char *argv[]) { if(argc != 3) { - fmt::println("USAGE: watchgit PATH BUILD_CMD"); + fmt::println("USAGE: escape_turings_tarpit PATH BUILD_CMD"); return 1; } else { const char *git_path = argv[1]; diff --git a/gui.cpp b/gui.cpp index 2706e44..fa7b8a7 100644 --- a/gui.cpp +++ b/gui.cpp @@ -14,25 +14,47 @@ #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive #include #include +#include +#include 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(); + + 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) { diff --git a/gui.hpp b/gui.hpp index c6a452c..aba7cc0 100644 --- a/gui.hpp +++ b/gui.hpp @@ -5,18 +5,28 @@ #include #include #include +#include + +class SoundQuip { +public: + sf::Sound sound; + sf::SoundBuffer buffer; + bool initialized; + + SoundQuip(); + + void load(nlohmann::json &data, const char *in_file); + void play(); + void stop(); +}; class GUI { vector lines; - sf::Sound you_died_sound; - sf::Sound build_works_sound; - sf::Sound build_failed_sound; - sf::Sound building_sound; - sf::SoundBuffer you_died_buffer; - sf::SoundBuffer build_works_buffer; - sf::SoundBuffer build_failed_buffer; - sf::SoundBuffer building_buffer; + SoundQuip you_died_sound; + SoundQuip build_works_sound; + SoundQuip build_failed_sound; + SoundQuip building_sound; public: diff --git a/jsontest.cpp b/jsontest.cpp new file mode 100644 index 0000000..f9497f0 --- /dev/null +++ b/jsontest.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include +using json = nlohmann::json; + +using namespace fmt; +using namespace std; + +int main(int argc, char *argv[]) { + if(argc != 2) { + println("USAGE: jsontest [jsonfile]"); + return 0; + } + + ifstream infile(argv[1]); + json data = json::parse(infile); + + json::string_t s2 = data["happy"].template get(); + + for(auto &el : data.items()) { + cout << el.key() << "=" << el.value() << "\n"; + } + + println("DATA HAPPY {}", s2); + return 0; +} diff --git a/meson.build b/meson.build index e8fcf73..d805901 100644 --- a/meson.build +++ b/meson.build @@ -20,11 +20,12 @@ ftxui_dom = dependency('ftxui-dom') ftxui_component = dependency('ftxui-component') catch2 = dependency('catch2-with-main') sfml = dependency('sfml') +json = dependency('nlohmann_json') dependencies = [ fmt, libgit2package_dep, efsw_dep, ftxui_screen, ftxui_dom, ftxui_component, - sfml + sfml, json ] executable('escape_turings_tarpit', @@ -47,6 +48,9 @@ executable('ftx_thread_test', 'ftx_thread_test.cpp', executable('audiotest', 'audiotest.cpp', dependencies: dependencies) +executable('jsontest', 'jsontest.cpp', + dependencies: dependencies) + runtests = executable('runtests', [ 'game_engine.cpp', 'tests/game_engine.cpp'], diff --git a/scripts/reset_build.ps1 b/scripts/reset_build.ps1 index 7de185d..3c7f3b5 100644 --- a/scripts/reset_build.ps1 +++ b/scripts/reset_build.ps1 @@ -16,6 +16,7 @@ meson wrap install flac meson wrap install freetype2 meson wrap install openal-soft meson wrap install sfml +meson wrap install nlohmann_json # $env:CC="clang" # $env:CXX="clang++" meson setup --default-library=static --prefer-static builddir diff --git a/scripts/reset_build.sh b/scripts/reset_build.sh index 06be5f5..6a8814f 100644 --- a/scripts/reset_build.sh +++ b/scripts/reset_build.sh @@ -19,4 +19,5 @@ meson wrap install flac meson wrap install freetype2 meson wrap install openal-soft meson wrap install sfml +meson wrap install nlohmann_json meson setup builddir