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.

master
Zed A. Shaw 3 months ago
parent a7c5de6ac3
commit fff182b457
  1. 22
      builder.cpp
  2. 7
      builder.hpp
  3. 3
      escape_turings_tarpit.cpp
  4. 2
      fsm.hpp
  5. 3
      game_engine.hpp
  6. 13
      gui.cpp
  7. 11
      gui.hpp

@ -33,7 +33,6 @@ Builder::Builder(GUI &g, GameEngine &engine)
} }
FILE *start_command(string &build_cmd) { FILE *start_command(string &build_cmd) {
println(">>>>>>>>> start_command {}", build_cmd);
FILE *build_out = popen(build_cmd.c_str(), "r"); FILE *build_out = popen(build_cmd.c_str(), "r");
dbc::check(build_out != nullptr, "Failed to run command."); dbc::check(build_out != nullptr, "Failed to run command.");
return build_out; 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) { MatchResult Builder::parse_line(const string &line) {
std::regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*"); 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) { void Builder::building(BuildEvent ev) {
println(">>> BUILDING");
// check if there's output // check if there's output
if(build_done) { 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(); gui.build_works();
} else { } else {
// BUG: make it not play two sounds // BUG: make it not play two sounds

@ -29,8 +29,9 @@ enum BuildEvent {
}; };
class Builder : DeadSimpleFSM<BuildState, BuildEvent> { class Builder : DeadSimpleFSM<BuildState, BuildEvent> {
GUI gui; // FOUND BUG: this was interesting, it got copied but the gui kept working until the refactor
GameEngine game; GUI &gui;
GameEngine &game;
string git_path = "NOT SET"; string git_path = "NOT SET";
string build_cmd = "NOT SET"; string build_cmd = "NOT SET";
efsw::FileWatcher* fileWatcher = NULL; efsw::FileWatcher* fileWatcher = NULL;
@ -48,8 +49,6 @@ class Builder : DeadSimpleFSM<BuildState, BuildEvent> {
MatchResult parse_line(const string &line); MatchResult parse_line(const string &line);
void run();
void event(BuildEvent ev) override { void event(BuildEvent ev) override {
try { try {
if(ev == QUIT) { if(ev == QUIT) {

@ -1,4 +1,5 @@
#include "builder.hpp" #include "builder.hpp"
#include "gui.hpp"
#include <fmt/core.h> #include <fmt/core.h>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -7,7 +8,7 @@ int main(int argc, char *argv[])
GameEngine game{100}; GameEngine game{100};
auto builder = Builder(gui, game); auto builder = Builder(gui, game);
builder.run(); gui.main_loop(game, builder);
return 1; return 1;
} }

@ -3,7 +3,7 @@
#include <fmt/core.h> #include <fmt/core.h>
#define FSM_EV(S, F) case S: F(); break #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<typename S, typename E> template<typename S, typename E>
class DeadSimpleFSM { class DeadSimpleFSM {

@ -23,6 +23,9 @@ class GameEngine {
GameEngine(int hp); GameEngine(int hp);
// FOUND BUG: I was accidentally copying this and shouldn't have been
GameEngine(GameEngine &g) = delete;
int determine_damage(string &type); int determine_damage(string &type);
void start_round(); void start_round();

@ -9,6 +9,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <fstream> #include <fstream>
#include "sfmlgui.hpp" #include "sfmlgui.hpp"
#include "builder.hpp"
using std::string, std::vector; using std::string, std::vector;
@ -46,22 +47,24 @@ GUI::GUI() {
building_sound.load(data, "building"); building_sound.load(data, "building");
} }
void GUI::output(const string &msg) { void GUI::output(const string msg) {
lines.push_back(msg); _lines.push_back(msg);
} }
int GUI::main_loop(GameEngine &game, std::function<bool()> runner) { int GUI::main_loop(GameEngine &game, Builder &builder) {
auto gui = SFMLGui(game); auto gui = SFMLGui(game);
gui.startup(); gui.startup();
while(gui.is_open()) { while(gui.is_open()) {
bool result = runner(); builder.event(BuildEvent::GO);
gui.handle_events(); gui.handle_events();
gui.update_entities(); gui.update_entities();
gui.update_log(lines); gui.update_log(_lines);
} }
builder.event(BuildEvent::QUIT);
gui.shutdown(); gui.shutdown();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

@ -9,6 +9,8 @@
using std::string; using std::string;
class Builder;
class SoundQuip { class SoundQuip {
public: public:
sf::Sound sound; sf::Sound sound;
@ -23,7 +25,7 @@ public:
}; };
class GUI { class GUI {
std::vector<string> lines; std::vector<string> _lines;
SoundQuip you_died_sound; SoundQuip you_died_sound;
SoundQuip build_works_sound; SoundQuip build_works_sound;
@ -34,9 +36,12 @@ class GUI {
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<bool()> runner); int main_loop(GameEngine &game, Builder &builder);
void build_works(); void build_works();
void build_failed(bool play_sound, const string &command); void build_failed(bool play_sound, const string &command);

Loading…
Cancel
Save