From c0ad0c8d236ab864486d5ecfb84367958839653a Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 10 Sep 2024 22:40:02 -0400 Subject: [PATCH] Rework the GUI so it uses SFMLBackend by moving the SoundQuip into SFMLBackend. --- gui.cpp | 20 -------------------- gui.hpp | 20 ++------------------ sfmlbackend.cpp | 22 ++++++++++++++++++++++ sfmlbackend.hpp | 15 +++++++++++++++ 4 files changed, 39 insertions(+), 38 deletions(-) diff --git a/gui.cpp b/gui.cpp index 45647b4..0da84eb 100644 --- a/gui.cpp +++ b/gui.cpp @@ -8,7 +8,6 @@ #include #include #include -#include "sfmlbackend.hpp" #include "builder.hpp" using std::string, std::vector; @@ -17,25 +16,6 @@ using namespace fmt; using namespace nlohmann; namespace fs = std::filesystem; -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)) { - println("Failed to load sound: {} with file {}", file_key, file_name); - } - - sound.setBuffer(buffer); -} - -void SoundQuip::play() { - sound.play(); -} - -void SoundQuip::stop() { - sound.stop(); -} - GUI::GUI() { std::ifstream infile(".tarpit.json"); json data = json::parse(infile); diff --git a/gui.hpp b/gui.hpp index 5b2849c..b54b558 100644 --- a/gui.hpp +++ b/gui.hpp @@ -1,29 +1,13 @@ #pragma once -#include -#include "game_engine.hpp" -#include #include -#include -#include +#include "game_engine.hpp" +#include "sfmlbackend.hpp" using std::string; class Builder; -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 { std::vector _lines; diff --git a/sfmlbackend.cpp b/sfmlbackend.cpp index 6700617..64bc203 100644 --- a/sfmlbackend.cpp +++ b/sfmlbackend.cpp @@ -9,11 +9,33 @@ #include #include #include +#include #include "sfmlbackend.hpp" +using namespace fmt; +using namespace nlohmann; using namespace ImGui; using std::string; +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)) { + println("Failed to load sound: {} with file {}", file_key, file_name); + } + + sound.setBuffer(buffer); +} + +void SoundQuip::play() { + sound.play(); +} + +void SoundQuip::stop() { + sound.stop(); +} + void SFMLBackend::ImGui_setup() { bool res = SFML::Init(window); fmt::println("IMGUI returned {}", res); diff --git a/sfmlbackend.hpp b/sfmlbackend.hpp index dd7f77a..8c1496d 100644 --- a/sfmlbackend.hpp +++ b/sfmlbackend.hpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include "game_engine.hpp" #include @@ -15,6 +17,19 @@ constexpr int TEXT_SIZE = 48; constexpr int Y_LINES = 23; constexpr int X_ROWS = 48; +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 SFMLBackend { sf::ContextSettings settings; sf::RenderWindow window;