From fff182b4573894b90d861c5a264787abc2fa6b1b Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 10 Sep 2024 04:38:21 -0400 Subject: [PATCH] Refactored out the main_loop so that it's not tightly coupled inside builder, and in the process found I was accidentally copying GUI and GameEngine because Builder wasn't using a &ref for them. Now they don't have a copy constructor to catch that. --- builder.cpp | 22 ++-------------------- builder.hpp | 7 +++---- escape_turings_tarpit.cpp | 3 ++- fsm.hpp | 2 +- game_engine.hpp | 3 +++ gui.cpp | 13 ++++++++----- gui.hpp | 11 ++++++++--- 7 files changed, 27 insertions(+), 34 deletions(-) diff --git a/builder.cpp b/builder.cpp index 89c85b4..94cdd2f 100644 --- a/builder.cpp +++ b/builder.cpp @@ -33,7 +33,6 @@ Builder::Builder(GUI &g, GameEngine &engine) } FILE *start_command(string &build_cmd) { - println(">>>>>>>>> start_command {}", build_cmd); FILE *build_out = popen(build_cmd.c_str(), "r"); dbc::check(build_out != nullptr, "Failed to run command."); return build_out; @@ -51,11 +50,6 @@ string read_line(FILE *build_out, bool &done_out) { } } -bool end_build(FILE *build_out, GUI &gui, string &build_cmd) { - int rc = pclose(build_out); - return rc == 0; -} - MatchResult Builder::parse_line(const string &line) { std::regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*"); @@ -76,24 +70,12 @@ MatchResult Builder::parse_line(const string &line) { } } -void Builder::run() { - int rc = gui.main_loop(game, [&] { - event(GO); - return 0; - }); - - if(rc != 0) println("ERROR IN GUI"); - - event(QUIT); -} - void Builder::building(BuildEvent ev) { - println(">>> BUILDING"); // check if there's output if(build_done) { - bool good = end_build(build_out, gui, build_cmd); + int rc = pclose(build_out); - if(good) { + if(rc == 0) { gui.build_works(); } else { // BUG: make it not play two sounds diff --git a/builder.hpp b/builder.hpp index 16e370e..482f460 100644 --- a/builder.hpp +++ b/builder.hpp @@ -29,8 +29,9 @@ enum BuildEvent { }; class Builder : DeadSimpleFSM { - GUI gui; - GameEngine game; + // FOUND BUG: this was interesting, it got copied but the gui kept working until the refactor + GUI &gui; + GameEngine &game; string git_path = "NOT SET"; string build_cmd = "NOT SET"; efsw::FileWatcher* fileWatcher = NULL; @@ -48,8 +49,6 @@ class Builder : DeadSimpleFSM { MatchResult parse_line(const string &line); - void run(); - void event(BuildEvent ev) override { try { if(ev == QUIT) { diff --git a/escape_turings_tarpit.cpp b/escape_turings_tarpit.cpp index 14d7568..a281b0c 100644 --- a/escape_turings_tarpit.cpp +++ b/escape_turings_tarpit.cpp @@ -1,4 +1,5 @@ #include "builder.hpp" +#include "gui.hpp" #include int main(int argc, char *argv[]) @@ -7,7 +8,7 @@ int main(int argc, char *argv[]) GameEngine game{100}; auto builder = Builder(gui, game); - builder.run(); + gui.main_loop(game, builder); return 1; } diff --git a/fsm.hpp b/fsm.hpp index fc2b505..a6903c4 100644 --- a/fsm.hpp +++ b/fsm.hpp @@ -3,7 +3,7 @@ #include #define FSM_EV(S, F) case S: F(); break -#define FSM_STATE(S, F, E) case S: fmt::println(">>> " #S ":" #F ":{}", int(E)); F(E); break +#define FSM_STATE(S, F, E) case S: F(E); break template class DeadSimpleFSM { diff --git a/game_engine.hpp b/game_engine.hpp index d994c2d..6b63428 100644 --- a/game_engine.hpp +++ b/game_engine.hpp @@ -23,6 +23,9 @@ class GameEngine { GameEngine(int hp); + // FOUND BUG: I was accidentally copying this and shouldn't have been + GameEngine(GameEngine &g) = delete; + int determine_damage(string &type); void start_round(); diff --git a/gui.cpp b/gui.cpp index 131b66e..3d60446 100644 --- a/gui.cpp +++ b/gui.cpp @@ -9,6 +9,7 @@ #include #include #include "sfmlgui.hpp" +#include "builder.hpp" using std::string, std::vector; @@ -46,22 +47,24 @@ GUI::GUI() { building_sound.load(data, "building"); } -void GUI::output(const string &msg) { - lines.push_back(msg); +void GUI::output(const string msg) { + _lines.push_back(msg); } -int GUI::main_loop(GameEngine &game, std::function runner) { +int GUI::main_loop(GameEngine &game, Builder &builder) { auto gui = SFMLGui(game); gui.startup(); while(gui.is_open()) { - bool result = runner(); + builder.event(BuildEvent::GO); gui.handle_events(); gui.update_entities(); - gui.update_log(lines); + gui.update_log(_lines); } + builder.event(BuildEvent::QUIT); + gui.shutdown(); return EXIT_SUCCESS; } diff --git a/gui.hpp b/gui.hpp index 0582561..5b2849c 100644 --- a/gui.hpp +++ b/gui.hpp @@ -9,6 +9,8 @@ using std::string; +class Builder; + class SoundQuip { public: sf::Sound sound; @@ -23,7 +25,7 @@ public: }; class GUI { - std::vector lines; + std::vector _lines; SoundQuip you_died_sound; SoundQuip build_works_sound; @@ -34,9 +36,12 @@ class GUI { GUI(); - void output(const string &msg); + // FOUND BUG: adding this found that I was accidentally copying the gui, really it shouldn't be copyable + GUI(GUI &g) = delete; + + void output(const string msg); - int main_loop(GameEngine &game, std::function runner); + int main_loop(GameEngine &game, Builder &builder); void build_works(); void build_failed(bool play_sound, const string &command);