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 4 weeks 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) {
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

@ -29,8 +29,9 @@ enum BuildEvent {
};
class Builder : DeadSimpleFSM<BuildState, BuildEvent> {
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<BuildState, BuildEvent> {
MatchResult parse_line(const string &line);
void run();
void event(BuildEvent ev) override {
try {
if(ev == QUIT) {

@ -1,4 +1,5 @@
#include "builder.hpp"
#include "gui.hpp"
#include <fmt/core.h>
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;
}

@ -3,7 +3,7 @@
#include <fmt/core.h>
#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>
class DeadSimpleFSM {

@ -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();

@ -9,6 +9,7 @@
#include <nlohmann/json.hpp>
#include <fstream>
#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<bool()> 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;
}

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

Loading…
Cancel
Save